Skip to content

File dp_state_pyhelp.h

FileList > doppler > dp_state_pyhelp.h

Go to the source code of this file

The state bytes interface for hand-written (no_generate) CPython bindings. More...

Macros

Type Name
define DP_PY_STATE_METHODS (FNPFX, SELF_T, HANDLE_EXPR, CPFX)
Define the state_bytes / get_state / set_state methods of a hand-written binding.

Detailed Description

jm's serializable flag generates the Python triplet (state_bytes / get_state / set_state) for every jm-managed object. A no_generate module (e.g. ddc_fn) owns its binding by hand, so jm cannot generate it there — this macro provides the same three methods so the hand-written face is byte-identical in behaviour and cannot drift from jm's output.

Include from a *_ext.c translation unit (after Python.h). Instantiate with the PyObject struct type, an expression yielding the live C handle (or NULL when closed/destroyed), and the C object prefix, e.g.:

DP_PY_STATE_METHODS(Ddcr, DdcrObject, (self->closed ? NULL : self->h), ddcr)

then add to the type's PyMethodDef table:

{"state_bytes", (PyCFunction)Ddcr_state_bytes, METH_NOARGS, "Serialized state size in bytes."}, {"get_state", (PyCFunction)Ddcr_get_state, METH_NOARGS, "Serialize the engine's mutable state to bytes."}, {"set_state", (PyCFunction)Ddcr_set_state, METH_O, "Restore mutable state from a get_state() blob."},

The C triplet (<pfx>_state_bytes/get_state/set_state) must follow the standard ABI (see dp_state.h): set_state returns DP_OK (0) or a negative error. set_state here enforces an exact size match, mirroring jm.

Macro Definition Documentation

define DP_PY_STATE_METHODS

Define the state_bytes / get_state / set_state methods of a hand-written binding.

#define DP_PY_STATE_METHODS (
    FNPFX,
    SELF_T,
    HANDLE_EXPR,
    CPFX
) 

Expands to three static functions, FNPFX_state_bytes, FNPFX_get_state and FNPFX_set_state (FNPFX pasted in), with the PyMethodDef signatures shown in the file description. Each raises RuntimeError ("object is closed") when HANDLE_EXPR is NULL. get_state returns a new bytes object of the C state_bytes size; set_state raises TypeError for a non-bytes argument and ValueError for a size mismatch or a non-zero return from the C set_state.

Parameters:

  • FNPFX Prefix for the three generated function names.
  • SELF_T The binding's PyObject struct type.
  • HANDLE_EXPR Expression, evaluated with self in scope, that yields the live C handle, or NULL when closed.
  • CPFX C object prefix of the state triplet (CPFX_state_bytes, CPFX_get_state, CPFX_set_state).


The documentation for this class was generated from the following file native/inc/doppler/dp_state_pyhelp.h