[pypy-svn] rev 1458 - in pypy/trunk/src: . pypy/translator/test

hpk at codespeak.net hpk at codespeak.net
Tue Sep 30 10:55:31 CEST 2003


Author: hpk
Date: Tue Sep 30 10:55:30 2003
New Revision: 1458

Added:
   pypy/trunk/src/pypy/translator/test/buildpyxmodule.py
Modified:
   pypy/trunk/src/   (props changed)
   pypy/trunk/src/pypy/translator/test/test_sourcegen.py
Log:
- added to the 'src' directory the following externals

    'pyrex' (which points to the hacked version of Pyrex
             with the "cinline" statement

    'vpath' (which is used to handle creating temporary 
             files etc.)

- so in order to run the translator you need to checkout 
  the 'src' directory of pypy which contains the above 
  externals.

- now the translator actually has two unit-tests that
  check if the produced C-module produces the
  expected results when we call the now-C function.



Added: pypy/trunk/src/pypy/translator/test/buildpyxmodule.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/translator/test/buildpyxmodule.py	Tue Sep 30 10:55:30 2003
@@ -0,0 +1,58 @@
+
+import autopath
+from pypy.tool import test
+
+from vpath.local import Path, mkdtemp
+import os, sys
+
+def make_module_from_pyxstring(string, num=[0]):
+    tmpdir = mkdtemp()
+    n = num[0] = num[0]+1
+    pyxfile = tmpdir.join('test%d.pyx' %n) 
+    pyxfile.write(string)
+    make_c_from_pyxfile(pyxfile)
+    module = make_module_from_c(pyxfile)
+    #print "made module", module
+    return module
+
+def make_module_from_c(pyxfile):
+    from distutils.core import setup
+    from distutils.extension import Extension
+    from pyrex.Distutils import build_ext
+
+    dirpath = pyxfile.dirname()
+    lastdir = os.curdir
+    os.chdir(str(dirpath))
+    try:
+        modname = pyxfile.purebasename()
+        #print "modname", modname
+        setup(
+          name = "testmodules",
+          ext_modules=[ 
+                Extension(modname, [str(pyxfile)])
+          ],
+          cmdclass = {'build_ext': build_ext},
+          script_name = 'setup.py',
+          script_args = ['build_ext', '--inplace', '-q', '--quiet']
+        )
+        # XXX not a nice way to import a module
+        sys.path.insert(0, dirpath)
+        exec "import %(modname)s as testmodule" % locals()
+        sys.path.pop(0)
+    finally:
+        os.chdir(lastdir)
+    return testmodule
+
+def make_c_from_pyxfile(pyxfile):
+    from pyrex.Compiler.Main import CompilationOptions, Context, PyrexError
+    try:
+        options = CompilationOptions(show_version = 0, 
+                                     use_listing_file = 0, 
+                                     output_file = None)
+        context = Context(options.include_path)
+        result = context.compile(str(pyxfile), options, c_only = 1)
+        if result.num_errors > 0:
+            raise ValueError, "failure %s" % result
+    except PyrexError, e:
+        print >>sys.stderr, e
+    cfile = pyxfile.newsuffix('.c')

Modified: pypy/trunk/src/pypy/translator/test/test_sourcegen.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_sourcegen.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_sourcegen.py	Tue Sep 30 10:55:30 2003
@@ -5,6 +5,7 @@
 from pypy.translator.genpyrex import GenPyrex
 from pypy.translator.controlflow import *
 
+from buildpyxmodule import make_module_from_pyxstring
 
 class TestCase(test.IntTestCase):
     def test_simple_func(self):
@@ -22,11 +23,8 @@
                            endbranch)
         fun = FunctionGraph(block, "f")
         result = GenPyrex(fun).emitcode()
-        self.assertEquals(result, """
-def f(x):
-    result = x + 1
-    return result
-""")
+        mod = make_module_from_pyxstring(result)
+        self.assertEquals(mod.f(1), 2)
 
     def test_if(self):
         """
@@ -52,14 +50,9 @@
                            conditionalbranch)
         fun = FunctionGraph(startblock, "f")
         result = GenPyrex(fun).emitcode()
-        self.assertEquals(result, """
-def f(i, j):
-    conditionres = i < 0
-    if conditionres: cinline "goto label1;"
-    return i
-    cinline "label1:"
-    return j
-""")
+        mod = make_module_from_pyxstring(result)
+        self.assertEquals(mod.f(-1, 42), 42)
+        self.assertEquals(mod.f(3, 5), 3)
 
 if __name__ == '__main__':
     test.main()


More information about the Pypy-commit mailing list