Commits

mattip committed 8154d15

compile test_capi once for a version of pypy and the c file

Comments (0)

Files changed (2)

lib_pypy/_pypy_testcapi.py

             return ext
 
 
-def compile_shared(csource, modulename):
+def compile_shared(csource, modulename, output_dir=None):
     """Compile '_testcapi.c' or '_ctypes_test.c' into an extension module,
     and import it.
     """
     thisdir = os.path.dirname(__file__)
-    output_dir = tempfile.mkdtemp()
+    if output_dir is None:
+        output_dir = tempfile.mkdtemp()
 
     from distutils.ccompiler import new_compiler
 

lib_pypy/_testcapi.py

+import sys, tempfile, imp, binascii, os
+
 try:
     import cpyext
 except ImportError:
     raise ImportError("No module named '_testcapi'")
-else:
+
+def get_hashed_dir(cfile):
+    # from cffi's Verifier()
+    key = '\x00'.join([sys.version[:3], cfile])
+    if sys.version_info >= (3,):
+        key = key.encode('utf-8')
+    k1 = hex(binascii.crc32(key[0::2]) & 0xffffffff)
+    k1 = k1.lstrip('0x').rstrip('L')
+    k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff)
+    k2 = k2.lstrip('0').rstrip('L')
+    output_dir = tempfile.gettempdir() + os.path.sep + 'tmp_%s%s' %(k1, k2)
+    if not os.path.exists(output_dir):
+        os.mkdir(output_dir)
+    return output_dir 
+
+cfile = '_testcapimodule.c'
+output_dir = get_hashed_dir(cfile)
+
+try:
+    fp, filename, description = imp.find_module('_testcapi', path=[output_dir])
+    imp.load_module('_testcapi', fp, filename, description)
+except ImportError:
     import _pypy_testcapi
-    _pypy_testcapi.compile_shared('_testcapimodule.c', '_testcapi')
+    _pypy_testcapi.compile_shared(cfile, '_testcapi', output_dir)