summaryrefslogtreecommitdiff
path: root/tests/test_pycapi.py
blob: 969b78e57544ba17ece6947ec576cc2540ff7cdd (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
from __future__ import absolute_import

import sys
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

    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
    if sys.version_info[0] == 2:
        func_type = ctypes.PYFUNCTYPE(c_void_p, py_object)
        PyCObject_AsVoidPtr = func_type(
            ('PyCObject_AsVoidPtr', ctypes.pythonapi))
        ptr = PyCObject_AsVoidPtr(api_obj)
    else:
        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