[Python-Dev] Encoding of PyFrameObject members

Gregory P. Smith greg at krypto.org
Fri Feb 6 07:47:25 CET 2015


On Thu Feb 05 2015 at 4:36:30 PM Francis Giraldeau <
francis.giraldeau at gmail.com> wrote:

> 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.
>

The faulthandler module calls into Python/traceback.c in signal context
which has some low level code for extracting something useful from
PyUnicode objects without triggering a conversion:

https://hg.python.org/cpython/file/f1a82e949fb8/Python/traceback.c#l531

That code is written to write output to a fd. It's much more useful for
signal handler tracing data collection from a signal handler if you
refactor it to dump its output into a preallocated char* buffer.

-gps


>
> Thanks!
>
> Francis
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> greg%40krypto.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150206/0f697ea1/attachment.html>


More information about the Python-Dev mailing list