Back to index

python3.2  3.2.2
Classes | Functions
ctypes.test.test_memfunctions Namespace Reference

Classes

class  MemFunctionsTest

Functions

def test_memmove
 def test_overflow(self):

string_at and wstring_at must use the Python calling

convention (which acquires the GIL and checks the Python

error flag).


def test_memset
def test_cast
def test_string_at
def test_wstring_at

Class Documentation

class ctypes::test::test_memfunctions::MemFunctionsTest

Definition at line 5 of file test_memfunctions.py.


Function Documentation

Definition at line 38 of file test_memfunctions.py.

00038 
00039     def test_cast(self):
00040         a = (c_ubyte * 32)(*map(ord, "abcdef"))
00041         self.assertEqual(cast(a, c_char_p).value, b"abcdef")
00042         self.assertEqual(cast(a, POINTER(c_byte))[:7],
00043                              [97, 98, 99, 100, 101, 102, 0])
00044         self.assertEqual(cast(a, POINTER(c_byte))[:7:],
00045                              [97, 98, 99, 100, 101, 102, 0])
00046         self.assertEqual(cast(a, POINTER(c_byte))[6:-1:-1],
00047                              [0, 102, 101, 100, 99, 98, 97])
00048         self.assertEqual(cast(a, POINTER(c_byte))[:7:2],
00049                              [97, 99, 101, 0])
00050         self.assertEqual(cast(a, POINTER(c_byte))[:7:7],
00051                              [97])

Here is the call graph for this function:

def test_overflow(self):

string_at and wstring_at must use the Python calling

convention (which acquires the GIL and checks the Python

error flag).

Provoke an error and catch it; see also issue

#3554: http://bugs.python.org/issue3554

self.assertRaises((OverflowError, MemoryError, SystemError), lambda: wstring_at(u"foo", sys.maxint - 1)) self.assertRaises((OverflowError, MemoryError, SystemError), lambda: string_at("foo", sys.maxint - 1))

Definition at line 16 of file test_memfunctions.py.

00016 
00017     def test_memmove(self):
00018         # large buffers apparently increase the chance that the memory
00019         # is allocated in high address space.
00020         a = create_string_buffer(1000000)
00021         p = b"Hello, World"
00022         result = memmove(a, p, len(p))
00023         self.assertEqual(a.value, b"Hello, World")
00024 
00025         self.assertEqual(string_at(result), b"Hello, World")
00026         self.assertEqual(string_at(result, 5), b"Hello")
00027         self.assertEqual(string_at(result, 16), b"Hello, World\0\0\0\0")
00028         self.assertEqual(string_at(result, 0), b"")

Here is the call graph for this function:

Definition at line 29 of file test_memfunctions.py.

00029 
00030     def test_memset(self):
00031         a = create_string_buffer(1000000)
00032         result = memset(a, ord('x'), 16)
00033         self.assertEqual(a.value, b"xxxxxxxxxxxxxxxx")
00034 
00035         self.assertEqual(string_at(result), b"xxxxxxxxxxxxxxxx")
00036         self.assertEqual(string_at(a), b"xxxxxxxxxxxxxxxx")
00037         self.assertEqual(string_at(a, 20), b"xxxxxxxxxxxxxxxx\0\0\0\0")

Here is the call graph for this function:

Definition at line 52 of file test_memfunctions.py.

00052 
00053     def test_string_at(self):
00054         s = string_at(b"foo bar")
00055         # XXX The following may be wrong, depending on how Python
00056         # manages string instances
00057         self.assertEqual(2, sys.getrefcount(s))
00058         self.assertTrue(s, "foo bar")
00059 
00060         self.assertEqual(string_at(b"foo bar", 7), b"foo bar")
00061         self.assertEqual(string_at(b"foo bar", 3), b"foo")

Here is the call graph for this function:

Definition at line 67 of file test_memfunctions.py.

00067 
00068         def test_wstring_at(self):
00069             p = create_unicode_buffer("Hello, World")
00070             a = create_unicode_buffer(1000000)
00071             result = memmove(a, p, len(p) * sizeof(c_wchar))
00072             self.assertEqual(a.value, "Hello, World")
00073 
00074             self.assertEqual(wstring_at(a), "Hello, World")
00075             self.assertEqual(wstring_at(a, 5), "Hello")
00076             self.assertEqual(wstring_at(a, 16), "Hello, World\0\0\0\0")
00077             self.assertEqual(wstring_at(a, 0), "")

Here is the call graph for this function: