# C++ program embedding Python crashes when calling PyUnicode\_AsUTF8

**URL:** <https://discuss.python.org/t/c-program-embedding-python-crashes-when-calling-pyunicode-asutf8/103824>\
**Category:** Python Help\
**Created:** [September 18, 2025, 6:11pm UTC](https://discuss.python.org/t/c-program-embedding-python-crashes-when-calling-pyunicode-asutf8/103824 "2025-09-18T18:11:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandros](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexandros/32/5744_2.png) [@Alexandros](https://discuss.python.org/u/Alexandros)\
**Post date:** [September 18, 2025, 6:11pm UTC](https://discuss.python.org/t/c-program-embedding-python-crashes-when-calling-pyunicode-asutf8/103824/1 "2025-09-18T18:11:52Z")

</div>

Hi all,

I’ve posted this in the Python form at python-forum[dot]io but I really think this is a more suitable place for this post, so I’m posting it here too (will report back to python forum if resolved).

I’m embedding Python into an openFrameworks (a C++ toolkit for creative coding) program through the C/C++ embeddings of the Pyo module (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](https://github.com/python/cpython/blob/3.13/Objects/unicodeobject.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:

```python
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](https://github.com/belangeo/pyo/blob/master/embedded/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.

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [September 18, 2025, 6:21pm UTC](https://discuss.python.org/t/c-program-embedding-python-crashes-when-calling-pyunicode-asutf8/103824/2 "2025-09-18T18:21:57Z")

</div>

Are you absolutely sure that `msg` is backed by a writable buffer large enough to contain the extended program? No code visible verifies this which is a sign of horrible C-coding practices. What happens if you run your example with valgrind?

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [September 18, 2025, 6:33pm UTC](https://discuss.python.org/t/c-program-embedding-python-crashes-when-calling-pyunicode-asutf8/103824/3 "2025-09-18T18:33:18Z")

</div>

`except Exception, _e_` is a syntax error in Python 3, so `PyRun_SimpleString` will be returning -1 to indicate that an exception occurred, which you’re not checking for.

I suspect that `PyImport_AddModule` and `PyObject_GetAttrString` are returning `NULL` for the same reason, which you’re not checking for either.

If you’re passing `NULL` to `PyUnicode_AsUTF8`, I’m not surprised it’s crashing. Don’t do that!

---

<div class="post-metadata">

**Author:** ![ngoldbaum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ngoldbaum/32/12276_2.png) [@ngoldbaum](https://discuss.python.org/u/ngoldbaum)\
**Post date:** [September 18, 2025, 9:47pm UTC](https://discuss.python.org/t/c-program-embedding-python-crashes-when-calling-pyunicode-asutf8/103824/4 "2025-09-18T21:47:48Z")

</div>

And in particular, _always_ check the return value of a C function call that has error cases. You _have_ to handle them, otherwise you get issues like this.

---

<div class="post-metadata">

**Author:** ![Alexandros](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexandros/32/5744_2.png) [@Alexandros](https://discuss.python.org/u/Alexandros)\
**Post date:** [September 19, 2025, 7:59am UTC](https://discuss.python.org/t/c-program-embedding-python-crashes-when-calling-pyunicode-asutf8/103824/5 "2025-09-19T07:59:16Z")

</div>

That was it! I now changed the code to the following and it doesn’t crash:

```python
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 as _e_:\n _error_=str(_e_)");
        PyEval_AcquireThread(interp);
        err = PyRun_SimpleString(msg);
        module = PyImport_AddModule(" __main__");
		if (module != NULL) {
			obj = PyObject_GetAttrString(module, "_error_");
        	if (obj != Py_None && obj != NULL) {
				if (PyUnicode_AsUTF8(obj) != NULL) {
        	    	strcpy(msg, PyUnicode_AsUTF8(obj));
				}
        	    err = 1;
        	}
		}
        PyEval_ReleaseThread(interp);
    }
    else {
        PyEval_AcquireThread(interp);
        err = PyRun_SimpleString(msg);
        PyEval_ReleaseThread(interp);
    }
    return err;
}

```

The problem is that the way this code is written it can only execute single lines, as it inserts a `try` statement with four spaces but doesn’t insert an additional four spaces for every line in the string to execute. So, if I try to execute the following:

```python
def foo():
    print("bar")

```

it will result in executing:

```python
_error_=None
try:
   def foo():
   print("bar")
except Exception as _e_:
   _error_=str(_e_)

```

so it will not indent `print(“bar”)`, which results in an indentation error.

I have to do some C string juggling there, I guess.
