From 9d0f772c5115217d55aef76fae33f483f2a98032 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 3 May 2013 10:37:08 -0400 Subject: 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(). --- Lib/imp.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'Lib/imp.py') diff --git a/Lib/imp.py b/Lib/imp.py index 6d156d3115..40869f5ac0 100644 --- a/Lib/imp.py +++ b/Lib/imp.py @@ -168,7 +168,7 @@ def load_module(name, file, filename, details): 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 @@ def load_module(name, file, filename, details): 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: -- cgit v1.2.1