Debian Codegraph @

tests/test_pycapi.py

raw
1 import unittest
2 import ctypes
3 from ctypes import c_void_p, py_object, c_char_p
4
5 import gi
6 from gi.repository import Gio
7
8
9 def get_capi():
10 if not hasattr(ctypes, "pythonapi"):
11 return None
12
13 class CAPI(ctypes.Structure):
14 _fields_ = [
15 ("", c_void_p),
16 ("", c_void_p),
17 ("", c_void_p),
18 ("newgobj", ctypes.PYFUNCTYPE(py_object, c_void_p)),
19 ]
20
21 api_obj = gi._gobject._PyGObject_API
22 func_type = ctypes.PYFUNCTYPE(c_void_p, py_object, c_char_p)
23 PyCapsule_GetPointer = func_type(("PyCapsule_GetPointer", ctypes.pythonapi))
24 ptr = PyCapsule_GetPointer(api_obj, b"gobject._PyGObject_API")
25
26 ptr = ctypes.cast(ptr, ctypes.POINTER(CAPI))
27 return ptr.contents
28
29
30 API = get_capi()
31
32
33 @unittest.skipUnless(API, "no pythonapi support")
34 class TestPythonCAPI(unittest.TestCase):
35 def test_newgobj(self):
36 w = Gio.FileInfo()
37 # XXX: ugh :/
38 ptr = int(repr(w).split()[-1].split(")")[0], 16)
39
40 capi = get_capi()
41 new_w = capi.newgobj(ptr)
42 assert w == new_w