Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2480c2e

Browse files
Issue #15204: Silence and check the 'U' mode deprecation warnings in tests.
Changed deprecation message in the fileinput module.
1 parent ed8c906 commit 2480c2e

5 files changed

Lines changed: 33 additions & 13 deletions

File tree

Lib/fileinput.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def __init__(self, files=None, inplace=False, backup="", bufsize=0,
224224
"'r', 'rU', 'U' and 'rb'")
225225
if 'U' in mode:
226226
import warnings
227-
warnings.warn("Use of 'U' mode is deprecated",
227+
warnings.warn("'U' mode is deprecated",
228228
DeprecationWarning, 2)
229229
self._mode = mode
230230
if openhook:

Lib/test/test_codecs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,9 @@ def test_bug691291(self):
602602
self.addCleanup(support.unlink, support.TESTFN)
603603
with open(support.TESTFN, 'wb') as fp:
604604
fp.write(s)
605-
with codecs.open(support.TESTFN, 'U', encoding=self.encoding) as reader:
605+
with support.check_warnings(('', DeprecationWarning)):
606+
reader = codecs.open(support.TESTFN, 'U', encoding=self.encoding)
607+
with reader:
606608
self.assertEqual(reader.read(), s1)
607609

608610
class UTF16LETest(ReadTest, unittest.TestCase):

Lib/test/test_fileinput.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from io import StringIO
2323
from fileinput import FileInput, hook_encoded
2424

25-
from test.support import verbose, TESTFN, run_unittest
25+
from test.support import verbose, TESTFN, run_unittest, check_warnings
2626
from test.support import unlink as safe_unlink
2727

2828

@@ -224,8 +224,10 @@ def test_opening_mode(self):
224224
try:
225225
# try opening in universal newline mode
226226
t1 = writeTmp(1, [b"A\nB\r\nC\rD"], mode="wb")
227-
fi = FileInput(files=t1, mode="U")
228-
lines = list(fi)
227+
with check_warnings(('', DeprecationWarning)):
228+
fi = FileInput(files=t1, mode="U")
229+
with check_warnings(('', DeprecationWarning)):
230+
lines = list(fi)
229231
self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
230232
finally:
231233
remove_tempfiles(t1)

Lib/test/test_io.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2777,7 +2777,8 @@ def test_attributes(self):
27772777
self.assertEqual(f.mode, "wb")
27782778
f.close()
27792779

2780-
f = self.open(support.TESTFN, "U")
2780+
with support.check_warnings(('', DeprecationWarning)):
2781+
f = self.open(support.TESTFN, "U")
27812782
self.assertEqual(f.name, support.TESTFN)
27822783
self.assertEqual(f.buffer.name, support.TESTFN)
27832784
self.assertEqual(f.buffer.raw.name, support.TESTFN)

Lib/test/test_zipfile.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from test.support import (TESTFN, findfile, unlink,
1616
requires_zlib, requires_bz2, requires_lzma,
17-
captured_stdout)
17+
captured_stdout, check_warnings)
1818

1919
TESTFN2 = TESTFN + "2"
2020
TESTFNDIR = TESTFN + "d"
@@ -35,6 +35,10 @@ def get_files(test):
3535
yield f
3636
test.assertFalse(f.closed)
3737

38+
def openU(zipfp, fn):
39+
with check_warnings(('', DeprecationWarning)):
40+
return zipfp.open(fn, 'rU')
41+
3842
class AbstractTestsWithSourceFile:
3943
@classmethod
4044
def setUpClass(cls):
@@ -875,6 +879,17 @@ def test_open_via_zip_info(self):
875879
data += zipfp.read(info)
876880
self.assertIn(data, {b"foobar", b"barfoo"})
877881

882+
def test_universal_deprecation(self):
883+
f = io.BytesIO()
884+
with zipfile.ZipFile(f, "w") as zipfp:
885+
zipfp.writestr('spam.txt', b'ababagalamaga')
886+
887+
with zipfile.ZipFile(f, "r") as zipfp:
888+
for mode in 'U', 'rU':
889+
with self.assertWarns(DeprecationWarning):
890+
zipopen = zipfp.open('spam.txt', mode)
891+
zipopen.close()
892+
878893
def test_universal_readaheads(self):
879894
f = io.BytesIO()
880895

@@ -884,7 +899,7 @@ def test_universal_readaheads(self):
884899

885900
data2 = b''
886901
with zipfile.ZipFile(f, 'r') as zipfp, \
887-
zipfp.open(TESTFN, 'rU') as zipopen:
902+
openU(zipfp, TESTFN) as zipopen:
888903
for line in zipopen:
889904
data2 += line
890905

@@ -1613,7 +1628,7 @@ def read_test(self, f, compression):
16131628
# Read the ZIP archive
16141629
with zipfile.ZipFile(f, "r") as zipfp:
16151630
for sep, fn in self.arcfiles.items():
1616-
with zipfp.open(fn, "rU") as fp:
1631+
with openU(zipfp, fn) as fp:
16171632
zipdata = fp.read()
16181633
self.assertEqual(self.arcdata[sep], zipdata)
16191634

@@ -1627,7 +1642,7 @@ def readline_read_test(self, f, compression):
16271642
# Read the ZIP archive
16281643
with zipfile.ZipFile(f, "r") as zipfp:
16291644
for sep, fn in self.arcfiles.items():
1630-
with zipfp.open(fn, "rU") as zipopen:
1645+
with openU(zipfp, fn) as zipopen:
16311646
data = b''
16321647
while True:
16331648
read = zipopen.readline()
@@ -1652,7 +1667,7 @@ def readline_test(self, f, compression):
16521667
# Read the ZIP archive
16531668
with zipfile.ZipFile(f, "r") as zipfp:
16541669
for sep, fn in self.arcfiles.items():
1655-
with zipfp.open(fn, "rU") as zipopen:
1670+
with openU(zipfp, fn) as zipopen:
16561671
for line in self.line_gen:
16571672
linedata = zipopen.readline()
16581673
self.assertEqual(linedata, line + b'\n')
@@ -1667,7 +1682,7 @@ def readlines_test(self, f, compression):
16671682
# Read the ZIP archive
16681683
with zipfile.ZipFile(f, "r") as zipfp:
16691684
for sep, fn in self.arcfiles.items():
1670-
with zipfp.open(fn, "rU") as fp:
1685+
with openU(zipfp, fn) as fp:
16711686
ziplines = fp.readlines()
16721687
for line, zipline in zip(self.line_gen, ziplines):
16731688
self.assertEqual(zipline, line + b'\n')
@@ -1682,7 +1697,7 @@ def iterlines_test(self, f, compression):
16821697
# Read the ZIP archive
16831698
with zipfile.ZipFile(f, "r") as zipfp:
16841699
for sep, fn in self.arcfiles.items():
1685-
with zipfp.open(fn, "rU") as fp:
1700+
with openU(zipfp, fn) as fp:
16861701
for line, zipline in zip(self.line_gen, fp):
16871702
self.assertEqual(zipline, line + b'\n')
16881703

0 commit comments

Comments
 (0)