<div dir="ltr"><br><br><div class="gmail_quote">On Thu Feb 05 2015 at 4:36:30 PM Francis Giraldeau <<a href="mailto:francis.giraldeau@gmail.com">francis.giraldeau@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>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):</div><div><br></div><div>PyFrameObject *frame = PyEval_GetFrame();</div><div>PyObject *ob = PyUnicode_AsUTF8String(frame->f_code->co_filename)<br></div><div>char *str = PyBytes_AsString(ob)<br></div><div><br></div><div>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.</div><div><br></div><div>Instead, I access members directly:</div><div><div>char *str = PyUnicode_DATA(frame->f_code->co_filename);</div><div>size_t len = PyUnicode_GET_DATA_SIZE(frame->f_code->co_filename);</div></div><div><br></div><div>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.</div></div></blockquote><div><br></div><div>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:</div><div><br></div><div><a href="https://hg.python.org/cpython/file/f1a82e949fb8/Python/traceback.c#l531">https://hg.python.org/cpython/file/f1a82e949fb8/Python/traceback.c#l531</a><br></div><div><br></div><div>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.</div><div><br></div><div>-gps</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>Thanks!</div><div><br></div><div>Francis</div></div>
______________________________<u></u>_________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/<u></u>mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/greg%40krypto.org" target="_blank">https://mail.python.org/<u></u>mailman/options/python-dev/<u></u>greg%40krypto.org</a><br>
</blockquote></div></div>