Author: Armin Rigo <[email protected]>
Branch: cffi-1.0
Changeset: r1890:bae04f21862d
Date: 2015-04-30 09:19 +0200
http://bitbucket.org/cffi/cffi/changeset/bae04f21862d/
Log: setuptools_ext: generate the C module in memory. Avoids the
os.rename() which, I think, fails to overwrite on Windows.
diff --git a/_cffi1/recompiler.py b/_cffi1/recompiler.py
--- a/_cffi1/recompiler.py
+++ b/_cffi1/recompiler.py
@@ -1,4 +1,4 @@
-import os, sys
+import os, sys, io
from cffi import ffiplatform, model
from .cffi_opcode import *
@@ -806,11 +806,26 @@
self.cffi_types[index] = CffiOp(OP_ENUM, enum_index)
-def make_c_source(ffi, module_name, preamble, target_c_file):
+if sys.version_info >= (3,):
+ NativeIO = io.StringIO
+else:
+ class NativeIO(io.BytesIO):
+ def write(self, s):
+ if isinstance(s, unicode):
+ s = s.encode('ascii')
+ super(NativeIO, self).write(s)
+
+def make_c_source(ffi, module_name, preamble, target_c_file=NativeIO):
recompiler = Recompiler(ffi, module_name)
recompiler.collect_type_table()
- with open(target_c_file, 'w') as f:
+ if target_c_file is NativeIO:
+ f = NativeIO()
recompiler.write_source_to_f(f, preamble)
+ return f.getvalue()
+ else:
+ with open(target_c_file, 'w') as f:
+ recompiler.write_source_to_f(f, preamble)
+ return None
def _get_extension(module_name, c_file, kwds):
source_name = ffiplatform.maybe_relative_path(c_file)
diff --git a/_cffi1/setuptools_ext.py b/_cffi1/setuptools_ext.py
--- a/_cffi1/setuptools_ext.py
+++ b/_cffi1/setuptools_ext.py
@@ -40,22 +40,20 @@
ext = Extension(name=module_name, sources=allsources, **kwds)
def make_mod(tmpdir):
- mkpath(tmpdir)
file_name = module_name + '.c'
log.info("generating cffi module %r" % file_name)
+ output = recompiler.make_c_source(ffi, module_name, source)
+ mkpath(tmpdir)
c_file = os.path.join(tmpdir, file_name)
- c_tmp = '%s.%s' % (c_file, os.getpid())
- recompiler.make_c_source(ffi, module_name, source, c_tmp)
try:
with open(c_file, 'r') as f1:
- with open(c_tmp, 'r') as f2:
- if f1.read() != f2.read():
- raise IOError
+ if f1.read() != output:
+ raise IOError
except IOError:
- os.rename(c_tmp, c_file)
+ with open(c_file, 'w') as f1:
+ f1.write(output)
else:
log.info("already up-to-date")
- os.unlink(c_tmp)
return c_file
if dist.ext_modules is None:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit