[pypy-svn] r49454 - in pypy/branch/clr-module-improvements/pypy/module/clr: . test

regmee at codespeak.net regmee at codespeak.net
Thu Dec 6 19:52:50 CET 2007


Author: regmee
Date: Thu Dec  6 19:52:49 2007
New Revision: 49454

Modified:
   pypy/branch/clr-module-improvements/pypy/module/clr/app_importer.py
   pypy/branch/clr-module-improvements/pypy/module/clr/test/test_importer.py
Log:
import hooks implemented. More test cases added. Hardcoded .NET modules names for the check to be resolved using reflection

Modified: pypy/branch/clr-module-improvements/pypy/module/clr/app_importer.py
==============================================================================
--- pypy/branch/clr-module-improvements/pypy/module/clr/app_importer.py	(original)
+++ pypy/branch/clr-module-improvements/pypy/module/clr/app_importer.py	Thu Dec  6 19:52:49 2007
@@ -1,66 +1,117 @@
 
-""" Importer class 
-    # Meta hooks are called at the start of Import Processing
-    # Meta hooks can override the sys.path, frozen modules , built-in modules
-    # to register a Meta Hook simply add importer object to sys.meta_path
-    # a path hook is registered by adding an Importer factory to sys.path_hooks
-    # sys.path_hooks  is a list of Class of the HOOK.
-    # whose __init__ is called when the calleable in the list is obtained.
-    # __init__ cant return anything so some __new__ method should return
-    This is used to enable the "import module" mechanism for .NET classes"""
+#     Meta hooks are called at the start of Import Processing
+#     Meta hooks can override the sys.path, frozen modules , built-in modules
+#     To register a Meta Hook simply add importer object to sys.meta_path
 
 import imp
 import sys
-        
+
+DotNetModuleList = ['System',
+                    'System.Collections',
+                    'System.Collections.ArrayList',
+                    'System.Collections.Stack',
+                    'System.Collections.Queue',
+                    'System.Math']
+
+
 class loader(object):
+    '''
+        This method returns the loaded module or raises an exception, ImportError
+        loader is ONLY calld for proper .NET modules (check done in Importer class
+    '''
     def __init__(self):
-        self.Names = [] 
+        pass
 
     def load_module(self, fullname):
-       
-        # Now since the module was not found .. Call the Loader and load it.
+        '''
+            The load_module() must fulfill the following *before* it runs any code:
+            Note that the module object *must* be in sys.modules before the
+            loader executes the module code.  
+
+          A  If 'fullname' exists in sys.modules, the loader must use that
+             else the loader must create a new module object and add it to sys.modules.
+
+                module = sys.modules.setdefault(fullname, new.module(fullname))
+
+          B  The __file__ attribute must be set.  String say "<frozen>"
+
+          C  The __name__ attribute must be set.  If one uses
+              imp.new_module() then the attribute is set automatically.
+
+          D  If it's a package, the __path__ variable must be set.  This must
+              be a list, but may be empty if __path__ has no further
+              significance to the importer (more on this later).
+
+          E  It should add a __loader__ attribute to the module, set to the loader object. 
+
+        '''
+        # If it is a call for a Class then return with the Class reference
+        code = 0 
         if fullname == "System.Math":
+            code = 1
             import clr
-            return clr.load_cli_class('System','Math')
-
+            sys.modules[fullname] = clr.load_cli_class('System','Math')
+        if fullname == "System.Collections.Queue":
+            code = 1
+            import clr
+            sys.modules[fullname] = clr.load_cli_class('System.Collections','Queue')
+        if fullname == "System.Collections.Stack":
+            code = 1
+            import clr
+            sys.modules[fullname] = clr.load_cli_class('System.Collections','Stack')
         if fullname == "System.Collections.ArrayList":
+            code = 1
             import clr
-            return clr.load_cli_class('System.Collections','ArrayList')
+            sys.modules[fullname] = clr.load_cli_class('System.Collections','ArrayList')
 
-        # Now create a new module and append it at the end of the sys.modules list
-        mod = imp.new_module(fullname)
-        mod.__file__ = "<%s>" % self.__class__.__name__
-        mod.__loader__ = self
-        mod.__name__ = fullname
-        #if ispkg:
-        #if :
-        #    mod.__path__ = []
-        #exec code in mod.__dict__'''
+        # if not a call for actual class assign an empty module for it. 
+        if not code:
+            mod = imp.new_module(fullname)
+            mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
+            mod.__file__ = "<%s>" % self.__class__.__name__
+            mod.__loader__ = self
+            mod.__name__ = fullname
 
-        # add it to the modules list
-        sys.modules[fullname] = mod
+            # if it is a PACKAGE then we are to initialize the __path__ for the module
+            # we won't deal with Packages here
 
-        return mod
+            # add it to the modules list
+            sys.modules[fullname] = mod
 
-class importer(object):
+        return sys.modules[fullname]
 
+class importer(object):
+    '''  
+         If the importer is installed on sys.meta_path, it will
+         receive a second argument, which is None for a top-level module, or
+         package.__path__ for submodules or subpackages
+
+         It should return a loader object if the module was found, or None if it wasn't.  
+         If find_module() raises an exception, the caller will abort the import.
+         When importer.find_module("spam.eggs.ham") is called, "spam.eggs" has already 
+         been imported and added to sys.modules.
+    '''
     def __init__(self):
         self.loader = loader()
 
-    def find_module(self, fullname, path):
-        # path will be None for top-level Module and __path__ for sub-modules
-        print fullname
-        if path != None:
-            __path__ = path
-        try:
-            return sys.modules[fullname]
-        except KeyError:
-            pass
-
-        try:
-            return self.loader
-        except ImportError:
-            print "Import Error exception raised hence you better quit"
+    def find_module(self, fullname, path = None):
+        #print "( fullname = %s )  + ( path = %s )"%(fullname, path)
+        if fullname in DotNetModuleList:
+            # fullname is a  .Net Module
+            if path != None:
+                __path__ = path
+            try:
+                return sys.modules[fullname]
+            except KeyError:
+                pass
+
+            try:
+                return self.loader
+            except ImportError:
+                return None 
+        else:
+            # fullname is not a .Net Module
             return None
+            
+
 
-#def load_cli_class(space, namespace, classname):

Modified: pypy/branch/clr-module-improvements/pypy/module/clr/test/test_importer.py
==============================================================================
--- pypy/branch/clr-module-improvements/pypy/module/clr/test/test_importer.py	(original)
+++ pypy/branch/clr-module-improvements/pypy/module/clr/test/test_importer.py	Thu Dec  6 19:52:49 2007
@@ -15,6 +15,19 @@
         Math = clr.load_cli_class('System', 'Math')
         assert Math is System.Math
 
+        import System.Collections.Stack
+        a = System.Collections.Stack()
+        a.Push(3)
+        a.Push(44)
+        sum = 0
+        for i in a:
+           sum += i
+        assert sum == 3+44
+
+        import System.Collections.ArrayList
+        ArrayList = clr.load_cli_class('System.Collections', 'ArrayList')
+        assert ArrayList is System.Collections.ArrayList
+
     def test_ImportError(self):
         skip('Fixme!')
         def fn():



More information about the Pypy-commit mailing list