[pypy-commit] cffi cffi-1.0: ffi.emit_c_code()

arigo noreply at buildbot.pypy.org
Thu May 7 23:34:47 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1922:aa668c54f80a
Date: 2015-05-07 22:50 +0200
http://bitbucket.org/cffi/cffi/changeset/aa668c54f80a/

Log:	ffi.emit_c_code()

diff --git a/_cffi1/recompiler.py b/_cffi1/recompiler.py
--- a/_cffi1/recompiler.py
+++ b/_cffi1/recompiler.py
@@ -839,12 +839,13 @@
     return ffiplatform.get_extension(source_name, module_name, **kwds)
 
 def recompile(ffi, module_name, preamble, tmpdir='.',
-              call_c_compiler=True, **kwds):
+              call_c_compiler=True, c_file=None, **kwds):
     if not isinstance(module_name, str):
         module_name = module_name.encode('ascii')
     if ffi._windows_unicode:
         ffi._apply_windows_unicode(kwds)
-    c_file = os.path.join(tmpdir, module_name + '.c')
+    if c_file is None:
+        c_file = os.path.join(tmpdir, module_name + '.c')
     ext = _get_extension(module_name, c_file, kwds)
     updated = make_c_source(ffi, module_name, preamble, c_file)
     if call_c_compiler:
diff --git a/_cffi1/test_recompiler.py b/_cffi1/test_recompiler.py
--- a/_cffi1/test_recompiler.py
+++ b/_cffi1/test_recompiler.py
@@ -601,3 +601,10 @@
     p = lib.ff7()
     assert ffi.cast("int *", p)[0] == 42
     assert lib.ff7b(p) == 42
+
+def test_emit_c_code():
+    ffi = FFI()
+    ffi.set_source("foobar", "??")
+    c_file = str(udir.join('test_emit_c_code'))
+    ffi.emit_c_code(c_file)
+    assert os.path.isfile(c_file)
diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -500,6 +500,15 @@
             sys.stderr.write("generated %r\n" % (ext.sources[0],))
         return ext
 
+    def emit_c_code(self, filename):
+        from _cffi1 import recompile
+        #
+        if not hasattr(self, '_assigned_source'):
+            raise ValueError("set_source() must be called before emit_c_code()")
+        source, kwds = self._assigned_source
+        recompile(self, self._recompiler_module_name, source,
+                  c_file=filename, call_c_compiler=False, **kwds)
+
     def compile(self, tmpdir='.'):
         from _cffi1 import recompile
         #


More information about the pypy-commit mailing list