Back to index

python3.2  3.2.2
Public Member Functions
ctypes.test.test_stringptr.StringPtrTestCase Class Reference

List of all members.

Public Member Functions

def test__POINTER_c_char
def test__c_char_p
def test_functions

Detailed Description

Definition at line 8 of file test_stringptr.py.


Member Function Documentation

Definition at line 30 of file test_stringptr.py.

00030 
00031     def test__c_char_p(self):
00032         class X(Structure):
00033             _fields_ = [("str", c_char_p)]
00034         x = X()
00035 
00036         # c_char_p and Python string is compatible
00037         # c_char_p and c_buffer is NOT compatible
00038         self.assertEqual(x.str, None)
00039         x.str = b"Hello, World"
00040         self.assertEqual(x.str, b"Hello, World")
00041         b = c_buffer(b"Hello, World")
00042         self.assertRaises(TypeError, setattr, x, b"str", b)
00043 

Here is the call graph for this function:

Definition at line 10 of file test_stringptr.py.

00010 
00011     def test__POINTER_c_char(self):
00012         class X(Structure):
00013             _fields_ = [("str", POINTER(c_char))]
00014         x = X()
00015 
00016         # NULL pointer access
00017         self.assertRaises(ValueError, getattr, x.str, "contents")
00018         b = c_buffer(b"Hello, World")
00019         from sys import getrefcount as grc
00020         self.assertEqual(grc(b), 2)
00021         x.str = b
00022         self.assertEqual(grc(b), 3)
00023 
00024         # POINTER(c_char) and Python string is NOT compatible
00025         # POINTER(c_char) and c_buffer() is compatible
00026         for i in range(len(b)):
00027             self.assertEqual(b[i], x.str[i])
00028 
00029         self.assertRaises(TypeError, setattr, x, "str", "Hello, World")

Here is the call graph for this function:

Definition at line 44 of file test_stringptr.py.

00044 
00045     def test_functions(self):
00046         strchr = lib.my_strchr
00047         strchr.restype = c_char_p
00048 
00049         # c_char_p and Python string is compatible
00050         # c_char_p and c_buffer are now compatible
00051         strchr.argtypes = c_char_p, c_char
00052         self.assertEqual(strchr(b"abcdef", b"c"), b"cdef")
00053         self.assertEqual(strchr(c_buffer(b"abcdef"), b"c"), b"cdef")
00054 
00055         # POINTER(c_char) and Python string is NOT compatible
00056         # POINTER(c_char) and c_buffer() is compatible
00057         strchr.argtypes = POINTER(c_char), c_char
00058         buf = c_buffer(b"abcdef")
00059         self.assertEqual(strchr(buf, b"c"), b"cdef")
00060         self.assertEqual(strchr(b"abcdef", b"c"), b"cdef")
00061 
00062         # XXX These calls are dangerous, because the first argument
00063         # to strchr is no longer valid after the function returns!
00064         # So we must keep a reference to buf separately
00065 
00066         strchr.restype = POINTER(c_char)
00067         buf = c_buffer(b"abcdef")
00068         r = strchr(buf, b"c")
00069         x = r[0], r[1], r[2], r[3], r[4]
00070         self.assertEqual(x, (b"c", b"d", b"e", b"f", b"\000"))
00071         del buf
00072         # x1 will NOT be the same as x, usually:
00073         x1 = r[0], r[1], r[2], r[3], r[4]

Here is the call graph for this function:


The documentation for this class was generated from the following file: