Ejemplo n.º 1
0
    def setup_class(cls):
        testfuncs_w = []
        keepalive_funcs = []

        def find_and_load_library_for_test(space, w_name, w_is_global=None):
            if w_is_global is None:
                w_is_global = space.wrap(0)
            if space.is_w(w_name, space.w_None):
                path = None
            else:
                import ctypes.util
                path = ctypes.util.find_library(space.str_w(w_name))
            return space.appexec([space.wrap(path), w_is_global],
                                 """(path, is_global):
                import _cffi_backend
                return _cffi_backend.load_library(path, is_global)""")

        test_lib_c = tmpdir.join('_test_lib.c')
        src_test_lib_c = py.path.local(__file__).dirpath().join('_test_lib.c')
        src_test_lib_c.copy(test_lib_c)
        eci = ExternalCompilationInfo(include_dirs=[cdir])
        test_lib = host.compile([test_lib_c], eci, standalone=False)

        cdll = ctypes.CDLL(str(test_lib))
        cdll.gettestfunc.restype = ctypes.c_void_p

        def testfunc_for_test(space, w_num):
            if hasattr(space, 'int_w'):
                w_num = space.int_w(w_num)
            addr = cdll.gettestfunc(w_num)
            return space.wrap(addr)

        space = cls.space
        if cls.runappdirect:

            def interp2app(func):
                def run(*args):
                    return func(space, *args)

                return run
        else:
            interp2app = gateway.interp2app

        w_func = space.wrap(interp2app(find_and_load_library_for_test))
        w_testfunc = space.wrap(interp2app(testfunc_for_test))
        space.appexec([
            space.wrap(str(tmpdir)), w_func, w_testfunc,
            space.wrap(sys.version[:3])
        ], """(path, func, testfunc, underlying_version):
            import sys
            sys.path.append(path)
            import _all_test_c
            _all_test_c.PY_DOT_PY = underlying_version
            _all_test_c.find_and_load_library = func
            _all_test_c._testfunc = testfunc
        """)
Ejemplo n.º 2
0
    def setup_class(cls):
        testfuncs_w = []
        keepalive_funcs = []

        def find_and_load_library_for_test(space, w_name, w_is_global=None):
            if w_is_global is None:
                w_is_global = space.wrap(0)
            if space.is_w(w_name, space.w_None):
                path = None
            else:
                import ctypes.util
                path = ctypes.util.find_library(space.str_w(w_name))
            return space.appexec([space.wrap(path), w_is_global],
            """(path, is_global):
                import _cffi_backend
                return _cffi_backend.load_library(path, is_global)""")

        test_lib_c = tmpdir.join('_test_lib.c')
        src_test_lib_c = py.path.local(__file__).dirpath().join('_test_lib.c')
        src_test_lib_c.copy(test_lib_c)
        eci = ExternalCompilationInfo(include_dirs=[cdir])
        test_lib = host.compile([test_lib_c], eci, standalone=False)

        cdll = ctypes.CDLL(str(test_lib))
        cdll.gettestfunc.restype = ctypes.c_void_p

        def testfunc_for_test(space, w_num):
            if hasattr(space, 'int_w'):
                w_num = space.int_w(w_num)
            addr = cdll.gettestfunc(w_num)
            return space.wrap(addr)

        space = cls.space
        if cls.runappdirect:
            def interp2app(func):
                def run(*args):
                    return func(space, *args)
                return run
        else:
            interp2app = gateway.interp2app

        w_func = space.wrap(interp2app(find_and_load_library_for_test))
        w_testfunc = space.wrap(interp2app(testfunc_for_test))
        space.appexec([space.wrap(str(tmpdir)), w_func, w_testfunc,
                       space.wrap(sys.version[:3])],
        """(path, func, testfunc, underlying_version):
            import sys
            sys.path.append(path)
            import _all_test_c
            _all_test_c.PY_DOT_PY = underlying_version
            _all_test_c.find_and_load_library = func
            _all_test_c._testfunc = testfunc
        """)
        UniqueCache.for_testing = True
Ejemplo n.º 3
0
    def compile_shared_lib(self,
                           outputfilename=None,
                           ignore_a_files=False,
                           debug_mode=True,
                           defines=[]):
        self = self.convert_sources_to_files()
        if ignore_a_files:
            if not [fn for fn in self.link_files if fn.endswith('.a')]:
                ignore_a_files = False  # there are none
        if not self.separate_module_files and not ignore_a_files:
            return self  # xxx there was some condition about win32 here
        else:
            #basepath = py.path.local(self.separate_module_files[0]).dirpath()
            basepath = udir.join('shared_cache')
        if outputfilename is None:
            # find more or less unique name there
            pth = basepath.join('externmod').new(ext=host.so_ext)
            num = 0
            while pth.check():
                pth = basepath.join('externmod_%d' %
                                    (num, )).new(ext=host.so_ext)
                num += 1
            basepath.ensure(dir=1)
            outputfilename = str(pth.dirpath().join(pth.purebasename))

        d = self._copy_attributes()
        if ignore_a_files:
            d['link_files'] = [
                fn for fn in d['link_files'] if not fn.endswith('.a')
            ]
        if debug_mode and sys.platform != 'win32':
            d['compile_extra'] = d['compile_extra'] + ('-g', '-O0')
        d['compile_extra'] = d['compile_extra'] + (
            '-DRPY_EXTERN=RPY_EXPORTED', )
        for define in defines:
            d['compile_extra'] += ('-D%s' % define, )
        self = ExternalCompilationInfo(**d)

        lib = str(
            host.compile([],
                         self,
                         outputfilename=outputfilename,
                         standalone=False))
        d = self._copy_attributes()
        d['libraries'] += (lib, )
        d['separate_module_files'] = ()
        d['separate_module_sources'] = ()
        return ExternalCompilationInfo(**d)
Ejemplo n.º 4
0
    def compile_shared_lib(self, outputfilename=None, ignore_a_files=False,
                           debug_mode=True, defines=[]):
        self = self.convert_sources_to_files()
        if ignore_a_files:
            if not [fn for fn in self.link_files if fn.endswith('.a')]:
                ignore_a_files = False    # there are none
        if not self.separate_module_files and not ignore_a_files:
            return self    # xxx there was some condition about win32 here
        else:
            #basepath = py.path.local(self.separate_module_files[0]).dirpath()
            basepath = udir.join('shared_cache')
        if outputfilename is None:
            # find more or less unique name there
            pth = basepath.join('externmod').new(ext=host.so_ext)
            num = 0
            while pth.check():
                pth = basepath.join(
                    'externmod_%d' % (num,)).new(ext=host.so_ext)
                num += 1
            basepath.ensure(dir=1)
            outputfilename = str(pth.dirpath().join(pth.purebasename))

        d = self._copy_attributes()
        if ignore_a_files:
            d['link_files'] = [fn for fn in d['link_files']
                                  if not fn.endswith('.a')]
        if debug_mode and sys.platform != 'win32':
            d['compile_extra'] = d['compile_extra'] + ('-g', '-O0')
        d['compile_extra'] = d['compile_extra'] + (
            '-DRPY_EXTERN=RPY_EXPORTED',)
        for define in defines:
            d['compile_extra'] += ('-D%s' % define,)
        self = ExternalCompilationInfo(**d)

        lib = str(host.compile([], self, outputfilename=outputfilename,
                               standalone=False))
        d = self._copy_attributes()
        d['libraries'] += (lib,)
        d['separate_module_files'] = ()
        d['separate_module_sources'] = ()
        return ExternalCompilationInfo(**d)
Ejemplo n.º 5
0
    def compile_shared_lib(self, outputfilename=None, ignore_a_files=False):
        self = self.convert_sources_to_files()
        if ignore_a_files:
            if not [fn for fn in self.link_files if fn.endswith('.a')]:
                ignore_a_files = False    # there are none
        if not self.separate_module_files and not ignore_a_files:
            if sys.platform != 'win32':
                return self
            if not self.export_symbols:
                return self
            basepath = udir.join('module_cache')
        else:
            #basepath = py.path.local(self.separate_module_files[0]).dirpath()
            basepath = udir.join('shared_cache')
        if outputfilename is None:
            # find more or less unique name there
            pth = basepath.join('externmod').new(ext=host.so_ext)
            num = 0
            while pth.check():
                pth = basepath.join(
                    'externmod_%d' % (num,)).new(ext=host.so_ext)
                num += 1
            basepath.ensure(dir=1)
            outputfilename = str(pth.dirpath().join(pth.purebasename))

        if ignore_a_files:
            d = self._copy_attributes()
            d['link_files'] = [fn for fn in d['link_files']
                                  if not fn.endswith('.a')]
            self = ExternalCompilationInfo(**d)

        lib = str(host.compile([], self, outputfilename=outputfilename,
                               standalone=False))
        d = self._copy_attributes()
        d['libraries'] += (lib,)
        d['separate_module_files'] = ()
        d['separate_module_sources'] = ()
        return ExternalCompilationInfo(**d)
Ejemplo n.º 6
0
    def setup_class(cls):
        if cls.runappdirect:
            _cffi_backend = py.test.importorskip('_cffi_backend')
            if _cffi_backend.__version__ != TEST_VERSION:
                py.test.skip(
                    "These tests are for cffi version %s, this Python "
                    "has version %s installed" %
                    (TEST_VERSION, _cffi_backend.__version__))

        testfuncs_w = []
        keepalive_funcs = []
        UniqueCache.for_testing = True

        test_lib_c = tmpdir.join('_test_lib.c')
        src_test_lib_c = py.path.local(__file__).dirpath().join('_test_lib.c')
        src_test_lib_c.copy(test_lib_c)
        eci = ExternalCompilationInfo(include_dirs=[cdir])
        test_lib = str(host.compile([test_lib_c], eci, standalone=False))

        cdll = ctypes.CDLL(test_lib)
        cdll.gettestfunc.restype = ctypes.c_void_p

        space = cls.space
        if cls.runappdirect:

            def find_and_load_library_for_test(name, is_global=False):
                if name is None:
                    path = None
                else:
                    import ctypes.util
                    path = ctypes.util.find_library(name)
                import _cffi_backend
                return _cffi_backend.load_library(path, is_global)

            def w_testfunc_for_test(num):
                import ctypes
                cdll = ctypes.CDLL(str(self.test_lib))
                cdll.gettestfunc.restype = ctypes.c_void_p
                return cdll.gettestfunc(num)

            cls.w_test_lib = space.wrap(test_lib)
            cls.w_func = find_and_load_library_for_test
            cls.w_testfunc = w_testfunc_for_test
        else:

            def find_and_load_library_for_test(space,
                                               w_name,
                                               w_is_global=None):
                if w_is_global is None:
                    w_is_global = space.wrap(0)
                if space.is_w(w_name, space.w_None):
                    path = None
                    w_name = space.newtext('None')
                else:
                    import ctypes.util
                    path = ctypes.util.find_library(space.text_w(w_name))
                if path is None and sys.platform == 'win32':
                    py.test.skip("cannot find library '%s'" %
                                 (space.text_w(w_name), ))
                return space.appexec([space.wrap(path), w_is_global],
                                     """(path, is_global):
                    import _cffi_backend
                    return _cffi_backend.load_library(path, is_global)""")

            def testfunc_for_test(space, w_num):
                if hasattr(space, 'int_w'):
                    w_num = space.int_w(w_num)
                addr = cdll.gettestfunc(w_num)
                return space.wrap(addr)

            cls.w_func = space.wrap(
                gateway.interp2app(find_and_load_library_for_test))
            cls.w_testfunc = space.wrap(gateway.interp2app(testfunc_for_test))
        cls.w_zz_init = space.appexec([
            space.wrap(str(tmpdir)), cls.w_func, cls.w_testfunc,
            space.wrap(sys.version[:3])
        ], """(path, func, testfunc, underlying_version):
            import sys
            sys.path.append(path)
            import _all_test_c
            _all_test_c.PY_DOT_PY = underlying_version
            _all_test_c.find_and_load_library = func
            _all_test_c._testfunc = testfunc
        """)