diff options
author | Christoph Reiter <reiter.christoph@gmail.com> | 2018-04-13 21:27:18 +0200 |
---|---|---|
committer | Christoph Reiter <reiter.christoph@gmail.com> | 2018-04-13 21:27:18 +0200 |
commit | 69a904e8143a9990f64aa9f83f42225239761a52 (patch) | |
tree | 935582fbb604f2a5c6301cde1183da2eef5328ea | |
parent | 064a9baef0c9df7ae0cf3cace6d405bf78b01241 (diff) | |
download | pygobject-69a904e8143a9990f64aa9f83f42225239761a52.tar.gz |
tests: add a test for accessing the C API through ctypes
For some API it might be easier to test things through Python and
I had this lying around so why not.
-rw-r--r-- | tests/test_pycapi.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/test_pycapi.py b/tests/test_pycapi.py new file mode 100644 index 00000000..969b78e5 --- /dev/null +++ b/tests/test_pycapi.py @@ -0,0 +1,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 |