[pypy-svn] rev 2087 - in pypy/trunk/src/pypy: interpreter module

rocco at codespeak.net rocco at codespeak.net
Sun Oct 26 20:54:54 CET 2003


Author: rocco
Date: Sun Oct 26 20:54:53 2003
New Revision: 2087

Added:
   pypy/trunk/src/pypy/module/os_modules.py   (contents, props changed)
Modified:
   pypy/trunk/src/pypy/interpreter/baseobjspace.py
   pypy/trunk/src/pypy/module/__init__.py
Log:
Add facilities for dynamic loading of builtin modules.

Meta information about the modules provided by the pypy/modules
directory is now encoded in pypy.modules.__init__.

A loader function in BaseObjectSpace reads this information and 
dynamically loads the builtin modules. There are facilities to 
alter module loading based on object space and other things 
such as platform.

The sys and __builtin__ modules are special cased out of the loading 
proceedure.

Also included is a simple wrapper to load the platform specific mdules
which do the work of the os module.

Modified: pypy/trunk/src/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/trunk/src/pypy/interpreter/baseobjspace.py	Sun Oct 26 20:54:53 2003
@@ -1,6 +1,7 @@
 from pypy.interpreter.executioncontext import ExecutionContext
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.miscutils import Stack, getthreadlocals
+import pypy.module
 
 __all__ = ['ObjSpace', 'OperationError', 'NoValue']
 
@@ -55,6 +56,9 @@
 
         self.sys._setbuiltinmodule(self.w_builtin)
 
+        #Now we can load all the builtin (interpreter level) modules.
+        self.make_builtin_modules()
+
     def make_sys(self):
         assert not hasattr(self, 'sys')
         from pypy.module import sysmodule
@@ -62,6 +66,17 @@
         self.w_sys = self.wrap(self.sys)
         self.sys._setbuiltinmodule(self.w_sys)
 
+    def make_builtin_modules(self):
+        for filename, classname, spaces in pypy.module._builtin_modules:
+            if self.__class__.__name__ not in spaces:
+                continue
+            mod = __import__("pypy.module.%s"%filename, globals(), locals(),
+                             [classname])
+            klass = getattr(mod, classname)
+            module = klass(self)
+            if module is not None:
+                self.sys._setbuiltinmodule(self.wrap(module))
+        
     # XXX get rid of this. 
     def get_builtin_module(self, name):
         if name == '__builtin__':

Modified: pypy/trunk/src/pypy/module/__init__.py
==============================================================================
--- pypy/trunk/src/pypy/module/__init__.py	(original)
+++ pypy/trunk/src/pypy/module/__init__.py	Sun Oct 26 20:54:53 2003
@@ -0,0 +1,31 @@
+
+# ObjSpaces that implement the standard Python semantics
+_std_spaces = ['TrivialObjSpace','StdObjSpace']
+
+_all_spaces = _std_spaces + ['FlowObjSpace']
+
+# The following item ia a list of
+#   (filename, classname, [applicable spaces]) tuples describing the
+#   builtin modules (pypy.interpreter.extmodule.ExtModule)
+#   availible in the pypy.module directory. 
+
+# classname should be a classname of a callable in 'filename' that returns
+#   an unwrapped builtin module instance (pypy.interpreter.extmodule.ExtModule
+#   instance) - it may return an unwrapped None if the builtin module is not
+#   to be loaded. (e.g. this is the wrong platform.)
+
+#Note: the builtin and sys modules are special cased for bootstrapping reasons.
+
+# But this is an example of what the list would look like:
+
+##_builtin_and_sys = [('builtin','__builtin__',_std_spaces),
+##                    ('sysmodule','Sys',_std_spaces),
+##                    ]
+
+_builtin_modules = [('os_modules','Posix',_std_spaces),
+                    ('os_modules','Nt',_std_spaces),
+                    ('os_modules','Os2',_std_spaces),
+                    ('os_modules','Mac',_std_spaces),
+                    ('os_modules','Ce',_std_spaces),
+                    ('os_modules','Riscos',_std_spaces),
+                    ]

Added: pypy/trunk/src/pypy/module/os_modules.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/module/os_modules.py	Sun Oct 26 20:54:53 2003
@@ -0,0 +1,61 @@
+"""Bootstrap the builtin modules which make up sys:
+posix, nt, os2, mac, ce, etc.
+
+"""
+import sys
+import os
+
+from pypy.interpreter.extmodule import ExtModule
+
+_names = sys.builtin_module_names
+
+# We use the second (metaclassish) meaning of type to construct a subclass
+#   of ExtModule - can't modify some attributes (like __doc__) after class
+#   creation, and wrapping code does not properly look at instance variables.
+def Posix(space):
+    if 'posix' in _names:
+        import posix
+        _posix = type('posix', (ExtModule,), posix.__dict__)
+        return _posix(space)
+    else:
+        return None
+
+def Nt(space):
+    if 'nt' in _names:
+        import nt
+        _nt = type('nt', (ExtModule,), nt.__dict__)
+        return _nt(space)
+    else:
+        return None
+
+def Os2(space):
+    if 'os2' in _names:
+        import os2
+        _os2 = type('os2', (ExtModule,), os2.__dict__)
+        return _os2(space)
+    else:
+        return None
+
+def Mac(space):
+    if 'mac' in _names:
+        import mac
+        _mac = type('mac', (ExtModule,), mac.__dict__)
+        return _mac(space)
+    else:
+        return None
+
+def Ce(space):
+    if 'ce' in _names:
+        import ce
+        _ce = type('ce', (ExtModule,), ce.__dict__)
+        return _ce(space)
+    else:
+        return None
+
+def Riscos(space):
+    if 'riscos' in _names:
+        import riscos
+        _riscos = type('riscos', (ExtModule,), riscos.__dict__)
+        return _riscos(space)
+    else:
+        return None


More information about the Pypy-commit mailing list