Sep-18-2025, 01:37 PM (This post was last modified: Sep-18-2025, 02:21 PM by Larz60+. Edit Reason: Added code tags )
I'm embedding Python into an openFrameworks (https://openframeworks.cc/, a C++ toolkit for creative coding) program through the C/C++ embeddings of the Pyo module (https://belangeo.github.io/pyo/index.html, a Python module for DSP - Digital Signal Processing - written in C).
It works fine when calling PyRun_SimpleString() to run a Python chunk of code, but when trying to get more information in case something goes wrong, the program calls PyUnicode_AsUTF8() and it crashes. I run the program with GDB and it pointed that this function calls PyUnicode_AsUTF8AndSize(), which is where the crash happens. This happens in unicodeobject.c from Python's sources (which by the way I couldn't locate in my system, only unicodeobject.h).
From Python's GitHub's cpython directory, I found unicodeobject.c and found the PyUnicode_AsUTF8() in line 4139 (https://github.com/python/cpython/blob/3...deobject.c). Indeed, this funciton calls PyUnicode_AsUTF8AndSize(PyObject *unicode, NULL), which is where GDB points to. Here's the bit of code from Pyo's C/C++ embeddings where this happens:
I think this is all the info I've got. I would appreaciate any help.
Thanks.
It works fine when calling PyRun_SimpleString() to run a Python chunk of code, but when trying to get more information in case something goes wrong, the program calls PyUnicode_AsUTF8() and it crashes. I run the program with GDB and it pointed that this function calls PyUnicode_AsUTF8AndSize(), which is where the crash happens. This happens in unicodeobject.c from Python's sources (which by the way I couldn't locate in my system, only unicodeobject.h).
From Python's GitHub's cpython directory, I found unicodeobject.c and found the PyUnicode_AsUTF8() in line 4139 (https://github.com/python/cpython/blob/3...deobject.c). Indeed, this funciton calls PyUnicode_AsUTF8AndSize(PyObject *unicode, NULL), which is where GDB points to. Here's the bit of code from Pyo's C/C++ embeddings where this happens:
INLINE int pyo_exec_statement(PyThreadState *interp, char *msg, int debug) { int err = 0; if (debug) { PyObject *module, *obj; char pp[26] = "_error_=None\ntry:\n "; memmove(msg + strlen(pp), msg, strlen(msg)+1); memmove(msg, pp, strlen(pp)); strcat(msg, "\nexcept Exception, _e_:\n _error_=str(_e_)"); PyEval_AcquireThread(interp); PyRun_SimpleString(msg); module = PyImport_AddModule("__main__"); obj = PyObject_GetAttrString(module, "_error_"); if (obj != Py_None) { strcpy(msg, PyUnicode_AsUTF8(obj)); err = 1; } PyEval_ReleaseThread(interp); } else { PyEval_AcquireThread(interp); err = PyRun_SimpleString(msg); PyEval_ReleaseThread(interp); } return err; }The call to PyUnicode_AsUTF8() happens in line 14 in the chunk above, with a valid PyObject* argument, since this is what is supposed to be returned by PyObject_GetAttrString(), in line 12 of the chunk above. The full code this chuck belongs to is here https://github.com/belangeo/pyo/blob/mas...ed/m_pyo.h. The code chunk above is in line 470.I think this is all the info I've got. I would appreaciate any help.
Thanks.
