On 28.11.2012 04:01, Robert Steckroth wrote:
Hey Gang, why is the below snippet for error checking not working? I wan't
to be able to pass in different PyTypeObjects
to the CHECK_TYPE function and check an object for that type.
It works fine when I insert put &PyString_Type directly into
the function like so -->
if ( ! PyObject_TypeCheck(s_obj, &PyString_type) )
However, the CHECK_TYPE function will return -1 if I pass the PyTypeObject
in, like at the bottom of this message.
Why does PyObject_TypeCheck not work if PyTypeObject is first passed into a
function?
int CHECK_TYPE(PyObject *s_obj, PyTypeObject obj_type) {
You need *obj_type here, since you need the pointer to the object, not
the object itself.
if ( ! s_obj )
return 1;
if ( ! PyObject_TypeCheck(s_obj, &obj_type) ) {
This needs to be obj_type.
Your code will pass in the address of
the PyTypeObject on the stack, which will not match any of the
type objects in the Python type system.
PyErr_Format(PyExc_TypeError, "[ %s ] object needs to be of
type [ %s ]\n\nUsage: %s", PyString_AsString(PyObject_Repr(s_obj)),
obj_type.tp_name, obj_type.tp_doc );
This code leaks memory since the representation object is not freed.
return -1; }
return 1;
}
if ( !CHECK_TYPE(align, PyString_Type) ) return NULL;
This needs to the &PyString_Type
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, Nov 28 2012)
Python Projects, Consulting and Support ... http://www.egenix.com/
mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/