I need to access frame members from within a signal handler for tracing purpose. My first attempt to access co_filename was like this (omitting  error checking):

PyFrameObject *frame = PyEval_GetFrame();
PyObject *ob = PyUnicode_AsUTF8String(frame->f_code->co_filename)
char *str = PyBytes_AsString(ob)

However, the function PyUnicode_AsUTF8String() calls PyObject_Malloc(), which is not reentrant. If the signal handler nest over PyObject_Malloc(), it causes a segfault, and it could also deadlock.

Instead, I access members directly:
char *str = PyUnicode_DATA(frame->f_code->co_filename);
size_t len = PyUnicode_GET_DATA_SIZE(frame->f_code->co_filename);

Is it safe to assume that unicode objects co_filename and co_name are always UTF-8 data for loaded code? I looked at the PyTokenizer_FromString() and it seems to convert everything to UTF-8 upfront, and I would like to make sure this assumption is valid.

Thanks!

Francis