[pypy-svn] r44729 - in pypy/dist/pypy/translator/cli: . test

antocuni at codespeak.net antocuni at codespeak.net
Wed Jul 4 19:38:06 CEST 2007


Author: antocuni
Date: Wed Jul  4 19:38:05 2007
New Revision: 44729

Modified:
   pypy/dist/pypy/translator/cli/carbonpython.py
   pypy/dist/pypy/translator/cli/test/test_carbonpython.py
Log:
add the possibility to specify the name of the target dll



Modified: pypy/dist/pypy/translator/cli/carbonpython.py
==============================================================================
--- pypy/dist/pypy/translator/cli/carbonpython.py	(original)
+++ pypy/dist/pypy/translator/cli/carbonpython.py	Wed Jul  4 19:38:05 2007
@@ -116,27 +116,41 @@
     return mydict[name]
 
 
-def compile_dll(filename):
-    _, name = os.path.split(filename)
-    dllname, _ = os.path.splitext(name)
+def compile_dll(filename, dllname=None, copy_dll=True):
+    dirname, name = os.path.split(filename)
+    if dllname is None:
+        dllname, _ = os.path.splitext(name)
+    elif dllname.endswith('.dll'):
+        dllname, _ = os.path.splitext(dllname)
     module = new.module(dllname)
     namespace = module.__dict__.get('_namespace_', dllname)
+    sys.path.insert(0, dirname)
     execfile(filename, module.__dict__)
+    sys.path.pop(0)
 
     dll = DllDef(dllname, namespace)
     dll.functions = collect_entrypoints(module.__dict__)
     dll.compile()
-    dll.driver.copy_cli_dll()
+    if copy_dll:
+        dll.driver.copy_cli_dll()
 
 def main(argv):
-    if len(argv) != 2:
+    if len(argv) == 2:
+        filename = argv[1]
+        dllname = None
+    elif len(argv) == 3:
+        filename = argv[1]
+        dllname = argv[2]
+    else:
         print >> sys.stderr, __doc__
         sys.exit(2)
-    filename = argv[1]
+
+    if not filename.endswith('.py'):
+        filename += '.py'
     if not os.path.exists(filename):
         print >> sys.stderr, "Cannot find file %s" % filename
         sys.exit(1)
-    compile_dll(filename)    
+    compile_dll(filename, dllname)
 
 if __name__ == '__main__':
     main(sys.argv)

Modified: pypy/dist/pypy/translator/cli/test/test_carbonpython.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_carbonpython.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_carbonpython.py	Wed Jul  4 19:38:05 2007
@@ -1,7 +1,9 @@
+import os
+import os.path
 from pypy.tool import udir
 from pypy.translator.cli.rte import Target
 from pypy.translator.cli.carbonpython import DllDef, export, collect_entrypoints,\
-     collect_class_entrypoints
+     collect_class_entrypoints, compile_dll
 from pypy.translator.cli.test.runtest import CliFunctionWrapper, CliTest
 
 TEMPLATE = """
@@ -140,3 +142,21 @@
             Console.WriteLine(Test.getitem(obj, 0));
         """)
         assert res == 42
+
+    def test_compile_dll(self):
+        cwd, _ = os.path.split(__file__)
+        mylib_py = os.path.join(cwd, 'mylib.py')
+        compile_dll(mylib_py, copy_dll=False)
+        res = self._csharp('mylib', """
+            Console.WriteLine(mylib.sum(20, 22));
+        """)
+        assert res == 42
+
+    def test_compile_dll_alternative_name(self):
+        cwd, _ = os.path.split(__file__)
+        mylib_py = os.path.join(cwd, 'mylib.py')
+        compile_dll(mylib_py, 'mylibxxx.dll', copy_dll=False)
+        res = self._csharp('mylibxxx', """
+            Console.WriteLine(mylibxxx.sum(20, 22));
+        """)
+        assert res == 42



More information about the Pypy-commit mailing list