[Python-checkins] r55952 - sandbox/trunk/import_in_py/pseudocode.py

brett.cannon python-checkins at python.org
Wed Jun 13 06:53:25 CEST 2007


Author: brett.cannon
Date: Wed Jun 13 06:53:24 2007
New Revision: 55952

Modified:
   sandbox/trunk/import_in_py/pseudocode.py
Log:
Add pseudocode for how import looks for extension, Python source, and Python
bytecode files.


Modified: sandbox/trunk/import_in_py/pseudocode.py
==============================================================================
--- sandbox/trunk/import_in_py/pseudocode.py	(original)
+++ sandbox/trunk/import_in_py/pseudocode.py	Wed Jun 13 06:53:24 2007
@@ -127,8 +127,40 @@
         return module
 
 
+from imp import get_suffixes, C_EXTENSION
 def find_extension_module(name, path_entry):
-    pass
+    """Try to locate a C extension module for the requested module."""
+    # Get the immediate name of the module being searched for as the extension
+    # module's file name will be based on it.
+    immediate_name = name.rsplit('.', 1)[-1]
+    # Check every possible C extension suffix with the immediate module name
+    # (typically two; '.so' and 'module.so').
+    for suffix in (suffix[0] for suffix in get_suffixes()
+            if suffix[2] == C_EXTENSION):
+        file_path = os.path.join(path_entry, immediate_name + suffix)
+        if os.path.isfile(file_path):  # I/O
+            return extension_loader(name, file_path)
+    else:
+        return None
 
+
+from imp import PY_SOURCE, PY_COMPILED
 def find_py_pyc_module(name, path_entry):
-    pass
+    """Try to locate a Python source code or bytecode module for the requested
+    module."""
+    # Get the immediate name of the module being imported as the Python file's
+    # name will be based on it.
+    immediate_name = name.rsplit('.', 1)[-1]
+    # Check every valid Python code suffix for possible files (typically two;
+    # '.py' and either '.pyc' or '.pyo').
+    for suffix in (suffix[0] for suffix in get_suffixes()
+            if suffix[2] in (PY_SOURCE, PY_COMPILED)):
+        # See if the module is actually a package.
+        pkg_init_path = os.path.join(path_entry, immediate_name,
+                                        '__init__' + suffix)
+        if os.path.isfile(pkg_init_path):  # I/O
+            return package_loader(name, pkg_init_path)
+        # If module is not a package, see if it is a file by itself.
+        file_path = os.path.join(path_entry, immediate_name + suffix)
+        if os.path.isfile(file_path):  # I/O
+            return py_loader(name, file_path)


More information about the Python-checkins mailing list