[Python-Dev] Encoding of PyFrameObject members

Francis Giraldeau francis.giraldeau at gmail.com
Fri Feb 6 00:27:24 CET 2015


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150205/ede56d14/attachment.html>


More information about the Python-Dev mailing list