From 201a85a8ea071d37f4fda2668c0a1c488d852f4e Mon Sep 17 00:00:00 2001 From: Daniel Elstner Date: Mon, 5 Oct 2015 01:53:43 +0200 Subject: Python: Restrict code to stable ABI subset Limit usage of the Python C API to the stable ABI subset as defined by PEP 384. This removes some type definitions and functions which libsigrokdecode made use of. Convert all affected code to suitable API alternatives. Also fix a few leaks that became apparent while working on the code. The most visible change is that PyTypeObject is now an opaque type. Thus, the custom Decoder and srd_logic types are now created on the heap via an alternative API. Unfortunately, since tp_name is now inaccessible, type names had to be removed from the log output. Stack traces after Python exceptions are now formatted by calling into Python, since the trace object C API is no longer available. --- type_logic.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'type_logic.c') diff --git a/type_logic.c b/type_logic.c index 0b11339..d126d7b 100644 --- a/type_logic.c +++ b/type_logic.c @@ -73,14 +73,25 @@ static PyObject *srd_logic_iternext(PyObject *self) return logic->sample; } -/** @cond PRIVATE */ -SRD_PRIV PyTypeObject srd_logic_type = { - PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "srd_logic", - .tp_basicsize = sizeof(srd_logic), - .tp_flags = Py_TPFLAGS_DEFAULT, - .tp_doc = "Sigrokdecode logic sample object", - .tp_iter = srd_logic_iter, - .tp_iternext = srd_logic_iternext, -}; -/** @endcond */ +/** Create the srd_logic type. + * @return The new type object. + * @private + */ +SRD_PRIV PyObject *srd_logic_type_new(void) +{ + PyType_Spec spec; + PyType_Slot slots[] = { + { Py_tp_doc, "sigrokdecode logic sample object" }, + { Py_tp_iter, (void *)&srd_logic_iter }, + { Py_tp_iternext, (void *)&srd_logic_iternext }, + { Py_tp_new, (void *)&PyType_GenericNew }, + { 0, NULL } + }; + spec.name = "srd_logic"; + spec.basicsize = sizeof(srd_logic); + spec.itemsize = 0; + spec.flags = Py_TPFLAGS_DEFAULT; + spec.slots = slots; + + return PyType_FromSpec(&spec); +} -- cgit v1.2.3-70-g09d2