1. Python CFFI
  2. Untitled project
  3. cffi

Commits

Stefano Rivera  committed a983c3e

Port distutils_module to FFIBuilder

  • Participants
  • Parent commits 70f2aa4
  • Branches split-verify

Comments (0)

Files changed (4)

File testing/snippets/distutils_module/ffibuilder.py

View file
  • Ignore whitespace
+import sys
+
+from cffi import FFIBuilder
+
+
+def build_ffi(path):
+    builder = FFIBuilder('snip_basic_module', path)
+    builder.cdef("""     // some declarations from the man page
+    struct passwd {
+        char *pw_name;
+        ...;
+    };
+    struct passwd *getpwuid(int uid);
+    """)
+    builder.makelib('passwd', """   // passed to the real C compiler
+    #include <sys/types.h>
+    #include <pwd.h>
+    """, libraries=[],  # or a list of libraries to link with
+         force_generic_engine=hasattr(sys, '_force_generic_engine_'))
+    builder.write_ffi_module()
+    return builder.list_built_files()

File testing/snippets/distutils_module/setup.py

View file
  • Ignore whitespace
+from distutils.core import setup
 
-from distutils.core import setup
-import snip_basic_verify
+from cffi.packaging import FFIExtension, build_ext
+
+import ffibuilder
 
 setup(
-    py_modules=['snip_basic_verify'],
-    ext_modules=[snip_basic_verify.ffi.verifier.get_extension()])
+    ext_modules=[FFIExtension(ffibuilder.build_ffi)],
+    cmdclass={'build_ext': build_ext},
+)

File testing/snippets/distutils_module/snip_basic_verify.py

  • Ignore whitespace
-
-from cffi import FFI
-import sys
-
-ffi = FFI()
-ffi.cdef("""     // some declarations from the man page
-    struct passwd {
-        char *pw_name;
-        ...;
-    };
-    struct passwd *getpwuid(int uid);
-""")
-C = ffi.verify("""   // passed to the real C compiler
-#include <sys/types.h>
-#include <pwd.h>
-""", libraries=[],    # or a list of libraries to link with
-     force_generic_engine=hasattr(sys, '_force_generic_engine_'))

File testing/test_zintegration.py

View file
  • Ignore whitespace
 
 def test_distutils_module():
     run_setup_and_program("distutils_module", '''
-    import snip_basic_verify
-    p = snip_basic_verify.C.getpwuid(0)
-    assert snip_basic_verify.ffi.string(p.pw_name) == b"root"
+    import snip_basic_module
+    lib = snip_basic_module.load_passwd()
+    p = lib.getpwuid(0)
+    assert snip_basic_module.string(p.pw_name) == b"root"
     ''')
 
 def test_distutils_package_1():