PATH:
opt
/
bitninja-python-dojo
/
embedded
/
lib
/
python3.9
/
ctypes
/
test
import unittest from test import support from ctypes import * import _ctypes_test lib = CDLL(_ctypes_test.__file__) class StringPtrTestCase(unittest.TestCase): @support.refcount_test def test__POINTER_c_char(self): class X(Structure): _fields_ = [("str", POINTER(c_char))] x = X() # NULL pointer access self.assertRaises(ValueError, getattr, x.str, "contents") b = c_buffer(b"Hello, World") from sys import getrefcount as grc self.assertEqual(grc(b), 2) x.str = b self.assertEqual(grc(b), 3) # POINTER(c_char) and Python string is NOT compatible # POINTER(c_char) and c_buffer() is compatible for i in range(len(b)): self.assertEqual(b[i], x.str[i]) self.assertRaises(TypeError, setattr, x, "str", "Hello, World") def test__c_char_p(self): class X(Structure): _fields_ = [("str", c_char_p)] x = X() # c_char_p and Python string is compatible # c_char_p and c_buffer is NOT compatible self.assertEqual(x.str, None) x.str = b"Hello, World" self.assertEqual(x.str, b"Hello, World") b = c_buffer(b"Hello, World") self.assertRaises(TypeError, setattr, x, b"str", b) def test_functions(self): strchr = lib.my_strchr strchr.restype = c_char_p # c_char_p and Python string is compatible # c_char_p and c_buffer are now compatible strchr.argtypes = c_char_p, c_char self.assertEqual(strchr(b"abcdef", b"c"), b"cdef") self.assertEqual(strchr(c_buffer(b"abcdef"), b"c"), b"cdef") # POINTER(c_char) and Python string is NOT compatible # POINTER(c_char) and c_buffer() is compatible strchr.argtypes = POINTER(c_char), c_char buf = c_buffer(b"abcdef") self.assertEqual(strchr(buf, b"c"), b"cdef") self.assertEqual(strchr(b"abcdef", b"c"), b"cdef") # XXX These calls are dangerous, because the first argument # to strchr is no longer valid after the function returns! # So we must keep a reference to buf separately strchr.restype = POINTER(c_char) buf = c_buffer(b"abcdef") r = strchr(buf, b"c") x = r[0], r[1], r[2], r[3], r[4] self.assertEqual(x, (b"c", b"d", b"e", b"f", b"\000")) del buf # Because r is a pointer to memory that is freed after deleting buf, # the pointer is hanging and using it would reference freed memory. if __name__ == '__main__': unittest.main()
[-] test_win32.py
[edit]
[-] test_bitfields.py
[edit]
[-] test_libc.py
[edit]
[-] test_array_in_pointer.py
[edit]
[-] test_varsize_struct.py
[edit]
[-] test_arrays.py
[edit]
[-] test_random_things.py
[edit]
[-] test_callbacks.py
[edit]
[-] test_init.py
[edit]
[-] test_numbers.py
[edit]
[-] test_stringptr.py
[edit]
[-] test_sizes.py
[edit]
[-] test_internals.py
[edit]
[+]
..
[-] test_errno.py
[edit]
[-] test_wintypes.py
[edit]
[-] test_struct_fields.py
[edit]
[-] test_unicode.py
[edit]
[-] test_checkretval.py
[edit]
[-] test_bytes.py
[edit]
[-] test_returnfuncptrs.py
[edit]
[-] test_python_api.py
[edit]
[-] test_parameters.py
[edit]
[-] __main__.py
[edit]
[-] test_strings.py
[edit]
[-] test_values.py
[edit]
[-] test_pointers.py
[edit]
[-] test_repr.py
[edit]
[-] test_delattr.py
[edit]
[-] test_pep3118.py
[edit]
[-] test_buffers.py
[edit]
[-] test_objects.py
[edit]
[+]
__pycache__
[-] test_cast.py
[edit]
[-] test_prototypes.py
[edit]
[-] test_keeprefs.py
[edit]
[-] test_incomplete.py
[edit]
[-] test_slicing.py
[edit]
[-] test_cfuncs.py
[edit]
[-] test_structures.py
[edit]
[-] test_funcptr.py
[edit]
[-] test_refcounts.py
[edit]
[-] test_byteswap.py
[edit]
[-] test_unaligned_structures.py
[edit]
[-] test_loading.py
[edit]
[-] test_frombuffer.py
[edit]
[-] test_anon.py
[edit]
[-] test_macholib.py
[edit]
[-] test_as_parameter.py
[edit]
[-] test_find.py
[edit]
[-] __init__.py
[edit]
[-] test_functions.py
[edit]
[-] test_pickling.py
[edit]
[-] test_memfunctions.py
[edit]
[-] test_simplesubclasses.py
[edit]