1 | n/a | from ctypes import * |
---|
2 | n/a | import unittest, sys |
---|
3 | n/a | from test import support |
---|
4 | n/a | |
---|
5 | n/a | ################################################################ |
---|
6 | n/a | # This section should be moved into ctypes\__init__.py, when it's ready. |
---|
7 | n/a | |
---|
8 | n/a | from _ctypes import PyObj_FromPtr |
---|
9 | n/a | |
---|
10 | n/a | ################################################################ |
---|
11 | n/a | |
---|
12 | n/a | from sys import getrefcount as grc |
---|
13 | n/a | if sys.version_info > (2, 4): |
---|
14 | n/a | c_py_ssize_t = c_size_t |
---|
15 | n/a | else: |
---|
16 | n/a | c_py_ssize_t = c_int |
---|
17 | n/a | |
---|
18 | n/a | class PythonAPITestCase(unittest.TestCase): |
---|
19 | n/a | |
---|
20 | n/a | def test_PyBytes_FromStringAndSize(self): |
---|
21 | n/a | PyBytes_FromStringAndSize = pythonapi.PyBytes_FromStringAndSize |
---|
22 | n/a | |
---|
23 | n/a | PyBytes_FromStringAndSize.restype = py_object |
---|
24 | n/a | PyBytes_FromStringAndSize.argtypes = c_char_p, c_py_ssize_t |
---|
25 | n/a | |
---|
26 | n/a | self.assertEqual(PyBytes_FromStringAndSize(b"abcdefghi", 3), b"abc") |
---|
27 | n/a | |
---|
28 | n/a | @support.refcount_test |
---|
29 | n/a | def test_PyString_FromString(self): |
---|
30 | n/a | pythonapi.PyBytes_FromString.restype = py_object |
---|
31 | n/a | pythonapi.PyBytes_FromString.argtypes = (c_char_p,) |
---|
32 | n/a | |
---|
33 | n/a | s = b"abc" |
---|
34 | n/a | refcnt = grc(s) |
---|
35 | n/a | pyob = pythonapi.PyBytes_FromString(s) |
---|
36 | n/a | self.assertEqual(grc(s), refcnt) |
---|
37 | n/a | self.assertEqual(s, pyob) |
---|
38 | n/a | del pyob |
---|
39 | n/a | self.assertEqual(grc(s), refcnt) |
---|
40 | n/a | |
---|
41 | n/a | @support.refcount_test |
---|
42 | n/a | def test_PyLong_Long(self): |
---|
43 | n/a | ref42 = grc(42) |
---|
44 | n/a | pythonapi.PyLong_FromLong.restype = py_object |
---|
45 | n/a | self.assertEqual(pythonapi.PyLong_FromLong(42), 42) |
---|
46 | n/a | |
---|
47 | n/a | self.assertEqual(grc(42), ref42) |
---|
48 | n/a | |
---|
49 | n/a | pythonapi.PyLong_AsLong.argtypes = (py_object,) |
---|
50 | n/a | pythonapi.PyLong_AsLong.restype = c_long |
---|
51 | n/a | |
---|
52 | n/a | res = pythonapi.PyLong_AsLong(42) |
---|
53 | n/a | self.assertEqual(grc(res), ref42 + 1) |
---|
54 | n/a | del res |
---|
55 | n/a | self.assertEqual(grc(42), ref42) |
---|
56 | n/a | |
---|
57 | n/a | @support.refcount_test |
---|
58 | n/a | def test_PyObj_FromPtr(self): |
---|
59 | n/a | s = "abc def ghi jkl" |
---|
60 | n/a | ref = grc(s) |
---|
61 | n/a | # id(python-object) is the address |
---|
62 | n/a | pyobj = PyObj_FromPtr(id(s)) |
---|
63 | n/a | self.assertIs(s, pyobj) |
---|
64 | n/a | |
---|
65 | n/a | self.assertEqual(grc(s), ref + 1) |
---|
66 | n/a | del pyobj |
---|
67 | n/a | self.assertEqual(grc(s), ref) |
---|
68 | n/a | |
---|
69 | n/a | def test_PyOS_snprintf(self): |
---|
70 | n/a | PyOS_snprintf = pythonapi.PyOS_snprintf |
---|
71 | n/a | PyOS_snprintf.argtypes = POINTER(c_char), c_size_t, c_char_p |
---|
72 | n/a | |
---|
73 | n/a | buf = c_buffer(256) |
---|
74 | n/a | PyOS_snprintf(buf, sizeof(buf), b"Hello from %s", b"ctypes") |
---|
75 | n/a | self.assertEqual(buf.value, b"Hello from ctypes") |
---|
76 | n/a | |
---|
77 | n/a | PyOS_snprintf(buf, sizeof(buf), b"Hello from %s (%d, %d, %d)", b"ctypes", 1, 2, 3) |
---|
78 | n/a | self.assertEqual(buf.value, b"Hello from ctypes (1, 2, 3)") |
---|
79 | n/a | |
---|
80 | n/a | # not enough arguments |
---|
81 | n/a | self.assertRaises(TypeError, PyOS_snprintf, buf) |
---|
82 | n/a | |
---|
83 | n/a | def test_pyobject_repr(self): |
---|
84 | n/a | self.assertEqual(repr(py_object()), "py_object(<NULL>)") |
---|
85 | n/a | self.assertEqual(repr(py_object(42)), "py_object(42)") |
---|
86 | n/a | self.assertEqual(repr(py_object(object)), "py_object(%r)" % object) |
---|
87 | n/a | |
---|
88 | n/a | if __name__ == "__main__": |
---|
89 | n/a | unittest.main() |
---|