extension modules in C for python, check for korrekt Object

Discussion in 'Python' started by Torsten Mohr, Jun 1, 2004.

  1. Torsten Mohr

    Torsten Mohr Guest

    Hi,

    i write a set of extension modules for python in C.
    I also use some new types that i defined there.

    At the moment i write some methods that expect some
    of these types as parameters. I now want to check
    that the right types were given as parameters.

    How can i do that?

    PyArg_ParseTuple only checks that an object was
    given as parameter, but not its type.

    Do i need to check tp_name in the objects type?


    Thanks for hints,
    Torsten.
    Torsten Mohr, Jun 1, 2004
    #1
    1. Advertising

  2. Torsten Mohr

    Greg Ewing Guest

    Torsten Mohr wrote:
    > At the moment i write some methods that expect some
    > of these types as parameters. I now want to check
    > that the right types were given as parameters.


    Use PyObject_IsInstance().

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
    Greg Ewing, Jun 2, 2004
    #2
    1. Advertising

  3. Am Mittwoch, 2. Juni 2004 05:26 schrieb Greg Ewing:
    > Use PyObject_IsInstance().


    Or PyObject_TypeCheck()... All documented in the Python C/API documentation on
    python.org.

    Heiko.
    Heiko Wundram, Jun 2, 2004
    #3
  4. Torsten Mohr <> writes:

    > Hi,
    >
    > i write a set of extension modules for python in C.
    > I also use some new types that i defined there.
    >
    > At the moment i write some methods that expect some
    > of these types as parameters. I now want to check
    > that the right types were given as parameters.
    >
    > How can i do that?
    >
    > PyArg_ParseTuple only checks that an object was
    > given as parameter, but not its type.


    It can check type, using the "O!" incantation.

    If you're asking "how do I get hold of the type object I defined in
    some other extension module?", then that's a good question. One
    approach is to use "CObjects":

    http://docs.python.org/ext/using-cobjects.html

    > Do i need to check tp_name in the objects type?


    This sounds fragile :)

    Cheers,
    mwh

    --
    Of the four project development variables - scope, cost, time and
    quality - quality isn't really a free variable. The only possible
    values are "excellent" and "insanely excellent", depending on
    whether lives are at stake. -- Kent Beck, XP Explained
    Michael Hudson, Jun 2, 2004
    #4
  5. Torsten Mohr

    Steve Menard Guest

    Torsten Mohr wrote:

    > Hi,
    >
    > i write a set of extension modules for python in C.
    > I also use some new types that i defined there.
    >
    > At the moment i write some methods that expect some
    > of these types as parameters. I now want to check
    > that the right types were given as parameters.
    >
    > How can i do that?
    >
    > PyArg_ParseTuple only checks that an object was
    > given as parameter, but not its type.
    >
    > Do i need to check tp_name in the objects type?
    >
    >
    > Thanks for hints,
    > Torsten.
    >


    The PyArg_ParseTuple method can do type checking for you, as long as
    your requirements are pretty simple.

    if you method accepts only one argument, and that argument must be of a
    specific type, the following will work :

    PyObject* some_method(PyObject* self, PyObject* args)
    {
    PyObject* value;

    if (! PyArg_ParseTuple(args, "O!", &typeStruct, &value))
    {
    // Exception ahs already been set byt the call ... so
    // only return NULL
    return NULL;
    }
    ...
    }

    typeStruct is a reference to the PyObejctType structure that defines the
    type. either the one you defined or those already available through python.

    Steve
    Steve Menard, Jun 2, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand
    Replies:
    3
    Views:
    876
    Tim Daneliuk
    Nov 8, 2003
  2. Tim Churches

    Use of GPLed Python extension modules

    Tim Churches, Nov 22, 2003, in forum: Python
    Replies:
    36
    Views:
    1,082
    Jarek Zgoda
    Nov 28, 2003
  3. Michael Kent
    Replies:
    3
    Views:
    613
    Michael Kent
    Jun 11, 2004
  4. Mark Asbach
    Replies:
    1
    Views:
    457
    Leo Kislov
    Nov 3, 2006
  5. Joachim Dahl
    Replies:
    6
    Views:
    836
    Martin v. Löwis
    Jun 3, 2009
Loading...

Share This Page