Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197799

Re: PyObjects From Python Via ctypes

From Lawrence D’Oliveiro <ldo@nz.invalid>
Newsgroups comp.lang.python
Subject Re: PyObjects From Python Via ctypes
Date 2026-06-11 07:44 +0000
Organization A noiseless patient Spider
Message-ID <110dp0f$19tv7$2@dont-email.me> (permalink)
References <110doqv$19tv7$1@dont-email.me>

Show all headers | View raw


On Thu, 11 Jun 2026 07:41:20 -0000 (UTC), I wrote:

> I see ctypes has a py_object type, but you can’t seem to do much
> with it. The following might be more useful:

Example use: accessing a more powerful Cairo wrapper in a context
where the GUI toolkit wrapper (e.g. PyGObject for GTK) wants to
give you only a pycairo object.

----
import ctypes as ct
import qahirah as qah
from pyobjhack import PyObject

class PyCairo_Context(ct.Structure) :
    "extract cairo_t pointer from pycairo.Context object."
    _fields_ = \
        [
            ("header", PyObject),
            ("ctx", ct.c_void_p),
        ]

    @classmethod
    def get_ctx(celf, pyctx) :
        return \
            ct.c_void_p(celf.from_address(id(pyctx)).ctx).value
    #end get_ctx

#end PyCairo_Context

def pycairo_to_qah(ctx) :
    "returns a qah.Context object wrapping the cairo_t pointer from" \
    " a given cairo.Context object."
    gx = PyCairo_Context.get_ctx(ctx)
    qah.cairo.cairo_reference(gx) # grab one for myself
    return \
        qah.Context(gx)
#end pycairo_to_qah

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

PyObjects From Python Via ctypes Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-06-11 07:41 +0000
  Re: PyObjects From Python Via ctypes Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-06-11 07:44 +0000

csiph-web