| 1 |
import cairo # pycairo |
| 2 |
|
| 3 |
import cairocffi |
| 4 |
|
| 5 |
|
| 6 |
def _UNSAFE_pycairo_context_to_cairocffi(pycairo_context): # noqa: N802 |
| 7 |
# Sanity check. Continuing with another type would probably segfault. |
| 8 |
if not isinstance(pycairo_context, cairo.Context): |
| 9 |
raise TypeError('Expected a cairo.Context, got %r' % pycairo_context) |
| 10 |
|
| 11 |
# On CPython, id() gives the memory address of a Python object. |
| 12 |
# pycairo implements Context as a C struct: |
| 13 |
# typedef struct { |
| 14 |
# PyObject_HEAD |
| 15 |
# cairo_t *ctx; |
| 16 |
# PyObject *base; |
| 17 |
# } PycairoContext; |
| 18 |
# Still on CPython, object.__basicsize__ is the size of PyObject_HEAD, |
| 19 |
# ie. the offset to the ctx field. |
| 20 |
# ffi.cast() converts the integer address to a cairo_t** pointer. |
| 21 |
# [0] dereferences that pointer, ie. read the ctx field. |
| 22 |
# The result is a cairo_t* pointer that cairocffi can use. |
| 23 |
return cairocffi.Context._from_pointer( |
| 24 |
cairocffi.ffi.cast('cairo_t **', |
| 25 |
id(pycairo_context) + object.__basicsize__)[0], |
| 26 |
incref=True) |