Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b23413

Browse files
author
Greg Stein
committed
add loading of dynamic library modules.
1 parent 645af9f commit 2b23413

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

Lib/imputil.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ def get_code(self, parent, modname, fqname):
333333
# byte-compiled file suffix
334334
_suffix = '.py' + _suffix_char
335335

336+
# the C_EXTENSION suffixes
337+
_c_suffixes = filter(lambda x: x[2] == imp.C_EXTENSION, imp.get_suffixes())
336338

337339
def _compile(pathname, timestamp):
338340
"""Compile (and cache) a Python source file.
@@ -429,7 +431,7 @@ def _timestamp(pathname):
429431
return None
430432
return long(s[8])
431433

432-
def _fs_import(dir, modname):
434+
def _fs_import(dir, modname, fqname):
433435
"Fetch a module from the filesystem."
434436

435437
pathname = _os_path_join(dir, modname)
@@ -441,6 +443,18 @@ def _fs_import(dir, modname):
441443
values = { }
442444
ispkg = 0
443445

446+
# look for dynload modules
447+
for desc in _c_suffixes:
448+
file = pathname + desc[0]
449+
try:
450+
fp = open(file, desc[1])
451+
except IOError:
452+
pass
453+
else:
454+
module = imp.load_module(fqname, fp, file, desc)
455+
values['__file__'] = file
456+
return 0, module, values
457+
444458
t_py = _timestamp(pathname + '.py')
445459
t_pyc = _timestamp(pathname + _suffix)
446460
if t_py is None and t_pyc is None:
@@ -578,7 +592,7 @@ def get_code(self, parent, modname, fqname):
578592

579593
# Return the module (and other info) if found in the specified
580594
# directory. Otherwise, return None.
581-
return _fs_import(dir, modname)
595+
return _fs_import(dir, modname, fqname)
582596

583597
def __repr__(self):
584598
return '<%s.%s for "%s" at 0x%x>' % (self.__class__.__module__,
@@ -602,11 +616,11 @@ def __init__(self, path=sys.path):
602616
def get_code(self, parent, modname, fqname):
603617
if parent:
604618
# we are looking for a module inside of a specific package
605-
return _fs_import(parent.__pkgdir__, modname)
619+
return _fs_import(parent.__pkgdir__, modname, fqname)
606620

607621
# scan sys.path, looking for the requested module
608622
for dir in self.path:
609-
result = _fs_import(dir, modname)
623+
result = _fs_import(dir, modname, fqname)
610624
if result:
611625
return result
612626

0 commit comments

Comments
 (0)