]> granicus.if.org Git - python/commitdiff
bpo-31681: Make sure pkgutil.get_data closes files properly (#3875)
authorElvis Pranskevichus <elvis@magic.io>
Mon, 9 Oct 2017 14:55:54 +0000 (10:55 -0400)
committerÉric Araujo <merwok@users.noreply.github.com>
Mon, 9 Oct 2017 14:55:54 +0000 (10:55 -0400)
Also remove an obsolete note about
backward compat with old Pythons.

Lib/pkgutil.py
Misc/NEWS.d/next/Library/2017-10-03-15-41-08.bpo-31681.sOJMKV.rst [new file with mode: 0644]

index ce072ec9ef75dcc1eede38e8dc870ae4aac417fe..68ca72b0e40fd718982886962a3c36f1d1d0adc1 100644 (file)
@@ -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 @@ class ImpLoader:
         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 (file)
index 0000000..b6fc781
--- /dev/null
@@ -0,0 +1 @@
+Fix pkgutil.get_data to avoid leaking open files.