summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-01-20 08:31:15 (GMT)
committerGitHub <noreply@github.com>2023-01-20 08:31:15 (GMT)
commitb2a662fc6b391f176bbf9da45657d21ef5b363ac (patch)
treebceca26823f948445aa0e900b1971a371d3cea0a /Lib/test
parent10c61301240374701279aa8f87bf9f1caf1e9c9a (diff)
downloadcpython-b2a662fc6b391f176bbf9da45657d21ef5b363ac.zip
cpython-b2a662fc6b391f176bbf9da45657d21ef5b363ac.tar.gz
cpython-b2a662fc6b391f176bbf9da45657d21ef5b363ac.tar.bz2
[3.10] gh-101144: Allow zipfile.Path .open & .read_text encoding to be positional (GH-101179) (GH-101182)
The zipfile.Path open() and read_text() encoding parameter can be supplied as a positional argument without causing a TypeError again. 3.10.0b1 included a regression that made it keyword only. Documentation update included as users writing code to be compatible with a wide range of versions will need to consider this for some time.. (cherry picked from commit 5927013e47a8c63b70e104152351f3447baa819c) (cherry picked from commit efe3a389cabd7295e6e0938767cdc4055c871e3c) Co-authored-by: Gregory P. Smith <greg@krypto.org> [Google] Automerge-Triggered-By: GH:gpshead
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_zipfile.py66
1 files changed, 65 insertions, 1 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index e557d56..cf40412 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -10,6 +10,7 @@ import string
import struct
import subprocess
import sys
+from test.support.script_helper import assert_python_ok
import time
import unittest
import unittest.mock as mock
@@ -2933,7 +2934,69 @@ class TestPath(unittest.TestCase):
a, b, g = root.iterdir()
with a.open(encoding="utf-8") as strm:
data = strm.read()
- assert data == "content of a"
+ self.assertEqual(data, "content of a")
+ with a.open('r', "utf-8") as strm: # not a kw, no gh-101144 TypeError
+ data = strm.read()
+ self.assertEqual(data, "content of a")
+
+ def test_open_encoding_utf16(self):
+ in_memory_file = io.BytesIO()
+ zf = zipfile.ZipFile(in_memory_file, "w")
+ zf.writestr("path/16.txt", "This was utf-16".encode("utf-16"))
+ zf.filename = "test_open_utf16.zip"
+ root = zipfile.Path(zf)
+ (path,) = root.iterdir()
+ u16 = path.joinpath("16.txt")
+ with u16.open('r', "utf-16") as strm:
+ data = strm.read()
+ self.assertEqual(data, "This was utf-16")
+ with u16.open(encoding="utf-16") as strm:
+ data = strm.read()
+ self.assertEqual(data, "This was utf-16")
+
+ def test_open_encoding_errors(self):
+ in_memory_file = io.BytesIO()
+ zf = zipfile.ZipFile(in_memory_file, "w")
+ zf.writestr("path/bad-utf8.bin", b"invalid utf-8: \xff\xff.")
+ zf.filename = "test_read_text_encoding_errors.zip"
+ root = zipfile.Path(zf)
+ (path,) = root.iterdir()
+ u16 = path.joinpath("bad-utf8.bin")
+
+ # encoding= as a positional argument for gh-101144.
+ data = u16.read_text("utf-8", errors="ignore")
+ self.assertEqual(data, "invalid utf-8: .")
+ with u16.open("r", "utf-8", errors="surrogateescape") as f:
+ self.assertEqual(f.read(), "invalid utf-8: \udcff\udcff.")
+
+ # encoding= both positional and keyword is an error; gh-101144.
+ with self.assertRaisesRegex(TypeError, "encoding"):
+ data = u16.read_text("utf-8", encoding="utf-8")
+
+ # both keyword arguments work.
+ with u16.open("r", encoding="utf-8", errors="strict") as f:
+ # error during decoding with wrong codec.
+ with self.assertRaises(UnicodeDecodeError):
+ f.read()
+
+ def test_encoding_warnings(self):
+ """EncodingWarning must blame the read_text and open calls."""
+ code = '''\
+import io, zipfile
+with zipfile.ZipFile(io.BytesIO(), "w") as zf:
+ zf.filename = '<test_encoding_warnings in memory zip file>'
+ zf.writestr("path/file.txt", b"Spanish Inquisition")
+ root = zipfile.Path(zf)
+ (path,) = root.iterdir()
+ file_path = path.joinpath("file.txt")
+ unused = file_path.read_text() # should warn
+ file_path.open("r").close() # should warn
+'''
+ proc = assert_python_ok('-X', 'warn_default_encoding', '-c', code)
+ warnings = proc.err.splitlines()
+ self.assertEqual(len(warnings), 2, proc.err)
+ self.assertRegex(warnings[0], rb"^<string>:8: EncodingWarning:")
+ self.assertRegex(warnings[1], rb"^<string>:9: EncodingWarning:")
def test_open_write(self):
"""
@@ -2975,6 +3038,7 @@ class TestPath(unittest.TestCase):
root = zipfile.Path(alpharep)
a, b, g = root.iterdir()
assert a.read_text(encoding="utf-8") == "content of a"
+ a.read_text("utf-8") # No positional arg TypeError per gh-101144.
assert a.read_bytes() == b"content of a"
@pass_alpharep