[Python-checkins] bpo-31681: Make sure pkgutil.get_data closes files properly (#3875)

Éric Araujo webhook-mailer at python.org
Mon Oct 9 10:55:57 EDT 2017


https://github.com/python/cpython/commit/cfe1aefcbd2534ddc1059a7332842644e6c8d1e4
commit: cfe1aefcbd2534ddc1059a7332842644e6c8d1e4
branch: 2.7
author: Elvis Pranskevichus <elvis at magic.io>
committer: Éric Araujo <merwok at users.noreply.github.com>
date: 2017-10-09T10:55:54-04:00
summary:

bpo-31681: Make sure pkgutil.get_data closes files properly (#3875)

Also remove an obsolete note about
backward compat with old Pythons.

files:
A Misc/NEWS.d/next/Library/2017-10-03-15-41-08.bpo-31681.sOJMKV.rst
M Lib/pkgutil.py

diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index ce072ec9ef7..68ca72b0e40 100644
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -1,8 +1,5 @@
 """Utilities to support packages."""
 
-# NOTE: This module must remain compatible with Python 2.3, as it is shared
-# by setuptools for distribution with Python 2.3 and up.
-
 import os
 import sys
 import imp
@@ -252,7 +249,8 @@ def load_module(self, fullname):
         return mod
 
     def get_data(self, pathname):
-        return open(pathname, "rb").read()
+        with open(pathname, "rb") as file:
+            return file.read()
 
     def _reopen(self):
         if self.file and self.file.closed:
diff --git a/Misc/NEWS.d/next/Library/2017-10-03-15-41-08.bpo-31681.sOJMKV.rst b/Misc/NEWS.d/next/Library/2017-10-03-15-41-08.bpo-31681.sOJMKV.rst
new file mode 100644
index 00000000000..b6fc781dcfa
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-10-03-15-41-08.bpo-31681.sOJMKV.rst
@@ -0,0 +1 @@
+Fix pkgutil.get_data to avoid leaking open files.



More information about the Python-checkins mailing list