Commits

Amaury Forgeot d'Arc  committed ca7830c

Start sharing code between CarbonPython and the C backend.

  • Participants
  • Parent commits 66a17d5
  • Branches separate-compilation

Comments (0)

Files changed (5)

File pypy/translator/cli/carbonpython.py

 
 from pypy.translator.driver import TranslationDriver
 from pypy.translator.cli.entrypoint import DllEntryPoint
+from pypy.translator.separate import export, is_exported
 
 class DllDef:
     def __init__(self, name, namespace, functions=[], dontmangle=True, isnetmodule=False):
         # add all functions to the appropriate namespace
         if self.namespace:
             for func, _ in self.functions:
-                if not hasattr(func, '_namespace_'):
-                    func._namespace_ = self.namespace
+                if not hasattr(func, 'namespace'):
+                    func.namespace = self.namespace
         self.driver.proceed(['compile_cli'])
 
-class export(object):
-    def __new__(self, *args, **kwds):
-        if len(args) == 1 and isinstance(args[0], types.FunctionType):
-            func = args[0]
-            func._inputtypes_ = ()
-            return func
-        return object.__new__(self, *args, **kwds)
-    
-    def __init__(self, *args, **kwds):
-        self.inputtypes = args
-        self.namespace = kwds.pop('namespace', None)
-        if len(kwds) > 0:
-            raise TypeError, "unexpected keyword argument: '%s'" % kwds.keys()[0]
-
-    def __call__(self, func):
-        func._inputtypes_ = self.inputtypes
-        if self.namespace is not None:
-            func._namespace_ = self.namespace
-        return func
-
-def is_exported(obj):
-    return isinstance(obj, (types.FunctionType, types.UnboundMethodType)) \
-           and hasattr(obj, '_inputtypes_')
-
 def collect_entrypoints(dic):
     entrypoints = []
     for item in dic.itervalues():
         if is_exported(item):
-            entrypoints.append((item, item._inputtypes_))
+            entrypoints.append((item, item.argtypes))
         elif isinstance(item, types.ClassType) or isinstance(item, type):
             entrypoints += collect_class_entrypoints(item)
     return entrypoints
     except AttributeError:
         return []
 
-    entrypoints = [(wrap_init(cls, __init__), __init__._inputtypes_)]
+    entrypoints = [(wrap_init(cls, __init__), __init__.argtypes)]
     for item in cls.__dict__.itervalues():
         if item is not __init__.im_func and is_exported(item):
-            inputtypes = (cls,) + item._inputtypes_
+            inputtypes = (cls,) + item.argtypes
             entrypoints.append((wrap_method(item), inputtypes))
     return entrypoints
 
     elif dllname.endswith('.dll'):
         dllname, _ = os.path.splitext(dllname)
     module = new.module(dllname)
-    namespace = module.__dict__.get('_namespace_', dllname)
+    namespace = module.__dict__.get('namespace', dllname)
     sys.path.insert(0, dirname)
     execfile(filename, module.__dict__)
     sys.path.pop(0)

File pypy/translator/cli/cts.py

     def graph_to_signature(self, graph, is_method = False, func_name = None):
         func_name = func_name or graph.name
         func_name = self.escape_name(func_name)
-        namespace = getattr(graph.func, '_namespace_', None)
+        namespace = getattr(graph.func, 'namespace', None)
         if namespace:
             func_name = '%s::%s' % (namespace, func_name)
 

File pypy/translator/cli/function.py

         if hasattr(self.db.genoo, 'exceptiontransformer'):
             self.auto_propagate_exceptions = False
         
-        namespace = getattr(self.graph.func, '_namespace_', None)
+        namespace = getattr(self.graph.func, 'namespace', None)
         if namespace:
             if '.' in namespace:
                 self.namespace, self.classname = namespace.rsplit('.', 1)

File pypy/translator/cli/test/test_carbonpython.py

         def baz():
             pass
 
-        assert foo._inputtypes_ == (int, float)
+        assert foo.argtypes == (int, float)
         assert not hasattr(foo, '_namespace_')
-        assert bar._inputtypes_ == (int, float)
-        assert bar._namespace_ == 'test'
-        assert baz._inputtypes_ == ()
+        assert bar.argtypes == (int, float)
+        assert bar.namespace == 'test'
+        assert baz.exported
+        assert not hasattr(baz, 'argtypes')
 
     def test_collect_entrypoints(self):
         @export(int, float)
                 pass
             
         class MyClass:
-            @export
+            @export()
             def __init__(self):
                 pass
             @export(int)
         assert res == 42
 
     def test_export_cliclass(self):
-        py.test.skip('it fails every other day on builbot, no clue why')
+        #py.test.skip('it fails every other day on builbot, no clue why')
         from pypy.translator.cli.dotnet import CLR
         
         @export(CLR.System.Collections.ArrayList, int)
         assert res == 42
 
     def test_compile_dll(self):
-        py.test.skip('This test fails every other day. No clue why :-(')
+        #py.test.skip('This test fails every other day. No clue why :-(')
         cwd, _ = os.path.split(__file__)
         mylib_py = os.path.join(cwd, 'mylib.py')
         compile_dll(mylib_py, copy_dll=False)

File pypy/translator/separate.py

             return decorated
         return object.__new__(cls, *args, **kwds)
 
-    def __init__(self, *args, **kwargs):
+    def __init__(self, *args, **kwds):
         self.argtypes = args
-        self.namespace = kwargs.get('namespace')
+        self.namespace = kwds.pop('namespace', None)
+        if kwds:
+            raise TypeError("unexpected keyword arguments: %s" % kwds.keys())
 
     def __call__(self, func):
         func.exported = True
         if self.namespace is not None:
             func.namespace = self.namespace
         return func
+
+def is_exported(obj):
+    return (isinstance(obj, (types.FunctionType, types.UnboundMethodType))
+            and getattr(obj, 'exported', False))
+