[capi-sig] Garbage output from warnings with embedded python

Campbell Barton ideasman42 at gmail.com
Mon Feb 23 02:45:39 CET 2009


Still not sure why this happens.
Did a test with a small C/Python file that just runs external scripts,
with compiling AND running as a string I don't print gibberish.

So for now I added this to our own warning function...

               // import sys; print '\t%s:%d' %
(sys._getframe(0).f_code.co_filename, sys._getframe(0).f_lineno)

               PyObject *getframe, *frame;
               PyObject *f_lineno, *f_code, *co_filename;

               getframe = PySys_GetObject("_getframe"); // borrowed
               if (getframe) {
                       frame = PyObject_CallObject(getframe, NULL);
                       if (frame) {
                               f_lineno= PyObject_GetAttrString(frame,
"f_lineno");
                               f_code= PyObject_GetAttrString(frame, "f_code");
                               if (f_lineno && f_code) {
                                       co_filename=
PyObject_GetAttrString(f_code, "co_filename");
                                       if (co_filename) {

                                               printf("\t%s:%d\n",
PyString_AsString(co_filename),(int)PyInt_AsLong(f_lineno));

                                               Py_DECREF(f_lineno);
                                               Py_DECREF(f_code);
                                               Py_DECREF(co_filename);
                                               Py_DECREF(frame);
                                               return;
                                       }
                               }

                               Py_XDECREF(f_lineno);
                               Py_XDECREF(f_code);
                               Py_DECREF(frame);
                       }

               }
               PyErr_Clear();
               printf("\tERROR - Could not access
sys._getframe(0).f_lineno or sys._getframe().f_code.co_filename\n");


More information about the capi-sig mailing list