[Python-checkins] cpython (3.3): Issue #15902: Fix imp.load_module() to accept None as a file when

brett.cannon python-checkins at python.org
Fri May 3 16:47:28 CEST 2013


http://hg.python.org/cpython/rev/c0a21617dbee
changeset:   83594:c0a21617dbee
branch:      3.3
parent:      83592:65cd71abebc8
user:        Brett Cannon <brett at python.org>
date:        Fri May 03 10:37:08 2013 -0400
summary:
  Issue #15902: Fix imp.load_module() to accept None as a file when
trying to load an extension module.

While at it, also add a proper unittest.skipIf() guard to another test
involving imp.load_dynamic().

files:
  Lib/imp.py           |   8 ++++++--
  Lib/test/test_imp.py |  15 +++++++++++++++
  Misc/NEWS            |   3 +++
  3 files changed, 24 insertions(+), 2 deletions(-)


diff --git a/Lib/imp.py b/Lib/imp.py
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -168,7 +168,7 @@
         warnings.simplefilter('ignore')
         if mode and (not mode.startswith(('r', 'U')) or '+' in mode):
             raise ValueError('invalid file open mode {!r}'.format(mode))
-        elif file is None and type_ in {PY_SOURCE, PY_COMPILED, C_EXTENSION}:
+        elif file is None and type_ in {PY_SOURCE, PY_COMPILED}:
             msg = 'file object required for import (type code {})'.format(type_)
             raise ValueError(msg)
         elif type_ == PY_SOURCE:
@@ -176,7 +176,11 @@
         elif type_ == PY_COMPILED:
             return load_compiled(name, filename, file)
         elif type_ == C_EXTENSION and load_dynamic is not None:
-            return load_dynamic(name, filename, file)
+            if file is None:
+                with open(filename, 'rb') as opened_file:
+                    return load_dynamic(name, filename, opened_file)
+            else:
+                return load_dynamic(name, filename, file)
         elif type_ == PKG_DIRECTORY:
             return load_package(name, filename)
         elif type_ == C_BUILTIN:
diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py
--- a/Lib/test/test_imp.py
+++ b/Lib/test/test_imp.py
@@ -208,6 +208,8 @@
             self.assertIsNot(orig_getenv, new_os.getenv)
 
     @support.cpython_only
+    @unittest.skipIf(not hasattr(imp, 'load_dynamic'),
+                     'imp.load_dynamic() required')
     def test_issue15828_load_extensions(self):
         # Issue 15828 picked up that the adapter between the old imp API
         # and importlib couldn't handle C extensions
@@ -230,6 +232,19 @@
         self.assertIn(path, err.exception.path)
         self.assertEqual(name, err.exception.name)
 
+    @support.cpython_only
+    @unittest.skipIf(not hasattr(imp, 'load_dynamic'),
+                     'imp.load_dynamic() required')
+    def test_load_module_extension_file_is_None(self):
+        # When loading an extension module and the file is None, open one
+        # on the behalf of imp.load_dynamic().
+        # Issue #15902
+        name = '_heapq'
+        found = imp.find_module(name)
+        assert found[2][2] == imp.C_EXTENSION
+        found[0].close()
+        imp.load_module(name, None, *found[1:])
+
 
 class ReloadTests(unittest.TestCase):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -44,6 +44,9 @@
 Library
 -------
 
+- Issue #15902: Fix imp.load_module() accepting None as a file when loading an
+  extension module.
+
 - Issue #17802: Fix an UnboundLocalError in html.parser.  Initial tests by
   Thomas Barlow.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list