Example #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])])
Example #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])])
Example #3
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
Example #4
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
Example #5
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)")
Example #6
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
Example #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)")
Example #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
Example #9
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
Example #10
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
Example #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
Example #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
Example #13
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])
Example #14
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])
Example #15
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])
Example #16
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])
Example #17
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])
Example #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])
Example #19
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
Example #20
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])
Example #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
Example #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.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])
Example #23
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
Example #24
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
Example #25
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
Example #26
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
Example #27
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
Example #28
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
Example #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
Example #30
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
Example #31
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
Example #32
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
Example #33
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])
Example #34
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