diff options
author | Victor Stinner <vstinner@python.org> | 2020-03-04 18:50:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-04 18:50:22 +0100 |
commit | 942f7a2dea2e95a0fa848329565c0d0288d92e47 (patch) | |
tree | 31abda8d45ef676a46ea63a6a8a61ccf6061e9c5 /Lib/test | |
parent | 00c77ae55a82548a6b45af73cdf712ea34910645 (diff) | |
download | cpython-942f7a2dea2e95a0fa848329565c0d0288d92e47.tar.gz cpython-942f7a2dea2e95a0fa848329565c0d0288d92e47.zip |
bpo-39674: Revert "bpo-37330: open() no longer accept 'U' in file mode (GH-16959)" (GH-18767)
This reverts commit e471e72977c83664f13d041c78549140c86c92de.
The mode will be removed from Python 3.10.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_codecs.py | 18 | ||||
-rw-r--r-- | Lib/test/test_fileinput.py | 22 | ||||
-rw-r--r-- | Lib/test/test_io.py | 17 |
3 files changed, 30 insertions, 27 deletions
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index dcdd574bc7f..54a3520802a 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -712,23 +712,11 @@ class UTF16Test(ReadTest, unittest.TestCase): self.addCleanup(support.unlink, support.TESTFN) with open(support.TESTFN, 'wb') as fp: fp.write(s) - with codecs.open(support.TESTFN, 'r', - encoding=self.encoding) as reader: + with support.check_warnings(('', DeprecationWarning)): + reader = codecs.open(support.TESTFN, 'U', encoding=self.encoding) + with reader: self.assertEqual(reader.read(), s1) - def test_invalid_modes(self): - for mode in ('U', 'rU', 'r+U'): - with self.assertRaises(ValueError) as cm: - codecs.open(support.TESTFN, mode, encoding=self.encoding) - self.assertIn('invalid mode', str(cm.exception)) - - for mode in ('rt', 'wt', 'at', 'r+t'): - with self.assertRaises(ValueError) as cm: - codecs.open(support.TESTFN, mode, encoding=self.encoding) - self.assertIn("can't have text and binary mode at once", - str(cm.exception)) - - class UTF16LETest(ReadTest, unittest.TestCase): encoding = "utf-16-le" ill_formed_sequence = b"\x80\xdc" diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py index 819557d5e86..014f19e6cbd 100644 --- a/Lib/test/test_fileinput.py +++ b/Lib/test/test_fileinput.py @@ -226,11 +226,19 @@ class FileInputTests(BaseTests, unittest.TestCase): self.assertEqual(fi.fileno(), -1) def test_opening_mode(self): - # invalid modes - for mode in ('w', 'rU', 'U'): - with self.subTest(mode=mode): - with self.assertRaises(ValueError): - FileInput(mode=mode) + try: + # invalid mode, should raise ValueError + fi = FileInput(mode="w") + self.fail("FileInput should reject invalid mode argument") + except ValueError: + pass + # try opening in universal newline mode + t1 = self.writeTmp(b"A\nB\r\nC\rD", mode="wb") + with check_warnings(('', DeprecationWarning)): + fi = FileInput(files=t1, mode="U") + with check_warnings(('', DeprecationWarning)): + lines = list(fi) + self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"]) def test_stdin_binary_mode(self): with mock.patch('sys.stdin') as m_stdin: @@ -977,6 +985,10 @@ class Test_hook_encoded(unittest.TestCase): self.assertEqual(lines, expected_lines) check('r', ['A\n', 'B\n', 'C\n', 'D\u20ac']) + with self.assertWarns(DeprecationWarning): + check('rU', ['A\n', 'B\n', 'C\n', 'D\u20ac']) + with self.assertWarns(DeprecationWarning): + check('U', ['A\n', 'B\n', 'C\n', 'D\u20ac']) with self.assertRaises(ValueError): check('rb', ['A\n', 'B\r\n', 'C\r', 'D\u20ac']) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index c27dfd96bc0..4a7cbe538cf 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -3900,6 +3900,16 @@ class MiscIOTest(unittest.TestCase): self.assertEqual(f.mode, "wb") f.close() + with support.check_warnings(('', DeprecationWarning)): + f = self.open(support.TESTFN, "U") + self.assertEqual(f.name, support.TESTFN) + self.assertEqual(f.buffer.name, support.TESTFN) + self.assertEqual(f.buffer.raw.name, support.TESTFN) + self.assertEqual(f.mode, "U") + self.assertEqual(f.buffer.mode, "rb") + self.assertEqual(f.buffer.raw.mode, "rb") + f.close() + f = self.open(support.TESTFN, "w+") self.assertEqual(f.mode, "w+") self.assertEqual(f.buffer.mode, "rb+") # Does it really matter? @@ -3913,13 +3923,6 @@ class MiscIOTest(unittest.TestCase): f.close() g.close() - def test_removed_u_mode(self): - # "U" mode has been removed in Python 3.9 - for mode in ("U", "rU", "r+U"): - with self.assertRaises(ValueError) as cm: - self.open(support.TESTFN, mode) - self.assertIn('invalid mode', str(cm.exception)) - def test_open_pipe_with_append(self): # bpo-27805: Ignore ESPIPE from lseek() in open(). r, w = os.pipe() |