Skip to content

Commit e9dd2fe

Browse files
committed
util: Factor out Python module load
Create a utility function for loading a Python module by its name in UTF-8.
1 parent 6d67d05 commit e9dd2fe

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

decoder.c

+3-15
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ static int check_method(PyObject *py_dec, const char *mod_name,
597597
*/
598598
SRD_API int srd_decoder_load(const char *module_name)
599599
{
600-
PyObject *py_modname, *py_basedec, *py_apiver;
600+
PyObject *py_basedec, *py_apiver;
601601
struct srd_decoder *d;
602602
long apiver;
603603
int is_subclass;
@@ -617,13 +617,7 @@ SRD_API int srd_decoder_load(const char *module_name)
617617

618618
d = g_malloc0(sizeof(struct srd_decoder));
619619

620-
/* Import the Python module. */
621-
py_modname = PyUnicode_FromString(module_name);
622-
if (!py_modname)
623-
goto except_out;
624-
625-
d->py_mod = PyImport_Import(py_modname);
626-
Py_DECREF(py_modname);
620+
d->py_mod = py_import_by_name(module_name);
627621
if (!d->py_mod)
628622
goto except_out;
629623

@@ -806,15 +800,9 @@ static void srd_decoder_load_all_zip_path(char *path)
806800
char *prefix;
807801
size_t prefix_len;
808802

809-
zipimport_mod = NULL;
810803
set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
811804

812-
modname = PyUnicode_FromString("zipimport");
813-
if (!modname)
814-
goto err_out;
815-
816-
zipimport_mod = PyImport_Import(modname);
817-
Py_DECREF(modname);
805+
zipimport_mod = py_import_by_name("zipimport");
818806
if (zipimport_mod == NULL)
819807
goto err_out;
820808

exception.c

+2-8
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ SRD_PRIV void srd_exception_catch(const char *format, ...)
8484
{
8585
va_list args;
8686
PyObject *py_etype, *py_evalue, *py_etraceback;
87-
PyObject *py_modname, *py_mod, *py_func, *py_tracefmt;
87+
PyObject *py_mod, *py_func, *py_tracefmt;
8888
char *msg, *etype_name, *evalue_str, *tracefmt_str;
8989
const char *etype_name_fallback;
9090

@@ -118,13 +118,7 @@ SRD_PRIV void srd_exception_catch(const char *format, ...)
118118
if (!py_etraceback)
119119
goto cleanup;
120120

121-
py_modname = PyUnicode_FromString("traceback");
122-
if (!py_modname)
123-
goto cleanup;
124-
125-
py_mod = PyImport_Import(py_modname);
126-
Py_DECREF(py_modname);
127-
121+
py_mod = py_import_by_name("traceback");
128122
if (!py_mod)
129123
goto cleanup;
130124

libsigrokdecode-internal.h

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ SRD_PRIV PyObject *srd_logic_type_new(void);
9696
PyMODINIT_FUNC PyInit_sigrokdecode(void);
9797

9898
/* util.c */
99+
SRD_PRIV PyObject *py_import_by_name(const char *name);
99100
SRD_PRIV int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr);
100101
SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key, char **outstr);
101102
SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr);

util.c

+28
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@
2121
#include <config.h>
2222
#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
2323

24+
/**
25+
* Import a Python module by name.
26+
*
27+
* This function is implemented in terms of PyImport_Import() rather than
28+
* PyImport_ImportModule(), so that the import hooks are not bypassed.
29+
*
30+
* @param[in] name The name of the module to load as UTF-8 string.
31+
* @return The Python module object, or NULL if an exception occurred. The
32+
* caller is responsible for evaluating and clearing the Python error state.
33+
*
34+
* @private
35+
*/
36+
SRD_PRIV PyObject *py_import_by_name(const char *name)
37+
{
38+
PyObject *py_mod, *py_modname;
39+
40+
py_modname = PyUnicode_FromString(name);
41+
if (!py_modname)
42+
return NULL;
43+
44+
py_mod = PyImport_Import(py_modname);
45+
Py_DECREF(py_modname);
46+
47+
return py_mod;
48+
}
49+
2450
/**
2551
* Get the value of a Python object's attribute, returned as a newly
2652
* allocated char *.
@@ -196,6 +222,8 @@ SRD_PRIV int py_strseq_to_char(PyObject *py_strseq, char ***out_strv)
196222
*
197223
* @param[in] py_obj The Python object. Must not be NULL.
198224
* @return A floating reference to a new variant, or NULL on failure.
225+
*
226+
* @private
199227
*/
200228
SRD_PRIV GVariant *py_obj_to_variant(PyObject *py_obj)
201229
{

0 commit comments

Comments
 (0)