[pypy-svn] r16736 - in pypy/release/0.7.x/pypy: interpreter module/posix module/recparser module/sys

pedronis at codespeak.net pedronis at codespeak.net
Sat Aug 27 12:56:28 CEST 2005


Author: pedronis
Date: Sat Aug 27 12:56:25 2005
New Revision: 16736

Modified:
   pypy/release/0.7.x/pypy/interpreter/baseobjspace.py
   pypy/release/0.7.x/pypy/interpreter/mixedmodule.py
   pypy/release/0.7.x/pypy/module/posix/__init__.py
   pypy/release/0.7.x/pypy/module/recparser/__init__.py
   pypy/release/0.7.x/pypy/module/sys/__init__.py
   pypy/release/0.7.x/pypy/module/sys/state.py
Log:
moved all the logic to install both mixed and faked modules to baseobjspace

now names like faked+_sre can be used in --usemodules option to force a faked module even if an app-level 
implementation is present under lib or a mixed module for the module is enabled by default.



Modified: pypy/release/0.7.x/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/release/0.7.x/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/release/0.7.x/pypy/interpreter/baseobjspace.py	Sat Aug 27 12:56:25 2005
@@ -145,16 +145,28 @@
     def __repr__(self):
         return self.__class__.__name__
 
-    def setbuiltinmodule(self, name, importname=None): 
+    def setbuiltinmodule(self, importname, installed_builtin_modules=[]): 
         """NOT_RPYTHON. load a lazy pypy/module and put it into sys.modules"""
-        if importname is None: 
-            importname = name 
-        Module = __import__("pypy.module.%s" % importname, 
+        import sys
+
+        fullname = "pypy.module.%s" % importname 
+
+        Module = __import__(fullname, 
                             None, None, ["Module"]).Module
+        if Module.applevel_name is not None:
+            name = Module.applevel_name
+        else:
+            name = importname
+
+        if name in installed_builtin_modules:
+            del sys.modules[fullname]
+            return None
+
         w_name = self.wrap(name) 
         w_mod = self.wrap(Module(self, w_name)) 
         w_modules = self.sys.get('modules')
         self.setitem(w_modules, w_name, w_mod) 
+        return name
 
     def getbuiltinmodule(self, name):
         w_name = self.wrap(name)
@@ -163,20 +175,14 @@
 
     # change this to influence which of our own
     # mixed modules should be used 
-    def get_builtinmodule_list(self):
+    def get_builtinmodule_to_install(self):
         """NOT_RPYTHON"""
         try:
             return self._builtinmodule_list
         except AttributeError:
             pass
 
-        modules = ['sys', '__builtin__', 'exceptions', 'unicodedata', '_codecs',
-            'array', 'marshal', 'errno', 'math']
-
-        if self.options.nofaking:
-            modules.append('posix')
-            modules.append('time')
-            modules.append('errno')
+        modules = ['sys', '__builtin__', 'exceptions']
 
         # there also are the '_sre' and 'marshal' modules 
         # but those currently cause translation problems.  You can
@@ -186,20 +192,37 @@
             if name not in modules: 
                 modules.append(name) 
 
-        # the returned builtinmodule_list contains tuples of
-        # names because some modules have a filesystem name 
-        # that differs from the app-visible name (because you 
-        # can specify implementation variants)
-        builtinmodule_list = [(x, None) for x in modules]
-        bml = builtinmodule_list
-        if ('posix', None) in bml:
-            bml[bml.index( ('posix', None) )] = (os.name, 'posix')
+        modules.extend(['unicodedata', '_codecs',
+                         'array', 'marshal', 'errno', 'math'])
+
+        if self.options.nofaking:
+            modules.append('posix')
+            modules.append('time')
+            modules.append('errno')
+
         if self.options.parser == "pypy":
-            builtinmodule_list.append(('parser', 'recparser'))
-            builtinmodule_list.append(('symbol', None))
-        self._builtinmodule_list = builtinmodule_list
+            modules.append('recparser')
+            modules.append('symbol')
+
+        import pypy
+        if not self.options.nofaking:
+            for modname in self.ALL_BUILTIN_MODULES:
+                if not (os.path.exists(
+                        os.path.join(os.path.dirname(pypy.__file__),
+                                     'lib', modname+'.py'))):            
+                    modules.append('faked+'+modname)
+
+        self._builtinmodule_list = modules
         return self._builtinmodule_list
 
+    ALL_BUILTIN_MODULES = [
+        'posix', 'nt', 'os2', 'mac', 'ce', 'riscos',
+        'math', 'array', 'select',
+        '_random', '_sre', 'time', '_socket', 'errno',
+        'unicodedata',
+        'parser', 'fcntl', '_codecs', 'binascii'
+    ]
+
     def make_builtins(self):
         "NOT_RPYTHON: only for initializing the space."
 
@@ -216,10 +239,8 @@
         self.setitem(w_modules, w_name, w_builtin) 
         self.setitem(self.builtin.w_dict, self.wrap('__builtins__'), w_builtin) 
 
-        for modname, mixedname in self.get_builtinmodule_list():
-            if modname not in ('sys', '__builtin__', 'exceptions'):##!!, 'marshal'):
-                self.setbuiltinmodule(modname, mixedname)
-        
+        installed_builtin_modules = ['sys', '__builtin__', 'exceptions'] # bootstrap ones
+
         # initialize with "bootstrap types" from objspace  (e.g. w_None)
         for name, value in self.__dict__.items():
             if name.startswith('w_') and not name.endswith('Type'): 
@@ -227,11 +248,55 @@
                 #print "setitem: space instance %-20s into builtins" % name
                 self.setitem(self.builtin.w_dict, self.wrap(name), value)
 
+        # install midex and faked modules and set builtin_module_names on sys
+        for mixedname in self.get_builtinmodule_to_install():
+            if not mixedname.startswith('faked+'):
+                self.install_mixemodule(mixedname, installed_builtin_modules)
+            else:
+                modname = mixedname[6:]
+                self.install_faked_module(modname, installed_builtin_modules)
+
+        installed_builtin_modules.sort()
+        w_builtin_module_names = self.newtuple(
+            [self.wrap(fn) for fn in installed_builtin_modules])
+
+        # force this value into the dict without unlazyfying everything
+        self.setitem(self.sys.w_dict, self.wrap('builtin_module_names'),
+                     w_builtin_module_names)
+
+    def install_mixemodule(self, mixedname, installed_builtin_modules):
+        """NOT_RPYTHON"""        
+        if mixedname not in installed_builtin_modules:
+            modname = self.setbuiltinmodule(mixedname, installed_builtin_modules)
+            if modname:
+                installed_builtin_modules.append(modname)
+
+    def load_cpython_module(self, modname):
+        "NOT_RPYTHON. Steal a module from CPython."
+        cpy_module = __import__(modname, {}, {}, ['*'])
+        return cpy_module
+
+    def install_faked_module(self, modname, installed_builtin_modules):
+        """NOT_RPYTHON"""
+        if modname in installed_builtin_modules:
+            return
+        try:
+            module = self.load_cpython_module(modname)
+        except ImportError:
+            return
+        else:
+            w_modules = self.sys.get('modules')
+            self.setitem(w_modules, self.wrap(modname), self.wrap(module))
+            installed_builtin_modules.append(modname)
+
     def setup_builtin_modules(self):
         "NOT_RPYTHON: only for initializing the space."
-        for modname, mixedname in self.get_builtinmodule_list():
+        from pypy.interpreter.module import Module
+        for w_modname in self.unpackiterable(self.sys.get('builtin_module_names')):
+            modname = self.unwrap(w_modname)
             mod = self.getbuiltinmodule(modname)
-            mod.setup_after_space_initialization()
+            if isinstance(mod, Module):
+                mod.setup_after_space_initialization()
 
     def initialize(self):
         """NOT_RPYTHON: Abstract method that should put some minimal

Modified: pypy/release/0.7.x/pypy/interpreter/mixedmodule.py
==============================================================================
--- pypy/release/0.7.x/pypy/interpreter/mixedmodule.py	(original)
+++ pypy/release/0.7.x/pypy/interpreter/mixedmodule.py	Sat Aug 27 12:56:25 2005
@@ -10,6 +10,8 @@
 class MixedModule(Module):
 
     NOT_RPYTHON_ATTRIBUTES = ['loaders']
+
+    applevel_name = None
     
     def __init__(self, space, w_name): 
         """ NOT_RPYTHON """ 

Modified: pypy/release/0.7.x/pypy/module/posix/__init__.py
==============================================================================
--- pypy/release/0.7.x/pypy/module/posix/__init__.py	(original)
+++ pypy/release/0.7.x/pypy/module/posix/__init__.py	Sat Aug 27 12:56:25 2005
@@ -10,6 +10,8 @@
 disguised Unix interface).  Refer to the library manual and
 corresponding Unix manual entries for more information on calls."""
 
+    applevel_name = os.name
+
     appleveldefs = {
     'error'      : 'app_posix.error',
     'stat_result': 'app_posix.stat_result',

Modified: pypy/release/0.7.x/pypy/module/recparser/__init__.py
==============================================================================
--- pypy/release/0.7.x/pypy/module/recparser/__init__.py	(original)
+++ pypy/release/0.7.x/pypy/module/recparser/__init__.py	Sat Aug 27 12:56:25 2005
@@ -12,6 +12,7 @@
      """The builtin parser module. 
      """ 
 
+     applevel_name = 'parser'
 
      appleveldefs = {
          'ParserError'  : 'app_class.ParserError',

Modified: pypy/release/0.7.x/pypy/module/sys/__init__.py
==============================================================================
--- pypy/release/0.7.x/pypy/module/sys/__init__.py	(original)
+++ pypy/release/0.7.x/pypy/module/sys/__init__.py	Sat Aug 27 12:56:25 2005
@@ -32,7 +32,7 @@
         'modules'               : 'state.get(space).w_modules', 
         'argv'                  : 'state.get(space).w_argv', 
         'warnoptions'           : 'state.get(space).w_warnoptions', 
-        'builtin_module_names'  : 'state.get(space).w_builtin_module_names', 
+        'builtin_module_names'  : 'state.w_None',
         'pypy_getudir'          : 'state.pypy_getudir', 
 
         '_getframe'             : 'vm._getframe', 

Modified: pypy/release/0.7.x/pypy/module/sys/state.py
==============================================================================
--- pypy/release/0.7.x/pypy/module/sys/state.py	(original)
+++ pypy/release/0.7.x/pypy/module/sys/state.py	Sat Aug 27 12:56:25 2005
@@ -6,61 +6,19 @@
 
 import sys, os 
 
-def load_cpython_module(modname):
-    "NOT_RPYTHON. Steal a module from CPython."
-    cpy_module = __import__(modname, globals(), locals(), None)
-    return cpy_module
-
 # ____________________________________________________________
 #
 
-ALL_BUILTIN_MODULES = [
-    'posix', 'nt', 'os2', 'mac', 'ce', 'riscos',
-    'math', 'array', 'select',
-    '_random', '_sre', 'time', '_socket', 'errno',
-    'unicodedata',
-     'parser', 'fcntl', '_codecs', 'binascii'
-]
-
 class State: 
     def __init__(self, space): 
         self.space = space 
 
         self.w_modules = space.newdict([])
-        self.complete_builtinmodules()
 
         self.w_warnoptions = space.newlist([])
         self.w_argv = space.newlist([])
         self.setinitialpath(space) 
 
-    def install_faked_module(self, modname):
-        space = self.space
-        try:
-            module = load_cpython_module(modname)
-        except ImportError:
-            return False
-        else:
-            space.setitem(self.w_modules, space.wrap(modname),
-                          space.wrap(module))
-            return True
-
-    def complete_builtinmodules(self):
-        space = self.space
-        builtinmodule_list = self.space.get_builtinmodule_list()
-        builtinmodule_names = [name for name, mixedname in builtinmodule_list]
-
-        if not space.options.nofaking:
-            for modname in ALL_BUILTIN_MODULES:
-                if modname not in builtinmodule_names:
-                    if not (os.path.exists(
-                            os.path.join(os.path.dirname(pypy.__file__),
-                            'lib', modname+'.py'))):
-                        if self.install_faked_module(modname):
-                             builtinmodule_names.append(modname)
-        builtinmodule_names.sort()
-        self.w_builtin_module_names = space.newtuple(
-            [space.wrap(fn) for fn in builtinmodule_names])
-
     def setinitialpath(self, space): 
         # Initialize the default path
         from pypy.interpreter import autopath



More information about the Pypy-commit mailing list