ยปCore Development>Code coverage>Lib/ctypes/test/test_python_api.py

Python code coverage for Lib/ctypes/test/test_python_api.py

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