Python Forum
C++ program embedding Python crashes when calling PyUnicode_AsUTF8
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ program embedding Python crashes when calling PyUnicode_AsUTF8
#1
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:

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.
Reply
#2
I added bbcode tags to your post.
This post
might help.
Reply
#3
At line 8 I see an except clause with a typical Python 2 syntax. Aren't you coding with an obsolete version of Python?
Alexandros likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
The problem is solved. Here's is the solution https://discuss.python.org/t/c-program-e...8/103824/3
Reply
#5
(Sep-18-2025, 06:27 PM)Gribouillis Wrote: At line 8 I see an except clause with a typical Python 2 syntax. Aren't you coding with an obsolete version of Python?
That's true. Didn't see your post (wasn't notified). It's that, together with not checking the output of PyImport_AddModule and PyObject_GetAttrString.
Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python script is hanging while calling a procedure in database prasanthi417 4 3,112 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  Understanding and debugging memory error crashes with python3.10.10 Arkaik 5 6,495 Apr-18-2023, 03:22 AM
Last Post: Larz60+
  Embedding python script into html via pyscript pyscript_dude 7 6,240 Apr-16-2023, 11:17 PM
Last Post: pyscript_dude
  Pydroid3 app crashes on xiaomi poco F3 JMD 2 3,363 Nov-27-2022, 11:56 AM
Last Post: JMD
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 2,790 Sep-19-2022, 02:32 AM
Last Post: Xeno
  C++ python embedding comarius 0 1,631 Aug-26-2022, 02:01 AM
Last Post: comarius
  HTML file crashes program mikefirth 12 7,649 Dec-31-2021, 03:57 AM
Last Post: Pedroski55
  Scraping a Flexible Element - works at first, and then crashes JonnyB 0 2,267 Aug-14-2021, 07:25 PM
Last Post: JonnyB
Question Embedding a python file online Dreary35 0 2,353 Jun-10-2021, 05:05 PM
Last Post: Dreary35
  Calling python from c++ in visual studio pdk5 0 3,069 May-24-2021, 10:18 AM
Last Post: pdk5

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.