[pypy-svn] r5078 - pypy/trunk/src/pypy/appspace

arigo at codespeak.net arigo at codespeak.net
Sat Jun 12 17:54:42 CEST 2004


Author: arigo
Date: Sat Jun 12 17:54:42 2004
New Revision: 5078

Added:
   pypy/trunk/src/pypy/appspace/imp.py
Log:
Draft 'imp' module at app-level.


Added: pypy/trunk/src/pypy/appspace/imp.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/appspace/imp.py	Sat Jun 12 17:54:42 2004
@@ -0,0 +1,69 @@
+"""
+This module provides the components needed to build your own
+__import__ function.  Undocumented functions are obsolete.
+"""
+
+# XXX partial implementation
+
+# XXX to be reviewed
+
+import sys, os
+
+PY_SOURCE = 1
+PKG_DIRECTORY = 5
+C_BUILTIN = 6
+
+def get_suffixes():
+    return [('.py', 'U', PY_SOURCE)]
+
+new_module = type(sys)
+
+
+def find_module(name, path=None):
+    if path is None:
+        if name in sys.builtin_module_names:
+            return (None, name, ('', '', C_BUILTIN))
+        path = sys.path
+    for base in path:
+        filename = os.path.join(base, name)
+        if os.path.isdir(filename):
+            return (None, filename, ('', '', PKG_DIRECTORY))
+        filename += '.py'
+        if os.path.exists(filename):
+            return (file(filename, 'U'), filename, ('.py', 'U', PY_SOURCE))
+    raise ImportError, 'No module named %s' % (name,)
+
+
+def load_module(name, file, filename, description):
+    suffix, mode, type = description
+    module = sys.modules.get(name)
+
+    if type == PY_SOURCE:
+        source = file.read()
+        co = compile(source, filename, 'exec')
+        if module is None:
+            sys.modules[name] = module = new_module(name)
+        module.__dict__.clear()
+        module.__name__ = name
+        module.__doc__ = None
+        module.__file__ = filename
+        exec co in module.__dict__
+        return module
+
+    if type == PKG_DIRECTORY:
+        initfilename = os.path.join(filename, '__init__.py')
+        if module is None:
+            sys.modules[name] = module = new_module(name)
+        module.__dict__.clear()
+        module.__name__ = name
+        module.__doc__ = None
+        module.__file__ = initfilename
+        module.__path__ = [filename]
+        execfile(initfilename, module.__dict__)
+        return module
+
+    if type == C_BUILTIN:
+        module = __import__(name, {}, {}, None)
+        return module
+
+    raise ValueError, 'invalid description argument: %r' % (description,)



More information about the Pypy-commit mailing list