def test_returning_str(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) char_check = lib.ptr('char_check', ['c', 'c'], 's') A = _rawffi.Array('c') arg1 = A(1) arg2 = A(1) arg1[0] = 'y' arg2[0] = 'x' res = char_check(arg1, arg2) assert _rawffi.charp2string(res[0]) == 'xxxxxx' assert _rawffi.charp2rawstring(res[0]) == 'xxxxxx' assert _rawffi.charp2rawstring(res[0], 3) == 'xxx' a = A(6, 'xx\x00\x00xx') assert _rawffi.charp2string(a.buffer) == 'xx' assert _rawffi.charp2rawstring(a.buffer, 4) == 'xx\x00\x00' res.free() arg1[0] = 'x' arg2[0] = 'y' res = char_check(arg1, arg2) assert res[0] == 0 assert _rawffi.charp2string(res[0]) is None res.free() arg1.free() arg2.free()
def test_buffer_info(self): a = self.array('c', 'Hi!') bi = a.buffer_info() assert bi[0] != 0 assert bi[1] == 3 import _rawffi data = _rawffi.charp2string(bi[0]) assert data[0:3] == 'Hi!'
def test_array_autofree(self): import gc, _rawffi oldnum = _rawffi._num_of_allocated_objects() A = _rawffi.Array('c') a = A(6, 'xxyxx\x00', autofree=True) assert _rawffi.charp2string(a.buffer) == 'xxyxx' a = None gc.collect() assert oldnum == _rawffi._num_of_allocated_objects()
def test_array_autofree(self): import gc, _rawffi gc.collect() oldnum = _rawffi._num_of_allocated_objects() A = _rawffi.Array('c') a = A(6, 'xxyxx\x00', autofree=True) assert _rawffi.charp2string(a.buffer) == 'xxyxx' a = None gc.collect() assert oldnum == _rawffi._num_of_allocated_objects()
def _CData_output(self, resarray, base=None, index=-1): # this seems to be a string if we're array of char, surprise! from ctypes import c_char, c_wchar if self._type_ is c_char: return _rawffi.charp2string(resarray.buffer, self._length_) if self._type_ is c_wchar: return _rawffi.wcharp2unicode(resarray.buffer, self._length_) res = self.__new__(self) ffiarray = self._ffiarray.fromaddress(resarray.buffer, self._length_) res._buffer = ffiarray res._base = base res._index = index return res
def test_returning_str(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) char_check = lib.ptr('char_check', ['c', 'c'], 's') A = _rawffi.Array('c') arg1 = A(1) arg2 = A(1) arg1[0] = 'y' arg2[0] = 'x' res = char_check(arg1, arg2) assert _rawffi.charp2string(res[0]) == 'xxxxxx' assert _rawffi.charp2rawstring(res[0]) == 'xxxxxx' assert _rawffi.charp2rawstring(res[0], 3) == 'xxx' a = A(6, 'xx\x00\x00xx') assert _rawffi.charp2string(a.buffer) == 'xx' assert _rawffi.charp2rawstring(a.buffer, 4) == 'xx\x00\x00' arg1[0] = 'x' arg2[0] = 'y' res = char_check(arg1, arg2) assert res[0] == 0 assert _rawffi.charp2string(res[0]) is None arg1.free() arg2.free()
def test_array_autofree(self): import gc, _rawffi gc.collect() try: oldnum = _rawffi._num_of_allocated_objects() except RuntimeError: oldnum = '?' A = _rawffi.Array('c') a = A(6, 'xxyxx\x00', autofree=True) assert _rawffi.charp2string(a.buffer) == 'xxyxx' a = None gc.collect() if oldnum != '?': assert oldnum == _rawffi._num_of_allocated_objects()
def _CData_output(self, resarray, base=None, index=-1): from _rawffi.alt import types # If a char_p or unichar_p is received, skip the string interpretation if base._ffiargtype != types.Pointer(types.char_p) and \ base._ffiargtype != types.Pointer(types.unichar_p): # this seems to be a string if we're array of char, surprise! from ctypes import c_char, c_wchar if self._type_ is c_char: return _rawffi.charp2string(resarray.buffer, self._length_) if self._type_ is c_wchar: return _rawffi.wcharp2unicode(resarray.buffer, self._length_) res = self.__new__(self) ffiarray = self._ffiarray.fromaddress(resarray.buffer, self._length_) res._buffer = ffiarray if base is not None: res._base = base res._index = index return res
def _getvalue(self): addr = self._buffer[0] if addr == 0: return None else: return _rawffi.charp2string(addr)
def getvalue(self): return _rawffi.charp2string(self._buffer.buffer, self._length_)