Esempio n. 1
0
 def test_bug_not_list_or_tuple(self):
     import _cffi_backend
     LONG = _cffi_backend.new_primitive_type('long')
     P_LONG = _cffi_backend.new_pointer_type(LONG)
     LONG_ARRAY_2 = _cffi_backend.new_array_type(P_LONG, 2)
     P_LONG_ARRAY_2 = _cffi_backend.new_pointer_type(LONG_ARRAY_2)
     LONG_ARRAY_ARRAY = _cffi_backend.new_array_type(P_LONG_ARRAY_2, None)
     raises(TypeError, _cffi_backend.newp, LONG_ARRAY_ARRAY, [set([4, 5])])
Esempio n. 2
0
 def test_bug_not_list_or_tuple(self):
     import _cffi_backend
     LONG = _cffi_backend.new_primitive_type('long')
     P_LONG = _cffi_backend.new_pointer_type(LONG)
     LONG_ARRAY_2 = _cffi_backend.new_array_type(P_LONG, 2)
     P_LONG_ARRAY_2 = _cffi_backend.new_pointer_type(LONG_ARRAY_2)
     LONG_ARRAY_ARRAY = _cffi_backend.new_array_type(P_LONG_ARRAY_2, None)
     raises(TypeError, _cffi_backend.newp, LONG_ARRAY_ARRAY, [set([4, 5])])
Esempio n. 3
0
        def main():
            import os
            try:
                import _cffi_backend
            except ImportError:
                sys.stderr.write('SKIP: cannot import _cffi_backend\n')
                return 0

            libc = _cffi_backend.load_library(None)
            BInt = _cffi_backend.new_primitive_type("int")
            BSizeT = _cffi_backend.new_primitive_type("size_t")
            BChar = _cffi_backend.new_primitive_type("char")
            BCharP = _cffi_backend.new_pointer_type(BChar)
            BWrite = _cffi_backend.new_function_type([BInt, BCharP, BSizeT],
                                                     BSizeT)  # not signed here!
            _write = libc.load_function(BWrite, 'write')
            i = 0
            fd0, fd1 = os.pipe()
            buffer = _cffi_backend.newp(BCharP, 'A')
            while i < 300:
                tmp = _write(fd1, buffer, 1)   # ID: cfficall
                assert tmp == 1
                assert os.read(fd0, 2) == 'A'
                i += 1
            os.close(fd0)
            os.close(fd1)
            return 42
Esempio n. 4
0
        def main():
            import os
            try:
                import _cffi_backend
            except ImportError:
                sys.stderr.write('SKIP: cannot import _cffi_backend\n')
                return 0

            libc = _cffi_backend.load_library(None)
            BInt = _cffi_backend.new_primitive_type("int")
            BSizeT = _cffi_backend.new_primitive_type("size_t")
            BChar = _cffi_backend.new_primitive_type("char")
            BCharP = _cffi_backend.new_pointer_type(BChar)
            BWrite = _cffi_backend.new_function_type(
                [BInt, BCharP, BSizeT], BSizeT)  # not signed here!
            _write = libc.load_function(BWrite, 'write')
            i = 0
            fd0, fd1 = os.pipe()
            buffer = _cffi_backend.newp(BCharP, 'A')
            while i < 300:
                tmp = _write(fd1, buffer, 1)  # ID: cfficall
                assert tmp == 1
                assert os.read(fd0, 2) == 'A'
                i += 1
            os.close(fd0)
            os.close(fd1)
            return 42
Esempio n. 5
0
 def test_fast_init_clongdouble_from_list(self):
     import _cffi_backend
     LONGDOUBLE = _cffi_backend.new_primitive_type('long double')
     P_LONGDOUBLE = _cffi_backend.new_pointer_type(LONGDOUBLE)
     LONGDOUBLE_ARRAY = _cffi_backend.new_array_type(P_LONGDOUBLE, None)
     buf = _cffi_backend.newp(LONGDOUBLE_ARRAY, [1.25, -3.5])
     assert float(buf[0]) == 1.25
     assert float(buf[1]) == -3.5
Esempio n. 6
0
 def test_TypeError_if_no_length(self):
     import _cffi_backend
     LONG = _cffi_backend.new_primitive_type('long')
     P_LONG = _cffi_backend.new_pointer_type(LONG)
     LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, 3)
     buf = _cffi_backend.newp(LONG_ARRAY)
     pbuf = _cffi_backend.cast(P_LONG, buf)
     raises(TypeError, "list(pbuf)")
Esempio n. 7
0
 def test_TypeError_if_no_length(self):
     import _cffi_backend
     LONG = _cffi_backend.new_primitive_type('long')
     P_LONG = _cffi_backend.new_pointer_type(LONG)
     LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, 3)
     buf = _cffi_backend.newp(LONG_ARRAY)
     pbuf = _cffi_backend.cast(P_LONG, buf)
     raises(TypeError, "list(pbuf)")
Esempio n. 8
0
 def test_fast_init_cfloat_from_list(self):
     import _cffi_backend
     FLOAT = _cffi_backend.new_primitive_type('float')
     P_FLOAT = _cffi_backend.new_pointer_type(FLOAT)
     FLOAT_ARRAY = _cffi_backend.new_array_type(P_FLOAT, None)
     buf = _cffi_backend.newp(FLOAT_ARRAY, [1.25, -3.5])
     assert buf[0] == 1.25
     assert buf[1] == -3.5
Esempio n. 9
0
 def test_fast_init_clongdouble_from_list(self):
     import _cffi_backend
     LONGDOUBLE = _cffi_backend.new_primitive_type('long double')
     P_LONGDOUBLE = _cffi_backend.new_pointer_type(LONGDOUBLE)
     LONGDOUBLE_ARRAY = _cffi_backend.new_array_type(P_LONGDOUBLE, None)
     buf = _cffi_backend.newp(LONGDOUBLE_ARRAY, [1.25, -3.5])
     assert float(buf[0]) == 1.25
     assert float(buf[1]) == -3.5
Esempio n. 10
0
 def test_fast_init_cfloat_from_list(self):
     import _cffi_backend
     FLOAT = _cffi_backend.new_primitive_type('float')
     P_FLOAT = _cffi_backend.new_pointer_type(FLOAT)
     FLOAT_ARRAY = _cffi_backend.new_array_type(P_FLOAT, None)
     buf = _cffi_backend.newp(FLOAT_ARRAY, [1.25, -3.5])
     assert buf[0] == 1.25
     assert buf[1] == -3.5
Esempio n. 11
0
 def test_fast_init_from_list_float(self):
     import _cffi_backend
     DOUBLE = _cffi_backend.new_primitive_type('double')
     P_DOUBLE = _cffi_backend.new_pointer_type(DOUBLE)
     DOUBLE_ARRAY = _cffi_backend.new_array_type(P_DOUBLE, None)
     buf = _cffi_backend.newp(DOUBLE_ARRAY, [1.1, 2.2, 3.3])
     assert buf[0] == 1.1
     assert buf[1] == 2.2
     assert buf[2] == 3.3
Esempio n. 12
0
 def test_fast_init_from_list(self):
     import _cffi_backend
     LONG = _cffi_backend.new_primitive_type('long')
     P_LONG = _cffi_backend.new_pointer_type(LONG)
     LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, None)
     buf = _cffi_backend.newp(LONG_ARRAY, [1, 2, 3])
     assert buf[0] == 1
     assert buf[1] == 2
     assert buf[2] == 3
Esempio n. 13
0
 def test_fast_init_from_list_float(self):
     import _cffi_backend
     DOUBLE = _cffi_backend.new_primitive_type('double')
     P_DOUBLE = _cffi_backend.new_pointer_type(DOUBLE)
     DOUBLE_ARRAY = _cffi_backend.new_array_type(P_DOUBLE, None)
     buf = _cffi_backend.newp(DOUBLE_ARRAY, [1.1, 2.2, 3.3])
     assert buf[0] == 1.1
     assert buf[1] == 2.2
     assert buf[2] == 3.3
Esempio n. 14
0
 def test_fast_init_from_list(self):
     import _cffi_backend
     LONG = _cffi_backend.new_primitive_type('long')
     P_LONG = _cffi_backend.new_pointer_type(LONG)
     LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, None)
     buf = _cffi_backend.newp(LONG_ARRAY, [1, 2, 3])
     assert buf[0] == 1
     assert buf[1] == 2
     assert buf[2] == 3
Esempio n. 15
0
 def test_fast_init_bool_from_list(self):
     import _cffi_backend
     BOOL = _cffi_backend.new_primitive_type('_Bool')
     P_BOOL = _cffi_backend.new_pointer_type(BOOL)
     BOOL_ARRAY = _cffi_backend.new_array_type(P_BOOL, None)
     buf = _cffi_backend.newp(BOOL_ARRAY, [1, 0])
     assert buf[0] is True
     assert buf[1] is False
     raises(OverflowError, _cffi_backend.newp, BOOL_ARRAY, [2])
     raises(OverflowError, _cffi_backend.newp, BOOL_ARRAY, [-1])
Esempio n. 16
0
 def test_fast_init_ushort_from_list(self):
     import _cffi_backend
     USHORT = _cffi_backend.new_primitive_type('unsigned short')
     P_USHORT = _cffi_backend.new_pointer_type(USHORT)
     USHORT_ARRAY = _cffi_backend.new_array_type(P_USHORT, None)
     buf = _cffi_backend.newp(USHORT_ARRAY, [1, 2, 40000])
     assert buf[0] == 1
     assert buf[1] == 2
     assert buf[2] == 40000
     raises(OverflowError, _cffi_backend.newp, USHORT_ARRAY, [70000])
     raises(OverflowError, _cffi_backend.newp, USHORT_ARRAY, [-1])
Esempio n. 17
0
 def test_fast_init_short_from_list(self):
     import _cffi_backend
     SHORT = _cffi_backend.new_primitive_type('short')
     P_SHORT = _cffi_backend.new_pointer_type(SHORT)
     SHORT_ARRAY = _cffi_backend.new_array_type(P_SHORT, None)
     buf = _cffi_backend.newp(SHORT_ARRAY, [1, -2, 3])
     assert buf[0] == 1
     assert buf[1] == -2
     assert buf[2] == 3
     raises(OverflowError, _cffi_backend.newp, SHORT_ARRAY, [40000])
     raises(OverflowError, _cffi_backend.newp, SHORT_ARRAY, [-40000])
Esempio n. 18
0
 def test_fast_init_short_from_list(self):
     import _cffi_backend
     SHORT = _cffi_backend.new_primitive_type('short')
     P_SHORT = _cffi_backend.new_pointer_type(SHORT)
     SHORT_ARRAY = _cffi_backend.new_array_type(P_SHORT, None)
     buf = _cffi_backend.newp(SHORT_ARRAY, [1, -2, 3])
     assert buf[0] == 1
     assert buf[1] == -2
     assert buf[2] == 3
     raises(OverflowError, _cffi_backend.newp, SHORT_ARRAY, [40000])
     raises(OverflowError, _cffi_backend.newp, SHORT_ARRAY, [-40000])
Esempio n. 19
0
 def test_fast_init_bool_from_list(self):
     import _cffi_backend
     BOOL = _cffi_backend.new_primitive_type('_Bool')
     P_BOOL = _cffi_backend.new_pointer_type(BOOL)
     BOOL_ARRAY = _cffi_backend.new_array_type(P_BOOL, None)
     buf = _cffi_backend.newp(BOOL_ARRAY, [1, 0])
     assert buf[0] == 1
     assert buf[1] == 0
     assert type(buf[1]) is int
     raises(OverflowError, _cffi_backend.newp, BOOL_ARRAY, [2])
     raises(OverflowError, _cffi_backend.newp, BOOL_ARRAY, [-1])
Esempio n. 20
0
 def test_fast_init_ushort_from_list(self):
     import _cffi_backend
     USHORT = _cffi_backend.new_primitive_type('unsigned short')
     P_USHORT = _cffi_backend.new_pointer_type(USHORT)
     USHORT_ARRAY = _cffi_backend.new_array_type(P_USHORT, None)
     buf = _cffi_backend.newp(USHORT_ARRAY, [1, 2, 40000])
     assert buf[0] == 1
     assert buf[1] == 2
     assert buf[2] == 40000
     raises(OverflowError, _cffi_backend.newp, USHORT_ARRAY, [70000])
     raises(OverflowError, _cffi_backend.newp, USHORT_ARRAY, [-1])
Esempio n. 21
0
 def test_fast_init_longlong_from_list(self):
     import _cffi_backend
     import sys
     large_int = 2 ** (50 if sys.maxsize > 2**31 - 1 else 30)
     LONGLONG = _cffi_backend.new_primitive_type('long long')
     P_LONGLONG = _cffi_backend.new_pointer_type(LONGLONG)
     LONGLONG_ARRAY = _cffi_backend.new_array_type(P_LONGLONG, None)
     buf = _cffi_backend.newp(LONGLONG_ARRAY, [1, -2, 3, large_int])
     assert buf[0] == 1
     assert buf[1] == -2
     assert buf[2] == 3
     assert buf[3] == large_int
Esempio n. 22
0
 def test_fast_init_ulong_from_list(self):
     import sys
     import _cffi_backend
     ULONG = _cffi_backend.new_primitive_type('unsigned long')
     P_ULONG = _cffi_backend.new_pointer_type(ULONG)
     ULONG_ARRAY = _cffi_backend.new_array_type(P_ULONG, None)
     buf = _cffi_backend.newp(ULONG_ARRAY, [1, 2, sys.maxsize])
     assert buf[0] == 1
     assert buf[1] == 2
     assert buf[2] == sys.maxsize
     raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-1])
     raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-sys.maxsize])
Esempio n. 23
0
 def test_fast_init_ulong_from_list(self):
     import sys
     import _cffi_backend
     ULONG = _cffi_backend.new_primitive_type('unsigned long')
     P_ULONG = _cffi_backend.new_pointer_type(ULONG)
     ULONG_ARRAY = _cffi_backend.new_array_type(P_ULONG, None)
     buf = _cffi_backend.newp(ULONG_ARRAY, [1, 2, sys.maxint])
     assert buf[0] == 1
     assert buf[1] == 2
     assert buf[2] == sys.maxint
     raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-1])
     raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-sys.maxint])
Esempio n. 24
0
 def test_fast_init_longlong_from_list(self):
     import _cffi_backend
     import sys
     large_int = 2**(50 if sys.maxsize > 2**31 - 1 else 30)
     LONGLONG = _cffi_backend.new_primitive_type('long long')
     P_LONGLONG = _cffi_backend.new_pointer_type(LONGLONG)
     LONGLONG_ARRAY = _cffi_backend.new_array_type(P_LONGLONG, None)
     buf = _cffi_backend.newp(LONGLONG_ARRAY, [1, -2, 3, large_int])
     assert buf[0] == 1
     assert buf[1] == -2
     assert buf[2] == 3
     assert buf[3] == large_int
Esempio n. 25
0
 def test_list_float(self):
     import _cffi_backend
     DOUBLE = _cffi_backend.new_primitive_type('double')
     P_DOUBLE = _cffi_backend.new_pointer_type(DOUBLE)
     DOUBLE_ARRAY = _cffi_backend.new_array_type(P_DOUBLE, 3)
     buf = _cffi_backend.newp(DOUBLE_ARRAY)
     buf[0] = 1.1
     buf[1] = 2.2
     buf[2] = 3.3
     lst = list(buf)
     assert lst == [1.1, 2.2, 3.3]
     if not self.runappdirect:
         assert self.get_count() == 1
Esempio n. 26
0
 def test_list_cfloat(self):
     import _cffi_backend
     FLOAT = _cffi_backend.new_primitive_type('float')
     P_FLOAT = _cffi_backend.new_pointer_type(FLOAT)
     FLOAT_ARRAY = _cffi_backend.new_array_type(P_FLOAT, 3)
     buf = _cffi_backend.newp(FLOAT_ARRAY)
     buf[0] = 1.25
     buf[1] = -2.5
     buf[2] = 3.75
     lst = list(buf)
     assert lst == [1.25, -2.5, 3.75]
     if not self.runappdirect:
         assert self.get_count() == 1
Esempio n. 27
0
 def test_list_float(self):
     import _cffi_backend
     DOUBLE = _cffi_backend.new_primitive_type('double')
     P_DOUBLE = _cffi_backend.new_pointer_type(DOUBLE)
     DOUBLE_ARRAY = _cffi_backend.new_array_type(P_DOUBLE, 3)
     buf = _cffi_backend.newp(DOUBLE_ARRAY)
     buf[0] = 1.1
     buf[1] = 2.2
     buf[2] = 3.3
     lst = list(buf)
     assert lst == [1.1, 2.2, 3.3]
     if not self.runappdirect:
         assert self.get_count() == 1
Esempio n. 28
0
 def test_list_ushort(self):
     import _cffi_backend
     USHORT = _cffi_backend.new_primitive_type('unsigned short')
     P_USHORT = _cffi_backend.new_pointer_type(USHORT)
     USHORT_ARRAY = _cffi_backend.new_array_type(P_USHORT, 3)
     buf = _cffi_backend.newp(USHORT_ARRAY)
     buf[0] = 1
     buf[1] = 2
     buf[2] = 50505
     lst = list(buf)
     assert lst == [1, 2, 50505]
     if not self.runappdirect:
         assert self.get_count() == 1
Esempio n. 29
0
 def test_list_cfloat(self):
     import _cffi_backend
     FLOAT = _cffi_backend.new_primitive_type('float')
     P_FLOAT = _cffi_backend.new_pointer_type(FLOAT)
     FLOAT_ARRAY = _cffi_backend.new_array_type(P_FLOAT, 3)
     buf = _cffi_backend.newp(FLOAT_ARRAY)
     buf[0] = 1.25
     buf[1] = -2.5
     buf[2] = 3.75
     lst = list(buf)
     assert lst == [1.25, -2.5, 3.75]
     if not self.runappdirect:
         assert self.get_count() == 1
Esempio n. 30
0
 def test_list_ushort(self):
     import _cffi_backend
     USHORT = _cffi_backend.new_primitive_type('unsigned short')
     P_USHORT = _cffi_backend.new_pointer_type(USHORT)
     USHORT_ARRAY = _cffi_backend.new_array_type(P_USHORT, 3)
     buf = _cffi_backend.newp(USHORT_ARRAY)
     buf[0] = 1
     buf[1] = 2
     buf[2] = 50505
     lst = list(buf)
     assert lst == [1, 2, 50505]
     if not self.runappdirect:
         assert self.get_count() == 1
Esempio n. 31
0
 def test_list_int(self):
     import _cffi_backend
     LONG = _cffi_backend.new_primitive_type('long')
     P_LONG = _cffi_backend.new_pointer_type(LONG)
     LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, 3)
     buf = _cffi_backend.newp(LONG_ARRAY)
     buf[0] = 1
     buf[1] = 2
     buf[2] = 3
     lst = list(buf)
     assert lst == [1, 2, 3]
     if not self.runappdirect:
         assert self.get_count() == 1
Esempio n. 32
0
 def test_list_int(self):
     import _cffi_backend
     LONG = _cffi_backend.new_primitive_type('long')
     P_LONG = _cffi_backend.new_pointer_type(LONG)
     LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, 3)
     buf = _cffi_backend.newp(LONG_ARRAY)
     buf[0] = 1
     buf[1] = 2
     buf[2] = 3
     lst = list(buf)
     assert lst == [1, 2, 3]
     if not self.runappdirect:
         assert self.get_count() == 1
Esempio n. 33
0
 def test_list_short(self):
     import _cffi_backend
     SHORT = _cffi_backend.new_primitive_type('short')
     P_SHORT = _cffi_backend.new_pointer_type(SHORT)
     SHORT_ARRAY = _cffi_backend.new_array_type(P_SHORT, 3)
     buf = _cffi_backend.newp(SHORT_ARRAY)
     buf[0] = 1
     buf[1] = 2
     buf[2] = 3
     lst = list(buf)
     assert lst == [1, 2, 3]
     if not self.runappdirect:
         assert self.get_count() == 1
Esempio n. 34
0
 def test_list_short(self):
     import _cffi_backend
     SHORT = _cffi_backend.new_primitive_type('short')
     P_SHORT = _cffi_backend.new_pointer_type(SHORT)
     SHORT_ARRAY = _cffi_backend.new_array_type(P_SHORT, 3)
     buf = _cffi_backend.newp(SHORT_ARRAY)
     buf[0] = 1
     buf[1] = 2
     buf[2] = 3
     lst = list(buf)
     assert lst == [1, 2, 3]
     if not self.runappdirect:
         assert self.get_count() == 1
Esempio n. 35
0
 def test_fast_init_ulong_from_list(self):
     import sys
     import _cffi_backend
     maxlong = sys.maxint
     if sys.platform == 'win32':
         # maxlong == 2**31-1 < sys.maxint == 2**63-1 on win64!
         maxlong = int(2**31 - 1)
     ULONG = _cffi_backend.new_primitive_type('unsigned long')
     P_ULONG = _cffi_backend.new_pointer_type(ULONG)
     ULONG_ARRAY = _cffi_backend.new_array_type(P_ULONG, None)
     buf = _cffi_backend.newp(ULONG_ARRAY, [1, 2, maxlong])
     assert buf[0] == 1
     assert buf[1] == 2
     assert buf[2] == maxlong
     raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-1])
     raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-maxlong])
Esempio n. 36
0
 def test_AsWriteBuffer(self):
     import array
     module = self.import_extension('buffer', [
         ('write_buffer_len', 'METH_O',
          """
          void* buf;
          Py_ssize_t buf_len;
          if (PyObject_AsWriteBuffer(args, &buf, &buf_len) < 0) {
             //PyErr_SetString(PyExc_ValueError, "bad value");
             return NULL;
          }
          return PyLong_FromLong(buf_len);
          """)])
     assert module.write_buffer_len(bytearray(b'123')) == 3
     assert module.write_buffer_len(array.array('i', [1, 2, 3])) == 12
     #
     import _cffi_backend
     BChar = _cffi_backend.new_primitive_type("char")
     BCharPtr = _cffi_backend.new_pointer_type(BChar)
     BCharArray = _cffi_backend.new_array_type(BCharPtr, None)
     p = _cffi_backend.newp(BCharArray, b"abcde")
     bb = _cffi_backend.buffer(p)
     assert module.write_buffer_len(bb) == 6