diff --git a/IPython/testing/tests/test_tools.py b/IPython/testing/tests/test_tools.py index 4fa5c96..2a6b9e4 100644 --- a/IPython/testing/tests/test_tools.py +++ b/IPython/testing/tests/test_tools.py @@ -62,9 +62,8 @@ def test_parser(): def test_temp_pyfile(): src = 'pass\n' - fname, fh = tt.temp_pyfile(src) + fname = tt.temp_pyfile(src) assert os.path.isfile(fname) - fh.close() with open(fname) as fh2: src2 = fh2.read() nt.assert_equal(src2, src) diff --git a/IPython/testing/tools.py b/IPython/testing/tools.py index 1d16f11..b2d7df4 100644 --- a/IPython/testing/tools.py +++ b/IPython/testing/tools.py @@ -269,20 +269,19 @@ class TempFileMixin(object): def mktmp(self, src, ext='.py'): """Make a valid python temp file.""" - fname, f = temp_pyfile(src, ext) + fname = temp_pyfile(src, ext) if not hasattr(self, 'tmps'): self.tmps=[] - self.tmps.append((f, fname)) + self.tmps.append(fname) self.fname = fname def tearDown(self): # If the tmpfile wasn't made because of skipped tests, like in # win32, there's nothing to cleanup. if hasattr(self, 'tmps'): - for f,fname in self.tmps: + for fname in self.tmps: # If the tmpfile wasn't made because of skipped tests, like in # win32, there's nothing to cleanup. - f.close() try: os.unlink(fname) except: diff --git a/IPython/utils/io.py b/IPython/utils/io.py index b59a1a1..fab9bae 100644 --- a/IPython/utils/io.py +++ b/IPython/utils/io.py @@ -205,10 +205,10 @@ def temp_pyfile(src, ext='.py'): It is the caller's responsibility to close the open file and unlink it. """ fname = tempfile.mkstemp(ext)[1] - f = open(fname,'w') - f.write(src) - f.flush() - return fname, f + with open(fname,'w') as f: + f.write(src) + f.flush() + return fname @undoc def atomic_writing(*args, **kwargs): diff --git a/IPython/utils/openpy.py b/IPython/utils/openpy.py index d544f41..3046c31 100644 --- a/IPython/utils/openpy.py +++ b/IPython/utils/openpy.py @@ -32,12 +32,12 @@ def source_to_unicode(txt, errors='replace', skip_encoding_cookie=True): except SyntaxError: encoding = "ascii" buffer.seek(0) - text = TextIOWrapper(buffer, encoding, errors=errors, line_buffering=True) - text.mode = 'r' - if skip_encoding_cookie: - return u"".join(strip_encoding_cookie(text)) - else: - return text.read() + with TextIOWrapper(buffer, encoding, errors=errors, line_buffering=True) as text: + text.mode = 'r' + if skip_encoding_cookie: + return u"".join(strip_encoding_cookie(text)) + else: + return text.read() def strip_encoding_cookie(filelike): """Generator to pull lines from a text-mode file, skipping the encoding diff --git a/IPython/utils/tests/test_openpy.py b/IPython/utils/tests/test_openpy.py index 5a01ac4..9e66d43 100644 --- a/IPython/utils/tests/test_openpy.py +++ b/IPython/utils/tests/test_openpy.py @@ -5,27 +5,35 @@ import nose.tools as nt from IPython.utils import openpy mydir = os.path.dirname(__file__) -nonascii_path = os.path.join(mydir, '../../core/tests/nonascii.py') +nonascii_path = os.path.join(mydir, "../../core/tests/nonascii.py") + def test_detect_encoding(): - with open(nonascii_path, 'rb') as f: + with open(nonascii_path, "rb") as f: enc, lines = openpy.detect_encoding(f.readline) - nt.assert_equal(enc, 'iso-8859-5') + nt.assert_equal(enc, "iso-8859-5") + def test_read_file(): - read_specified_enc = io.open(nonascii_path, encoding='iso-8859-5').read() + with io.open(nonascii_path, encoding="iso-8859-5") as f: + read_specified_enc = f.read() read_detected_enc = openpy.read_py_file(nonascii_path, skip_encoding_cookie=False) nt.assert_equal(read_detected_enc, read_specified_enc) - assert u'coding: iso-8859-5' in read_detected_enc - - read_strip_enc_cookie = openpy.read_py_file(nonascii_path, skip_encoding_cookie=True) - assert u'coding: iso-8859-5' not in read_strip_enc_cookie + assert "coding: iso-8859-5" in read_detected_enc + + read_strip_enc_cookie = openpy.read_py_file( + nonascii_path, skip_encoding_cookie=True + ) + assert "coding: iso-8859-5" not in read_strip_enc_cookie + def test_source_to_unicode(): - with io.open(nonascii_path, 'rb') as f: + with io.open(nonascii_path, "rb") as f: source_bytes = f.read() - nt.assert_equal(openpy.source_to_unicode(source_bytes, skip_encoding_cookie=False).splitlines(), - source_bytes.decode('iso-8859-5').splitlines()) + nt.assert_equal( + openpy.source_to_unicode(source_bytes, skip_encoding_cookie=False).splitlines(), + source_bytes.decode("iso-8859-5").splitlines(), + ) source_no_cookie = openpy.source_to_unicode(source_bytes, skip_encoding_cookie=True) - nt.assert_not_in(u'coding: iso-8859-5', source_no_cookie) + nt.assert_not_in("coding: iso-8859-5", source_no_cookie)