summaryrefslogtreecommitdiff
path: root/pkg_resources
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2017-09-16 11:23:22 -0700
committerJon Dufresne <jon.dufresne@gmail.com>2017-09-16 12:36:34 -0700
commitb1e47b43f3d2b972057ebe4c564b0ed03d39cd3d (patch)
treecbb27e7459cdf542a021ceecde51eab2a51a36ac /pkg_resources
parent9f295a706590b1e3618978d6f2d83af0b893ec4d (diff)
downloadpython-setuptools-git-b1e47b43f3d2b972057ebe4c564b0ed03d39cd3d.tar.gz
Clean up resource warnings during tests
When tests are invoked with the Python flag -Wall, warnings appear in the form of: ResourceWarning: unclosed file ... Close all files and resources deterministically to avoid such warnings. Most often, easiest to do using a context manager.
Diffstat (limited to 'pkg_resources')
-rw-r--r--pkg_resources/__init__.py6
-rw-r--r--pkg_resources/tests/test_pkg_resources.py4
2 files changed, 7 insertions, 3 deletions
diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py
index 68349df4..049b8a54 100644
--- a/pkg_resources/__init__.py
+++ b/pkg_resources/__init__.py
@@ -2124,7 +2124,11 @@ def non_empty_lines(path):
"""
Yield non-empty lines from file at path
"""
- return (line.rstrip() for line in open(path) if line.strip())
+ with open(path) as f:
+ for line in f:
+ line = line.strip()
+ if line:
+ yield line
def resolve_egg_link(path):
diff --git a/pkg_resources/tests/test_pkg_resources.py b/pkg_resources/tests/test_pkg_resources.py
index 49bf7a04..c6a7ac97 100644
--- a/pkg_resources/tests/test_pkg_resources.py
+++ b/pkg_resources/tests/test_pkg_resources.py
@@ -92,8 +92,8 @@ class TestZipProvider(object):
ts = timestamp(self.ref_time)
os.utime(filename, (ts, ts))
filename = zp.get_resource_filename(manager, 'data.dat')
- f = open(filename)
- assert f.read() == 'hello, world!'
+ with open(filename) as f:
+ assert f.read() == 'hello, world!'
manager.cleanup_resources()