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