Ejemplo n.º 1
0
 def test_vararg(self):
     if not sys.platform.startswith('linux'):
         py.test.skip("probably no symbol 'stderr' in the lib")
     ffi = FFI(backend=self.Backend())
     ffi.cdef("""
        int fprintf(void *, const char *format, ...);
        void *stderr;
     """)
     ffi.C = ffi.dlopen(None)
     with FdWriteCapture() as fd:
         ffi.C.fprintf(ffi.C.stderr, b"hello with no arguments\n")
         ffi.C.fprintf(ffi.C.stderr, b"hello, %s!\n",
                       ffi.new("char[]", b"world"))
         ffi.C.fprintf(ffi.C.stderr, ffi.new("char[]", b"hello, %s!\n"),
                       ffi.new("char[]", b"world2"))
         ffi.C.fprintf(ffi.C.stderr,
                       b"hello int %d long %ld long long %lld\n",
                       ffi.cast("int", 42), ffi.cast("long", 84),
                       ffi.cast("long long", 168))
         ffi.C.fprintf(ffi.C.stderr, b"hello %p\n", ffi.NULL)
     res = fd.getvalue()
     assert res == (b"hello with no arguments\n"
                    b"hello, world!\n"
                    b"hello, world2!\n"
                    b"hello int 42 long 84 long long 168\n"
                    b"hello (nil)\n")
Ejemplo n.º 2
0
    def test_function_pointer(self):
        ffi = FFI(backend=self.Backend())

        def cb(charp):
            assert repr(charp).startswith("<cdata 'char *' 0x")
            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(b"Hello")
        assert res == 42
        #
        if not sys.platform.startswith('linux'):
            py.test.skip("probably no symbol 'stderr' in the lib")
        ffi.cdef("""
            int fputs(const char *, void *);
            void *stderr;
        """)
        ffi.C = ffi.dlopen(None)
        fptr = ffi.cast("int(*)(const char *txt, void *)", ffi.C.fputs)
        assert fptr == ffi.C.fputs
        assert repr(fptr).startswith("<cdata 'int(*)(char *, void *)' 0x")
        with FdWriteCapture() as fd:
            fptr(b"world\n", ffi.C.stderr)
        res = fd.getvalue()
        assert res == b'world\n'
Ejemplo n.º 3
0
 def test_fputs_without_const(self):
     if not sys.platform.startswith('linux'):
         py.test.skip("probably no symbol 'stderr' in the lib")
     ffi = FFI(backend=self.Backend())
     ffi.cdef("""
         int fputs(char *, void *);
         void *stderr;
     """)
     ffi.C = ffi.dlopen(None)
     ffi.C.fputs  # fetch before capturing, for easier debugging
     with FdWriteCapture() as fd:
         ffi.C.fputs(b"hello\n", ffi.C.stderr)
         ffi.C.fputs(b"  world\n", ffi.C.stderr)
     res = fd.getvalue()
     assert res == b'hello\n  world\n'