File: test_pycapi.py

package info (click to toggle)
pygobject 3.54.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,864 kB
  • sloc: ansic: 40,281; python: 26,363; sh: 477; makefile: 81; xml: 35; cpp: 1
file content (42 lines) | stat: -rw-r--r-- 1,076 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import unittest
import ctypes
from ctypes import c_void_p, py_object, c_char_p

import gi
from gi.repository import Gio


def get_capi():
    if not hasattr(ctypes, "pythonapi"):
        return None

    class CAPI(ctypes.Structure):
        _fields_ = [
            ("", c_void_p),
            ("", c_void_p),
            ("", c_void_p),
            ("newgobj", ctypes.PYFUNCTYPE(py_object, c_void_p)),
        ]

    api_obj = gi._gobject._PyGObject_API
    func_type = ctypes.PYFUNCTYPE(c_void_p, py_object, c_char_p)
    PyCapsule_GetPointer = func_type(("PyCapsule_GetPointer", ctypes.pythonapi))
    ptr = PyCapsule_GetPointer(api_obj, b"gobject._PyGObject_API")

    ptr = ctypes.cast(ptr, ctypes.POINTER(CAPI))
    return ptr.contents


API = get_capi()


@unittest.skipUnless(API, "no pythonapi support")
class TestPythonCAPI(unittest.TestCase):
    def test_newgobj(self):
        w = Gio.FileInfo()
        # XXX: ugh :/
        ptr = int(repr(w).split()[-1].split(")")[0], 16)

        capi = get_capi()
        new_w = capi.newgobj(ptr)
        assert w == new_w