For background: I am working on a modification to a
game engine that uses Python for automation. I am
trying to expose a few new objects to python scripts.
The engine already exposes a dozen or so different
objects to scripts, and I am exposing these new objects
the exact same way.
I am unable to override str() or repr() for my objects.
If I set the appropriate member in the type structure
(tp_str or tp_repr), PyObject_Str()/PyObject_Repr()
mangle the object when trying to convert it. If I leave
the structure member empty, everything works (except
for the str()/repr() override not working, naturally.)
I've gone over all my code multiple times, and can't
find anything that could possibly cause this. When I
step through the code in the VC++ debugger, I can see
the ob_type member of my object become corrupted at a
certain point in PyObject_Str()'s execution.
This block of code:
if (v->ob_type->tp_str == NULL)
return PyObject_Repr(v);
res = (*v->ob_type->tp_str)(v);
Is apparently the culprit. If tp_str is NULL, the line
following the if is never reached. If it is set,
however, that line is executed, and for some reason
(one which I cannot determine, since tp_str isn't
documented at all), that line calls a method of
std::string, and once that function pointer call
returns, my object's ob_type member has been changed to
an invalid pointer (usually something like 0x00000032.)
|