Commits

Amaury Forgeot d'Arc committed 96d4341

Move @export to pypy/translate/, and make it compatible with the one used by cli/carbonpython.py

  • Participants
  • Parent commits 87c817c
  • Branches separate-compilation

Comments (0)

Files changed (4)

File pypy/translator/c/separate.py

-import types
-
-class export(object):
-    """decorator to mark a function as exported by a shared module.
-    Can be used with a signature::
-        @export(float, float)
-        def f(x, y):
-            return x + y
-    or without any argument at all::
-        @export
-        def f(x, y):
-            return x + y
-    in which case the function must be used somewhere else, which will
-    trigger its annotation."""
-    argtypes = None
-
-    def __new__(cls, *args, **kwds):
-        if len(args) == 1 and isinstance(args[0], types.FunctionType):
-            func = args[0]
-            decorated = export()(func)
-            del decorated.argtypes
-            return decorated
-        return object.__new__(cls, *args, **kwds)
-
-    def __init__(self, *args):
-        self.argtypes = args
-
-    def __call__(self, func):
-        if self.argtypes is not None:
-            func.argtypes = self.argtypes
-        return func

File pypy/translator/c/test/test_separate.py

-from pypy.translator.c.separate import export
+from pypy.translator.separate import export
 from pypy.translator.translator import TranslationContext
 from pypy.translator.c.genc import CExtModuleBuilder, CLibraryBuilder, gen_forwarddecl
 from pypy.translator.tool.cbuild import ExternalCompilationInfo

File pypy/translator/separate.py

+import types
+
+class export(object):
+    """decorator to mark a function as exported by a shared module.
+    Can be used with a signature::
+        @export(float, float)
+        def f(x, y):
+            return x + y
+    or without any argument at all::
+        @export
+        def f(x, y):
+            return x + y
+    in which case the function must be used somewhere else, which will
+    trigger its annotation."""
+
+    argtypes = None
+    namespace = None
+
+    def __new__(cls, *args, **kwds):
+        if len(args) == 1 and isinstance(args[0], types.FunctionType):
+            func = args[0]
+            decorated = export()(func)
+            del decorated.argtypes
+            return decorated
+        return object.__new__(cls, *args, **kwds)
+
+    def __init__(self, *args, **kwargs):
+        self.argtypes = args
+        self.namespace = kwargs.get('namespace')
+
+    def __call__(self, func):
+        func.exported = True
+        if self.argtypes is not None:
+            func.argtypes = self.argtypes
+        if self.namespace is not None:
+            func.namespace = self.namespace
+        return func

File pypy/translator/test/test_separate.py

+from pypy.translator.separate import export
+
+class TestSeparate:
+    def test_export(self):
+        @export(int, float)
+        def foo(x, y):
+            pass
+        @export(int, float, namespace='test')
+        def bar(x, y):
+            pass
+        @export
+        def baz():
+            pass
+
+        assert foo.argtypes == (int, float)
+        assert not hasattr(foo, '_namespace_')
+        assert bar.argtypes == (int, float)
+        assert bar.namespace == 'test'
+        assert not hasattr(baz, 'argtypes')
+        assert baz.exported