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. --- module_sigrokdecode.c | 63 +++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 32 deletions(-) (limited to 'module_sigrokdecode.c') diff --git a/module_sigrokdecode.c b/module_sigrokdecode.c index 468a1cb..17563c1 100644 --- a/module_sigrokdecode.c +++ b/module_sigrokdecode.c @@ -23,11 +23,7 @@ /** @cond PRIVATE */ -/* type_decoder.c */ -extern SRD_PRIV PyTypeObject srd_Decoder_type; - -/* type_logic.c */ -extern SRD_PRIV PyTypeObject srd_logic_type; +SRD_PRIV PyObject *srd_logic_type = NULL; /* * When initialized, a reference to this module inside the Python interpreter @@ -47,42 +43,45 @@ static struct PyModuleDef sigrokdecode_module = { /** @cond PRIVATE */ PyMODINIT_FUNC PyInit_sigrokdecode(void) { - PyObject *mod; + PyObject *mod, *Decoder_type, *logic_type; - /* tp_new needs to be assigned here for compiler portability. */ - srd_Decoder_type.tp_new = PyType_GenericNew; - if (PyType_Ready(&srd_Decoder_type) < 0) - return NULL; + mod = PyModule_Create(&sigrokdecode_module); + if (!mod) + goto err_out; - srd_logic_type.tp_new = PyType_GenericNew; - if (PyType_Ready(&srd_logic_type) < 0) - return NULL; + Decoder_type = srd_Decoder_type_new(); + if (!Decoder_type) + goto err_out; + if (PyModule_AddObject(mod, "Decoder", Decoder_type) < 0) + goto err_out; - mod = PyModule_Create(&sigrokdecode_module); - Py_INCREF(&srd_Decoder_type); - if (PyModule_AddObject(mod, "Decoder", - (PyObject *)&srd_Decoder_type) == -1) - return NULL; - Py_INCREF(&srd_logic_type); - if (PyModule_AddObject(mod, "srd_logic", - (PyObject *)&srd_logic_type) == -1) - return NULL; + logic_type = srd_logic_type_new(); + if (!logic_type) + goto err_out; + if (PyModule_AddObject(mod, "srd_logic", logic_type) < 0) + goto err_out; /* Expose output types as symbols in the sigrokdecode module */ - if (PyModule_AddIntConstant(mod, "OUTPUT_ANN", SRD_OUTPUT_ANN) == -1) - return NULL; - if (PyModule_AddIntConstant(mod, "OUTPUT_PYTHON", SRD_OUTPUT_PYTHON) == -1) - return NULL; - if (PyModule_AddIntConstant(mod, "OUTPUT_BINARY", SRD_OUTPUT_BINARY) == -1) - return NULL; - if (PyModule_AddIntConstant(mod, "OUTPUT_META", SRD_OUTPUT_META) == -1) - return NULL; + if (PyModule_AddIntConstant(mod, "OUTPUT_ANN", SRD_OUTPUT_ANN) < 0) + goto err_out; + if (PyModule_AddIntConstant(mod, "OUTPUT_PYTHON", SRD_OUTPUT_PYTHON) < 0) + goto err_out; + if (PyModule_AddIntConstant(mod, "OUTPUT_BINARY", SRD_OUTPUT_BINARY) < 0) + goto err_out; + if (PyModule_AddIntConstant(mod, "OUTPUT_META", SRD_OUTPUT_META) < 0) + goto err_out; /* Expose meta input symbols. */ - if (PyModule_AddIntConstant(mod, "SRD_CONF_SAMPLERATE", SRD_CONF_SAMPLERATE) == -1) - return NULL; + if (PyModule_AddIntConstant(mod, "SRD_CONF_SAMPLERATE", SRD_CONF_SAMPLERATE) < 0) + goto err_out; + srd_logic_type = logic_type; mod_sigrokdecode = mod; return mod; +err_out: + Py_XDECREF(mod); + srd_exception_catch("Failed to initialize module"); + + return NULL; } /** @endcond */ -- cgit v1.2.3-70-g09d2