Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Robert Steckroth <robertsteckroth-Re5JQEeQqe8AvxtiuMwx3w <at> public.gmane.org>
Subject: Fwd: What's up with PyTypeObject and functions?
Newsgroups: gmane.comp.python.capi
Date: Saturday 1st December 2012 20:19:48 UTC (over 4 years ago)
---------- Forwarded message ----------
From: Robert Steckroth

Date: Sat, Dec 1, 2012 at 3:18 PM
Subject: Re: [capi-sig] What's up with PyTypeObject and functions?
To: "M.-A. Lemburg" 


Thank you for the response~ I guess I should flesh
out implementations before integration into the project.
The error checking worked out nicely->

    if ( ! PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, &s_obj,
&align, &padding_list)
                                           || !CHECK_TYPE(s_obj,
&PySurface_Type) || !CHECK_TYPE(align, &PyString_Type) ||
!CHECK_TYPE(padding_list, &PyList_Type) )
             return NULL; PyErr_Clear();

if ( ! PyArg_ParseTuple(args, "|O", &sd_obj) || (!CHECK_TYPE(sd_obj,
&PySurface_Type) && !CHECK_TYPE(sd_obj, &PyDisplay_Type)) )
             return NULL; PyErr_Clear();


int CHECK_TYPE(PyObject *s_obj, PyTypeObject *obj_type) {
        if ( ! s_obj ) return 1;
        PyObject *obj_str=NULL, *ptype=NULL, *pvalue=NULL,
*ptraceback=NULL, *last_error=NULL, *tmp=NULL;
 if ( ! PyObject_TypeCheck(s_obj, obj_type) ) {
             obj_str = PyObject_Repr(s_obj);
             PyErr_Fetch(&ptype, &pvalue, &ptraceback);
             if ( pvalue ) { tmp = PyObject_Str(pvalue); last_error =
PyString_FromFormat("%s or", PyString_AsString(tmp)); }
             else          { last_error=PyString_FromString(""); }

             PyErr_Format(PyExc_TypeError, "%s [ %s ] object needs to be of
type [ %s ]", PyString_AsString(last_error), PyString_AsString(obj_str),
obj_type->tp_name );
             Py_DECREF(obj_str); Py_DECREF(last_error); Py_XDECREF(tmp);
Py_XDECREF(pvalue); Py_XDECREF(ptype); Py_XDECREF(ptraceback);
             return NULL; }
return 1;
}





On Wed, Nov 28, 2012 at 4:02 AM, M.-A. Lemburg
 wrote:

> 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/
>



-- 
Bust0ut, Surgemcgee: Systems Engineer ---
surgemcgee.com
Django_Teamplate3d



-- 
Bust0ut, Surgemcgee: Systems Engineer ---
surgemcgee.com
Django_Teamplate3d
 
CD: 4ms