示例#1
0
    def test_function_pointer(self):
        ffi = FFI(backend=self.Backend())

        def cb(charp):
            assert repr(charp) == "<cdata 'char *'>"
            return 42

        fptr = ffi.callback("int(*)(const char *txt)", cb)
        assert fptr != ffi.callback("int(*)(const char *)", cb)
        assert repr(fptr) == "<cdata 'int(*)(char *)' calling %r>" % (cb, )
        res = fptr("Hello")
        assert res == 42
        #
        ffi.cdef("""
            int puts(const char *);
            int fflush(void *);
        """)
        fptr = ffi.cast("int(*)(const char *txt)", ffi.C.puts)
        assert fptr == ffi.C.puts
        assert repr(fptr) == "<cdata 'int(*)(char *)'>"
        with FdWriteCapture() as fd:
            fptr("world")
            ffi.C.fflush(None)
        res = fd.getvalue()
        assert res == 'world\n'
示例#2
0
    def test_functionptr_simple(self):
        ffi = FFI(backend=self.Backend())
        py.test.raises(TypeError, ffi.callback, "int(*)(int)")
        py.test.raises(TypeError, ffi.callback, "int(*)(int)", 0)

        def cb(n):
            return n + 1

        p = ffi.callback("int(*)(int)", cb)
        res = p(41)  # calling an 'int(*)(int)', i.e. a function pointer
        assert res == 42 and type(res) is int
        res = p(ffi.cast("int", -41))
        assert res == -40 and type(res) is int
        assert repr(p).startswith(
            "<cdata 'int(*)(int)' calling <function cb at 0x")
        assert ffi.typeof(p) is ffi.typeof("int(*)(int)")
        q = ffi.new("int(*)(int)", p)
        assert repr(
            q) == "<cdata 'int(* *)(int)' owning %d bytes>" % (SIZE_OF_PTR)
        py.test.raises(TypeError, "q(43)")
        res = q[0](43)
        assert res == 44
        q = ffi.cast("int(*)(int)", p)
        assert repr(q) == "<cdata 'int(*)(int)'>"
        res = q(45)
        assert res == 46
示例#3
0
 def test_cast_functionptr_and_int(self):
     ffi = FFI(backend=self.Backend())
     def cb(n):
         return n + 1
     a = ffi.callback("int(*)(int)", cb)
     p = ffi.cast("void *", a)
     assert p
     b = ffi.cast("int(*)(int)", p)
     assert b(41) == 42
     assert a == b
     assert hash(a) == hash(b)
示例#4
0
    def test_cast_functionptr_and_int(self):
        ffi = FFI(backend=self.Backend())

        def cb(n):
            return n + 1

        a = ffi.callback("int(*)(int)", cb)
        p = ffi.cast("void *", a)
        assert p
        b = ffi.cast("int(*)(int)", p)
        assert b(41) == 42
        assert a == b
        assert hash(a) == hash(b)
示例#5
0
 def test_function_pointer(self):
     ffi = FFI(backend=self.Backend())
     def cb(charp):
         assert repr(charp) == "<cdata 'char *'>"
         return 42
     fptr = ffi.callback("int(*)(const char *txt)", cb)
     assert fptr != ffi.callback("int(*)(const char *)", cb)
     assert repr(fptr) == "<cdata 'int(*)(char *)' calling %r>" % (cb,)
     res = fptr("Hello")
     assert res == 42
     #
     ffi.cdef("""
         int puts(const char *);
         int fflush(void *);
     """)
     fptr = ffi.cast("int(*)(const char *txt)", ffi.C.puts)
     assert fptr == ffi.C.puts
     assert repr(fptr) == "<cdata 'int(*)(char *)'>"
     with FdWriteCapture() as fd:
         fptr("world")
         ffi.C.fflush(None)
     res = fd.getvalue()
     assert res == 'world\n'
示例#6
0
 def test_functionptr_simple(self):
     ffi = FFI(backend=self.Backend())
     py.test.raises(TypeError, ffi.callback, "int(*)(int)")
     py.test.raises(TypeError, ffi.callback, "int(*)(int)", 0)
     def cb(n):
         return n + 1
     p = ffi.callback("int(*)(int)", cb)
     res = p(41)     # calling an 'int(*)(int)', i.e. a function pointer
     assert res == 42 and type(res) is int
     res = p(ffi.cast("int", -41))
     assert res == -40 and type(res) is int
     assert repr(p).startswith(
         "<cdata 'int(*)(int)' calling <function cb at 0x")
     assert ffi.typeof(p) is ffi.typeof("int(*)(int)")
     q = ffi.new("int(*)(int)", p)
     assert repr(q) == "<cdata 'int(* *)(int)' owning %d bytes>" % (
         SIZE_OF_PTR)
     py.test.raises(TypeError, "q(43)")
     res = q[0](43)
     assert res == 44
     q = ffi.cast("int(*)(int)", p)
     assert repr(q) == "<cdata 'int(*)(int)'>"
     res = q(45)
     assert res == 46