##// END OF EJS Templates
Merge pull request #11792 from Carreau/textiowrapperleak...
Matthias Bussonnier -
r25107:4f349e0f merge
parent child Browse files
Show More
@@ -62,9 +62,8 b' def test_parser():'
62 62
63 63 def test_temp_pyfile():
64 64 src = 'pass\n'
65 fname, fh = tt.temp_pyfile(src)
65 fname = tt.temp_pyfile(src)
66 66 assert os.path.isfile(fname)
67 fh.close()
68 67 with open(fname) as fh2:
69 68 src2 = fh2.read()
70 69 nt.assert_equal(src2, src)
@@ -269,20 +269,19 b' class TempFileMixin(object):'
269 269
270 270 def mktmp(self, src, ext='.py'):
271 271 """Make a valid python temp file."""
272 fname, f = temp_pyfile(src, ext)
272 fname = temp_pyfile(src, ext)
273 273 if not hasattr(self, 'tmps'):
274 274 self.tmps=[]
275 self.tmps.append((f, fname))
275 self.tmps.append(fname)
276 276 self.fname = fname
277 277
278 278 def tearDown(self):
279 279 # If the tmpfile wasn't made because of skipped tests, like in
280 280 # win32, there's nothing to cleanup.
281 281 if hasattr(self, 'tmps'):
282 for f,fname in self.tmps:
282 for fname in self.tmps:
283 283 # If the tmpfile wasn't made because of skipped tests, like in
284 284 # win32, there's nothing to cleanup.
285 f.close()
286 285 try:
287 286 os.unlink(fname)
288 287 except:
@@ -205,10 +205,10 b" def temp_pyfile(src, ext='.py'):"
205 205 It is the caller's responsibility to close the open file and unlink it.
206 206 """
207 207 fname = tempfile.mkstemp(ext)[1]
208 f = open(fname,'w')
209 f.write(src)
210 f.flush()
211 return fname, f
208 with open(fname,'w') as f:
209 f.write(src)
210 f.flush()
211 return fname
212 212
213 213 @undoc
214 214 def atomic_writing(*args, **kwargs):
@@ -32,12 +32,12 b" def source_to_unicode(txt, errors='replace', skip_encoding_cookie=True):"
32 32 except SyntaxError:
33 33 encoding = "ascii"
34 34 buffer.seek(0)
35 text = TextIOWrapper(buffer, encoding, errors=errors, line_buffering=True)
36 text.mode = 'r'
37 if skip_encoding_cookie:
38 return u"".join(strip_encoding_cookie(text))
39 else:
40 return text.read()
35 with TextIOWrapper(buffer, encoding, errors=errors, line_buffering=True) as text:
36 text.mode = 'r'
37 if skip_encoding_cookie:
38 return u"".join(strip_encoding_cookie(text))
39 else:
40 return text.read()
41 41
42 42 def strip_encoding_cookie(filelike):
43 43 """Generator to pull lines from a text-mode file, skipping the encoding
@@ -5,27 +5,35 b' import nose.tools as nt'
5 5 from IPython.utils import openpy
6 6
7 7 mydir = os.path.dirname(__file__)
8 nonascii_path = os.path.join(mydir, '../../core/tests/nonascii.py')
8 nonascii_path = os.path.join(mydir, "../../core/tests/nonascii.py")
9
9 10
10 11 def test_detect_encoding():
11 with open(nonascii_path, 'rb') as f:
12 with open(nonascii_path, "rb") as f:
12 13 enc, lines = openpy.detect_encoding(f.readline)
13 nt.assert_equal(enc, 'iso-8859-5')
14 nt.assert_equal(enc, "iso-8859-5")
15
14 16
15 17 def test_read_file():
16 read_specified_enc = io.open(nonascii_path, encoding='iso-8859-5').read()
18 with io.open(nonascii_path, encoding="iso-8859-5") as f:
19 read_specified_enc = f.read()
17 20 read_detected_enc = openpy.read_py_file(nonascii_path, skip_encoding_cookie=False)
18 21 nt.assert_equal(read_detected_enc, read_specified_enc)
19 assert u'coding: iso-8859-5' in read_detected_enc
20
21 read_strip_enc_cookie = openpy.read_py_file(nonascii_path, skip_encoding_cookie=True)
22 assert u'coding: iso-8859-5' not in read_strip_enc_cookie
22 assert "coding: iso-8859-5" in read_detected_enc
23
24 read_strip_enc_cookie = openpy.read_py_file(
25 nonascii_path, skip_encoding_cookie=True
26 )
27 assert "coding: iso-8859-5" not in read_strip_enc_cookie
28
23 29
24 30 def test_source_to_unicode():
25 with io.open(nonascii_path, 'rb') as f:
31 with io.open(nonascii_path, "rb") as f:
26 32 source_bytes = f.read()
27 nt.assert_equal(openpy.source_to_unicode(source_bytes, skip_encoding_cookie=False).splitlines(),
28 source_bytes.decode('iso-8859-5').splitlines())
33 nt.assert_equal(
34 openpy.source_to_unicode(source_bytes, skip_encoding_cookie=False).splitlines(),
35 source_bytes.decode("iso-8859-5").splitlines(),
36 )
29 37
30 38 source_no_cookie = openpy.source_to_unicode(source_bytes, skip_encoding_cookie=True)
31 nt.assert_not_in(u'coding: iso-8859-5', source_no_cookie)
39 nt.assert_not_in("coding: iso-8859-5", source_no_cookie)
General Comments 0
You need to be logged in to leave comments. Login now