Commits

Stefano Rivera committed 1e3bfa8

Port distutils_package_2 to FFIBuilder

Comments (0)

Files changed (5)

testing/snippets/distutils_package_2/setup.py

+from distutils.core import setup
 
-from distutils.core import setup
-import snip_basic_verify2
+from cffi.packaging import FFIExtension, build_ext
+
+import snip_basic_module2.ffibuilder
+
 
 setup(
-    packages=['snip_basic_verify2'],
-    ext_package='snip_basic_verify2',
-    ext_modules=[snip_basic_verify2.ffi.verifier.get_extension()])
+    packages=['snip_basic_module2'],
+    ext_package='snip_basic_module2',
+    ext_modules=[FFIExtension(snip_basic_module2.ffibuilder.build_ffi)],
+    cmdclass={'build_ext': build_ext},
+)

testing/snippets/distutils_package_2/snip_basic_module2/__init__.py

Empty file added.

testing/snippets/distutils_package_2/snip_basic_module2/ffibuilder.py

+import sys
+
+from cffi import FFIBuilder
+
+
+def build_ffi(path):
+    builder = FFIBuilder('_ffi', 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()

testing/snippets/distutils_package_2/snip_basic_verify2/__init__.py

-
-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
-     ext_package='snip_basic_verify2',
-     force_generic_engine=hasattr(sys, '_force_generic_engine_'))

testing/test_zintegration.py

 
 def test_distutils_package_2():
     run_setup_and_program("distutils_package_2", '''
-    import snip_basic_verify2
-    p = snip_basic_verify2.C.getpwuid(0)
-    assert snip_basic_verify2.ffi.string(p.pw_name) == b"root"
+    from snip_basic_module2 import _ffi
+    lib = _ffi.load_passwd()
+    p = lib.getpwuid(0)
+    assert _ffi.string(p.pw_name) == b"root"
     ''')
 
 def test_setuptools_module():