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

pdg at codespeak.net pdg at codespeak.net
Tue Jan 15 13:26:59 CET 2008


Author: pdg
Date: Tue Jan 15 13:26:58 2008
New Revision: 50626

Modified:
   pypy/branch/clr-module-improvements/pypy/module/clr/__init__.py
   pypy/branch/clr-module-improvements/pypy/module/clr/app_importer.py
   pypy/branch/clr-module-improvements/pypy/module/clr/interp_clr.py
   pypy/branch/clr-module-improvements/pypy/module/clr/test/test_importer.py
Log:
(antocuni, pdg) fixed importing of generic classes.

Modified: pypy/branch/clr-module-improvements/pypy/module/clr/__init__.py
==============================================================================
--- pypy/branch/clr-module-improvements/pypy/module/clr/__init__.py	(original)
+++ pypy/branch/clr-module-improvements/pypy/module/clr/__init__.py	Tue Jan 15 13:26:58 2008
@@ -14,8 +14,8 @@
         '_CliObject_internal': 'interp_clr.W_CliObject',
         'call_staticmethod': 'interp_clr.call_staticmethod',
         'load_cli_class': 'interp_clr.load_cli_class',
-        'list_of_valid_namespaces': 'interp_clr.list_of_valid_namespaces',
-        'list_of_generic_classes': 'interp_clr.list_of_generic_classes',
+        'get_extra_type_info': 'interp_clr.get_extra_type_info',
+        #'list_of_generic_classes': 'interp_clr.list_of_generic_classes',
         'isDotNetType': 'interp_clr.isDotNetType',
         'load_assembly': 'interp_clr.load_assembly',
         'list_of_loadedAssemblies': 'interp_clr.list_of_loadedAssemblies',

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	Tue Jan 15 13:26:58 2008
@@ -22,12 +22,14 @@
     def __init__(self):
         import clr
         # this might not be the correct place to load the valid NameSpaces
-        self.valid_name_spaces = set(clr.list_of_valid_namespaces())
+        valid_namespaces, generic_mappings = clr.get_extra_type_info()
+        self.valid_namespaces = set(valid_namespaces)
+        self.generic_mappings = dict(generic_mappings)
 
     def find_module(self, fullname, path=None):
         # check for true NAMESPACE or .NET TYPE 
         import clr
-        if fullname in self.valid_name_spaces or clr.isDotNetType(fullname): 
+        if fullname in self.valid_namespaces or fullname in self.generic_mappings or clr.isDotNetType(fullname): 
             # fullname is a  .Net Module
             return self
         else:
@@ -59,16 +61,16 @@
         '''
         # If it is a call for a Class then return with the Class reference
         import clr
-        if clr.isDotNetType(fullname):
+        if clr.isDotNetType(fullname) or fullname in self.generic_mappings:
             ''' Task is to breakup System.Collections.ArrayList and call 
                 clr.load_cli_class('System.Collections','ArrayList')
             '''
+            fullname = self.generic_mappings.get(fullname, fullname)
             rindex = fullname.rfind('.')
             if rindex != -1:
                 leftStr = fullname[:rindex]
                 rightStr = fullname[rindex+1:]
                 sys.modules[fullname] = clr.load_cli_class(leftStr, rightStr)
-
         else:  # if not a call for actual class (say for namespaces) assign an empty module 
             if fullname not in sys.modules:
                 mod = CLRModule(fullname)
@@ -86,10 +88,10 @@
 
             # treating System.Collections.Generic specially here.
             # this treatment is done after the empty module insertion
-            if fullname == "System.Collections.Generic":
-                genericClassList = clr.list_of_generic_classes()
-                for genericClass in genericClassList:
-                    sys.modules[genericClass[: genericClass.find('`')]] = clr.load_cli_class("System.Collections.Generic", genericClass)
+            #if fullname == "System.Collections.Generic":
+            #    genericClassList = clr.list_of_generic_classes()
+            #    for genericClass in genericClassList:
+            #        sys.modules[genericClass[: genericClass.find('`')]] = clr.load_cli_class("System.Collections.Generic", genericClass)
 
         return sys.modules[fullname]
 

Modified: pypy/branch/clr-module-improvements/pypy/module/clr/interp_clr.py
==============================================================================
--- pypy/branch/clr-module-improvements/pypy/module/clr/interp_clr.py	(original)
+++ pypy/branch/clr-module-improvements/pypy/module/clr/interp_clr.py	Tue Jan 15 13:26:58 2008
@@ -127,6 +127,13 @@
         list_w.append(space.newtuple(items_w))
     return space.newlist(list_w)
 
+def wrap_list_of_pairs(space, lst):
+    list_w = []
+    for (a,b) in lst:
+        items_w = [space.wrap(a), space.wrap(b)]
+        list_w.append(space.newtuple(items_w))
+    return space.newlist(list_w)
+
 def wrap_list_of_strings(space, lst):
     list_w = [space.wrap(s) for s in lst]
     return space.newlist(list_w)
@@ -185,7 +192,7 @@
         return self.cache.get(fullname, None)
 CliClassCache = _CliClassCache()
 
-def list_of_valid_namespaces(space):
+def get_extra_type_info(space):
     """
     We use this function to play with reflection
     and then return useful data to the app_importer module
@@ -195,6 +202,7 @@
     Return: List of Valid .NET namespaces
     """
     namespaces = {}
+    generics = []
     currentDomain = System.AppDomain.get_CurrentDomain()
     assems = currentDomain.GetAssemblies()
     for loadedAssembly in assems:
@@ -208,38 +216,44 @@
                 for chunk in chunks[1:]:
                     temp_name += "."+chunk
                     namespaces[temp_name] = None
+            if type.get_IsGenericType():
+                fullname = type.get_FullName()
+                index = fullname.rfind("`")
+                assert index != -1
+                generics.append((fullname[0:index], fullname))
     w_listOfNamespaces = wrap_list_of_strings(space, namespaces.keys())
-    return w_listOfNamespaces
+    w_generic_mappings = wrap_list_of_pairs(space, generics)
+    return space.newtuple([w_listOfNamespaces, w_generic_mappings])
 
-def list_of_generic_classes(space):
-    """
-    use reflection to get a list of generic classes 
-
-    Return: List of generic classes
-    e.g. [Dictionary`2 , List`1 , IEnumerator`1 , IEnumerable`1]
-    """
-    listOfGenericTypes = []
-    currentDomain = System.AppDomain.get_CurrentDomain()
-    assems = currentDomain.GetAssemblies()
-    for loadedAssembly in assems:
-        typesInAssembly = loadedAssembly.GetTypes()
-        for type in typesInAssembly:
-            namespace = type.get_Namespace()
-            type_str = type.ToString()
-            if namespace == "System.Collections.Generic":
-                rightDot = type_str.rfind('.')
-                rightTilde = type_str.rfind('`')
-                firstSqBracket = type_str.find('[')
-                firstPlus = type_str.find('+')
-                if rightDot != -1 and rightTilde != -1:
-                    if firstPlus == -1:
-                        nameToPush = type_str[rightDot+1 : firstSqBracket] 
-                    else:
-                        nameToPush = type_str[rightDot+1 : firstPlus] 
-                    if nameToPush not in listOfGenericTypes:
-                        listOfGenericTypes.append(nameToPush) 
-    w_listOfGenericTypes = wrap_list_of_strings(space, listOfGenericTypes)
-    return w_listOfGenericTypes
+#def list_of_generic_classes(space):
+#    """
+#    use reflection to get a list of generic classes 
+#
+#    Return: List of generic classes
+#    e.g. [Dictionary`2 , List`1 , IEnumerator`1 , IEnumerable`1]
+#    """
+#    listOfGenericTypes = []
+#    currentDomain = System.AppDomain.get_CurrentDomain()
+#    assems = currentDomain.GetAssemblies()
+#    for loadedAssembly in assems:
+#        typesInAssembly = loadedAssembly.GetTypes()
+#        for type in typesInAssembly:
+#            namespace = type.get_Namespace()
+#            type_str = type.ToString()
+#            if namespace == "System.Collections.Generic":
+#                rightDot = type_str.rfind('.')
+#                rightTilde = type_str.rfind('`')
+#                firstSqBracket = type_str.find('[')
+#                firstPlus = type_str.find('+')
+#                if rightDot != -1 and rightTilde != -1:
+#                    if firstPlus == -1:
+#                        nameToPush = type_str[rightDot+1 : firstSqBracket] 
+#                    else:
+#                        nameToPush = type_str[rightDot+1 : firstPlus] 
+#                    if nameToPush not in listOfGenericTypes:
+#                        listOfGenericTypes.append(nameToPush) 
+#    w_listOfGenericTypes = wrap_list_of_strings(space, listOfGenericTypes)
+#    return w_listOfGenericTypes
 
 def isDotNetType(space, nameFromImporter):
     """

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	Tue Jan 15 13:26:58 2008
@@ -7,7 +7,7 @@
 
     def test_list_of_valid_namespaces(self):
         import clr
-        ns = clr.list_of_valid_namespaces()
+        ns, gen = clr.get_extra_type_info()
         
         assert 'System' in ns
         assert 'System.Collections' in ns
@@ -51,3 +51,7 @@
     def test_lazy_import(self):
         import System
         System.Runtime.InteropServices # does not raise attribute error
+
+    def test_generic_class_import(self):
+        import System.Collections.Generic.List
+



More information about the Pypy-commit mailing list