Stefano Rivera avatar Stefano Rivera committed 49b84f3

Port setuptools tests to FFIBuilder

Comments (0)

Files changed (12)

testing/snippets/setuptools_module/ffibuilder.py

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

testing/snippets/setuptools_module/setup.py

+from setuptools import setup
 
-from setuptools import setup
-import snip_setuptools_verify
+from cffi.packaging import FFIExtension, build_ext
+
+import ffibuilder
+
 
 setup(
-    zip_safe=False,
-    py_modules=['snip_setuptools_verify'],
-    ext_modules=[snip_setuptools_verify.ffi.verifier.get_extension()])
+    data_files=['ffibuilder.py'],
+    ext_modules=[FFIExtension(ffibuilder.build_ffi)],
+    cmdclass={'build_ext': build_ext},
+)

testing/snippets/setuptools_module/snip_setuptools_verify.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
-     force_generic_engine=hasattr(sys, '_force_generic_engine_'))

testing/snippets/setuptools_package_1/setup.py

+from setuptools import setup
 
-from setuptools import setup
-import snip_setuptools_verify1
+from cffi.packaging import FFIExtension, build_ext
+
+import snip_setuptools_module1.ffibuilder
 
 setup(
-    zip_safe=False,
-    packages=['snip_setuptools_verify1'],
-    ext_modules=[snip_setuptools_verify1.ffi.verifier.get_extension()])
+    packages=['snip_setuptools_module1'],
+    ext_modules=[FFIExtension(snip_setuptools_module1.ffibuilder.build_ffi)],
+    cmdclass={'build_ext': build_ext},
+)
Add a comment to this file

testing/snippets/setuptools_package_1/snip_setuptools_module1/__init__.py

Empty file added.

testing/snippets/setuptools_package_1/snip_setuptools_module1/ffibuilder.py

+import sys
+
+from cffi import FFIBuilder
+
+
+def build_ffi(path):
+    builder = FFIBuilder('snip_setuptools_module1._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/setuptools_package_1/snip_setuptools_verify1/__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
-     force_generic_engine=hasattr(sys, '_force_generic_engine_'))

testing/snippets/setuptools_package_2/setup.py

+from setuptools import setup
 
-from setuptools import setup
-import snip_setuptools_verify2
+from cffi.packaging import FFIExtension, build_ext
+
+import snip_setuptools_module2.ffibuilder
+
 
 setup(
-    zip_safe=False,
-    packages=['snip_setuptools_verify2'],
-    ext_package='snip_setuptools_verify2',
-    ext_modules=[snip_setuptools_verify2.ffi.verifier.get_extension()])
+    packages=['snip_setuptools_module2'],
+    ext_package='snip_setuptools_module2',
+    ext_modules=[FFIExtension(snip_setuptools_module2.ffibuilder.build_ffi)],
+    cmdclass={'build_ext': build_ext},
+)
Add a comment to this file

testing/snippets/setuptools_package_2/snip_setuptools_module2/__init__.py

Empty file added.

testing/snippets/setuptools_package_2/snip_setuptools_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/setuptools_package_2/snip_setuptools_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_setuptools_verify2',
-     force_generic_engine=hasattr(sys, '_force_generic_engine_'))

testing/test_zintegration.py

 
 def test_setuptools_module():
     run_setup_and_program("setuptools_module", '''
-    import snip_setuptools_verify
-    p = snip_setuptools_verify.C.getpwuid(0)
-    assert snip_setuptools_verify.ffi.string(p.pw_name) == b"root"
+    import snip_setuptools_module as _ffi
+    lib = _ffi.load_passwd()
+    p = lib.getpwuid(0)
+    assert _ffi.string(p.pw_name) == b"root"
     ''')
 
 def test_setuptools_package_1():
     run_setup_and_program("setuptools_package_1", '''
-    import snip_setuptools_verify1
-    p = snip_setuptools_verify1.C.getpwuid(0)
-    assert snip_setuptools_verify1.ffi.string(p.pw_name) == b"root"
+    from snip_setuptools_module1 import _ffi
+    lib = _ffi.load_passwd()
+    p = lib.getpwuid(0)
+    assert _ffi.string(p.pw_name) == b"root"
     ''')
 
 def test_setuptools_package_2():
     run_setup_and_program("setuptools_package_2", '''
-    import snip_setuptools_verify2
-    p = snip_setuptools_verify2.C.getpwuid(0)
-    assert snip_setuptools_verify2.ffi.string(p.pw_name) == b"root"
+    from snip_setuptools_module2 import _ffi
+    lib = _ffi.load_passwd()
+    p = lib.getpwuid(0)
+    assert _ffi.string(p.pw_name) == b"root"
     ''')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.