[pypy-svn] r15443 - pypy/dist/pypy/translator/tool

arigo at codespeak.net arigo at codespeak.net
Sat Jul 30 21:58:57 CEST 2005


Author: arigo
Date: Sat Jul 30 21:58:55 2005
New Revision: 15443

Modified:
   pypy/dist/pypy/translator/tool/cbuild.py
Log:
Refactored make_module_from_c() into two independently callable functions.


Modified: pypy/dist/pypy/translator/tool/cbuild.py
==============================================================================
--- pypy/dist/pypy/translator/tool/cbuild.py	(original)
+++ pypy/dist/pypy/translator/tool/cbuild.py	Sat Jul 30 21:58:55 2005
@@ -2,7 +2,7 @@
 
 import py
 
-import os, sys, inspect, re
+import os, sys, inspect, re, imp
 from pypy.translator.tool import stdoutcapture
 
 debug = 0
@@ -36,7 +36,7 @@
         opt = '-O0'
     gcv['OPT'] = opt
 
-def make_module_from_c(cfile, include_dirs=None):
+def compile_c_module(cfile, modname, include_dirs=None):
     #try:
     #    from distutils.log import set_threshold
     #    set_threshold(10000)
@@ -47,10 +47,8 @@
         include_dirs = []
 
     dirpath = cfile.dirpath()
-    lastdir = py.path.local()
-    os.chdir(str(dirpath))
+    lastdir = dirpath.chdir()
     try:
-        modname = cfile.purebasename
         if debug: print "modname", modname
         c = stdoutcapture.Capture(mixed_out_err = True)
         try:
@@ -116,19 +114,26 @@
                     fdump.close()
             # XXX do we need to do some check on fout/ferr?
             # XXX not a nice way to import a module
-            if debug: print "inserting path to sys.path", dirpath
-            sys.path.insert(0, '.')
-            if debug: print "import %(modname)s as testmodule" % locals()
-            exec "import %(modname)s as testmodule" % locals()
-            sys.path.pop(0)
         except:
             print data
             raise
     finally:
-        os.chdir(str(lastdir))
-        #if not debug:
-        #dirpath.rmtree()
-    return testmodule
+        lastdir.chdir()
+
+def make_module_from_c(cfile, include_dirs=None):
+    cfile = py.path.local(cfile)
+    modname = cfile.purebasename
+    compile_c_module(cfile, modname, include_dirs)
+    return import_module_from_directory(cfile.dirpath(), modname)
+
+def import_module_from_directory(dir, modname):
+    file, pathname, description = imp.find_module(modname, [str(dir)])
+    try:
+        mod = imp.load_module(modname, file, pathname, description)
+    finally:
+        if file:
+            file.close()
+    return mod
 
 def make_c_from_pyxfile(pyxfile):
     from pypy.translator.pyrex import genpyrex



More information about the Pypy-commit mailing list