summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Laxalde <denis.laxalde@logilab.fr>2017-07-04 09:25:01 +0200
committerDenis Laxalde <denis.laxalde@logilab.fr>2017-07-04 09:25:01 +0200
commitefe27c4a0250b0ac3726e64813e5ff157e51e814 (patch)
tree77ff4ee8e817e8e0957f65fa917ac48e9452c116
parent6a832d17a3555c652d5530625a40f73c69441720 (diff)
downloadlogilab-common-efe27c4a0250b0ac3726e64813e5ff157e51e814.tar.gz
Use a context manager for io.open()
-rw-r--r--logilab/common/fileutils.py6
-rw-r--r--test/unittest_fileutils.py3
2 files changed, 4 insertions, 5 deletions
diff --git a/logilab/common/fileutils.py b/logilab/common/fileutils.py
index 48828fb..93439d3 100644
--- a/logilab/common/fileutils.py
+++ b/logilab/common/fileutils.py
@@ -283,10 +283,8 @@ def lines(path, comments=None):
:warning: at some point this function will probably return an iterator
"""
- stream = io.open(path)
- result = stream_lines(stream, comments)
- stream.close()
- return result
+ with io.open(path) as stream:
+ return stream_lines(stream, comments)
def stream_lines(stream, comments=None):
diff --git a/test/unittest_fileutils.py b/test/unittest_fileutils.py
index a43b60f..555e73f 100644
--- a/test/unittest_fileutils.py
+++ b/test/unittest_fileutils.py
@@ -55,7 +55,8 @@ class GetModeTC(TestCase):
class NormReadTC(TestCase):
def test_known_values_norm_read(self):
- data = io.open(NEWLINES_TXT).read()
+ with io.open(NEWLINES_TXT) as f:
+ data = f.read()
self.assertEqual(data.strip(), '\n'.join(['# mixed new lines', '1', '2', '3']))