Esempio n. 1
0
    def test_function(self):
        import _cffi_backend
        from re_python_pysrc import ffi

        lib = ffi.dlopen(self.extmod)
        assert lib.add42(-10) == 32
        assert type(lib.add42) is _cffi_backend.FFI.CData
Esempio n. 2
0
    def test_no_such_function_or_global_var(self):
        from re_python_pysrc import ffi

        lib = ffi.dlopen(self.extmod)
        e = raises(ffi.error, getattr, lib, "no_such_function")
        assert str(e.value).startswith("symbol 'no_such_function' not found in library '")
        e = raises(ffi.error, getattr, lib, "no_such_globalvar")
        assert str(e.value).startswith("symbol 'no_such_globalvar' not found in library '")
Esempio n. 3
0
def test_global_var():
    from re_python_pysrc import ffi
    lib = ffi.dlopen(extmod)
    assert lib.globalvar42 == 1234
    p = ffi.addressof(lib, 'globalvar42')
    lib.globalvar42 += 5
    assert p[0] == 1239
    p[0] -= 1
    assert lib.globalvar42 == 1238
Esempio n. 4
0
def test_dlopen_none():
    import _cffi_backend
    from re_python_pysrc import ffi
    name = None
    if sys.platform == 'win32':
        import ctypes.util
        name = ctypes.util.find_msvcrt()
    lib = ffi.dlopen(name)
    assert lib.strlen(b"hello") == 5
Esempio n. 5
0
    def test_dlclose(self):
        import _cffi_backend
        from re_python_pysrc import ffi

        lib = ffi.dlopen(self.extmod)
        ffi.dlclose(lib)
        e = raises(ffi.error, ffi.dlclose, lib)
        assert str(e.value) == ("library '%s' is already closed" % (self.extmod,))
        e = raises(ffi.error, getattr, lib, "add42")
        assert str(e.value) == ("library '%s' has been closed" % (self.extmod,))
Esempio n. 6
0
def test_dlclose():
    import _cffi_backend
    from re_python_pysrc import ffi
    lib = ffi.dlopen(extmod)
    ffi.dlclose(lib)
    e = py.test.raises(ffi.error, ffi.dlclose, lib)
    assert str(e.value).startswith(
        "library '%s' is already closed" % (extmod,))
    e = py.test.raises(ffi.error, getattr, lib, 'add42')
    assert str(e.value) == (
        "library '%s' has been closed" % (extmod,))
Esempio n. 7
0
def test_dlopen_none():
    import _cffi_backend
    from re_python_pysrc import ffi
    name = None
    if sys.platform == 'win32':
        import ctypes.util
        name = ctypes.util.find_msvcrt()
        if name is None:
            py.test.skip("dlopen(None) cannot work on Windows with Python 3")
    lib = ffi.dlopen(name)
    assert lib.strlen(b"hello") == 5
Esempio n. 8
0
 def test_include_1(self):
     from re_py_subsrc import ffi
     assert ffi.integer_const('FOOBAR') == -42
     assert ffi.integer_const('FOOBAZ') == -43
     assert ffi.integer_const('k2') == 121212
     lib = ffi.dlopen(self.extmod)     # <- a random unrelated library would be fine
     assert lib.FOOBAR == -42
     assert lib.FOOBAZ == -43
     assert lib.k2 == 121212
     #
     p = ffi.new("bar_t *", [5, b"foobar"])
     assert p.a[4] == ord('a')
Esempio n. 9
0
def test_dlclose():
    import _cffi_backend
    from re_python_pysrc import ffi
    lib = ffi.dlopen(extmod)
    ffi.dlclose(lib)
    if type(extmod) is not str:   # unicode, on python 2
        str_extmod = extmod.encode('utf-8')
    else:
        str_extmod = extmod
    e = py.test.raises(ffi.error, getattr, lib, 'add42')
    assert str(e.value) == (
        "library '%s' has been closed" % (str_extmod,))
    ffi.dlclose(lib)   # does not raise
Esempio n. 10
0
def test_include_1():
    sub_ffi = FFI()
    sub_ffi.cdef("static const int k2 = 121212;")
    sub_ffi.include(original_ffi)
    assert 'macro FOOBAR' in original_ffi._parser._declarations
    assert 'macro FOOBAZ' in original_ffi._parser._declarations
    sub_ffi.set_source('re_python_pysrc', None)
    sub_ffi.emit_python_code(str(tmpdir.join('_re_include_1.py')))
    #
    if sys.version_info[:2] >= (3, 3):
        import importlib
        importlib.invalidate_caches()  # issue 197 (but can't reproduce myself)
    #
    from _re_include_1 import ffi
    assert ffi.integer_const('FOOBAR') == -42
    assert ffi.integer_const('FOOBAZ') == -43
    assert ffi.integer_const('k2') == 121212
    lib = ffi.dlopen(extmod)  # <- a random unrelated library would be fine
    assert lib.FOOBAR == -42
    assert lib.FOOBAZ == -43
    assert lib.k2 == 121212
    #
    p = ffi.new("bar_t *", [5, b"foobar"])
    assert p.a[4] == ord('a')
Esempio n. 11
0
def test_include_1():
    sub_ffi = FFI()
    sub_ffi.cdef("static const int k2 = 121212;")
    sub_ffi.include(original_ffi)
    assert 'macro FOOBAR' in original_ffi._parser._declarations
    assert 'macro FOOBAZ' in original_ffi._parser._declarations
    sub_ffi.set_source('re_python_pysrc', None)
    sub_ffi.emit_python_code(str(tmpdir.join('_re_include_1.py')))
    #
    if sys.version_info[:2] >= (3, 3):
        import importlib
        importlib.invalidate_caches()  # issue 197 (but can't reproduce myself)
    #
    from _re_include_1 import ffi
    assert ffi.integer_const('FOOBAR') == -42
    assert ffi.integer_const('FOOBAZ') == -43
    assert ffi.integer_const('k2') == 121212
    lib = ffi.dlopen(extmod)     # <- a random unrelated library would be fine
    assert lib.FOOBAR == -42
    assert lib.FOOBAZ == -43
    assert lib.k2 == 121212
    #
    p = ffi.new("bar_t *", [5, b"foobar"])
    assert p.a[4] == ord('a')
Esempio n. 12
0
def test_function_with_varargs():
    import _cffi_backend
    from re_python_pysrc import ffi
    lib = ffi.dlopen(extmod, 0)
    assert lib.add43(45, ffi.cast("int", -5)) == 45
    assert type(lib.add43) is _cffi_backend.FFI.CData
Esempio n. 13
0
def test_function():
    import _cffi_backend
    from re_python_pysrc import ffi
    lib = ffi.dlopen(extmod)
    assert lib.add42(-10) == 32
    assert type(lib.add42) is _cffi_backend.FFI.CData
Esempio n. 14
0
def test_global_const_nonint():
    from re_python_pysrc import ffi
    lib = ffi.dlopen(extmod)
    assert ffi.string(lib.globalconsthello, 8) == b"hello"
    py.test.raises(AttributeError, ffi.addressof, lib, 'globalconsthello')
Esempio n. 15
0
def test_global_const_int():
    from re_python_pysrc import ffi
    lib = ffi.dlopen(extmod)
    assert lib.globalconst42 == 4321
    py.test.raises(AttributeError, ffi.addressof, lib, 'globalconst42')
Esempio n. 16
0
    def test_global_const_int(self):
        from re_python_pysrc import ffi

        lib = ffi.dlopen(self.extmod)
        assert lib.globalconst42 == 4321
        raises(AttributeError, ffi.addressof, lib, "globalconst42")
Esempio n. 17
0
def test_constant_via_lib():
    from re_python_pysrc import ffi
    lib = ffi.dlopen(extmod)
    assert lib.FOOBAR == -42
    assert lib.FOOBAZ == -43
Esempio n. 18
0
 def test_constant_via_lib(self):
     self.fix_path()
     from re_python_pysrc import ffi
     lib = ffi.dlopen(self.extmod)
     assert lib.FOOBAR == -42
     assert lib.FOOBAZ == -43
Esempio n. 19
0
 def test_global_const_int(self):
     self.fix_path()
     from re_python_pysrc import ffi
     lib = ffi.dlopen(self.extmod)
     assert lib.globalconst42 == 4321
     raises(AttributeError, ffi.addressof, lib, 'globalconst42')
Esempio n. 20
0
 def test_global_const_nonint(self):
     self.fix_path()
     from re_python_pysrc import ffi
     lib = ffi.dlopen(self.extmod)
     assert ffi.string(lib.globalconsthello, 8) == "hello"
     raises(AttributeError, ffi.addressof, lib, 'globalconsthello')