Garbage output from warnings with embedded python
![](https://secure.gravatar.com/avatar/984ff69427eeffa305d6f3e7ca65873f.jpg?s=120&d=mm&r=g)
Recently I wanted to deprecate some functions in our the C api, and found that warnings give weired output when running through through our C api.
A simple test is to do this...
print range(1.5)
When I run this from python I get good output.. test.py:1: DeprecationWarning: integer argument expected, got float print range(1.5)
But when running from our C api I get
/home/ideasman42/blender-svn/blender/blender.bin:1: DeprecationWarning: integer argument expected, got float ELF�p40�"4()&44�4�44�4�����F��F��F�K �K ğ<@G�K �K xxHH�H� P�td�2?��C ��C L,L,Q�td/lib/ld-linux.so.2GNU����8�1LQ �U�N�.yH {��d
I thought this was todo with running a compiled string so I tried PyRun_String() on the text buffer instead of PyEval_EvalCode() but the same problem. Id be interested to know if anyones embedded python gives proper warnings. On Linux testing with python 2.6 here.
--
- Campbell
![](https://secure.gravatar.com/avatar/a7f2a6cda71e76087df248f647931241.jpg?s=120&d=mm&r=g)
Wow, haven't seen that in a long time. And it happened on the same deprecation warning.
Ken
Campbell Barton wrote:
Recently I wanted to deprecate some functions in our the C api, and found that warnings give weired output when running through through our C api.
A simple test is to do this...
print range(1.5)
When I run this from python I get good output.. test.py:1: DeprecationWarning: integer argument expected, got float print range(1.5)
But when running from our C api I get
/home/ideasman42/blender-svn/blender/blender.bin:1: DeprecationWarning: integer argument expected, got float ELF�p40�"4()&44�4�44�4�����F��F��F�K �K ğ<@G�K �K xxHH�H� P�td�2?��C ��C L,L,Q�td/lib/ld-linux.so.2GNU����8�1LQ �U�N�.yH {��d
I thought this was todo with running a compiled string so I tried PyRun_String() on the text buffer instead of PyEval_EvalCode() but the same problem. Id be interested to know if anyones embedded python gives proper warnings. On Linux testing with python 2.6 here.
![](https://secure.gravatar.com/avatar/984ff69427eeffa305d6f3e7ca65873f.jpg?s=120&d=mm&r=g)
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");
![](https://secure.gravatar.com/avatar/984ff69427eeffa305d6f3e7ca65873f.jpg?s=120&d=mm&r=g)
had another look into it and figured out whats going on, in short for warnings python wants there to be a file on disk for the script and in our case there is None, its not getting __file__ from the global namespace so it gets argv[0] which is the blender3d binary in our case.
The only fix I found so far was to set the __file__ variable in the namespace before running the script.
commit log. http://lists.blender.org/pipermail/bf-blender-cvs/2009-May/019681.html
On Sun, Feb 22, 2009 at 6:45 PM, Campbell Barton <ideasman42@gmail.com> wrote:
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");
--
- Campbell
participants (2)
-
Campbell Barton
-
Ken Hughes