C api question and determining PyObject type

Discussion in 'Python' started by lallous, Nov 16, 2009.

  1. lallous

    lallous Guest

    Hello

    I have an a class defined as:

    class __object(object):
    pass

    Now, I call a C function that takes a PyObject* and checks its type:

    if (PyString_Check(obj)) ...
    if (PySequence_Check(obj)) ....

    Before doing the check, I print the passed object with PyObject_Str() and
    get:

    passed object: <__main__.__object object at 0x040E4050>

    However, the C code returns true on the:
    if (PySequence_Check(obj)) ....


    Why? That is not a sequence?

    Please advise.

    --
    Elias
    lallous, Nov 16, 2009
    #1
    1. Advertising

  2. lallous

    lallous Guest

    Actually, the object class is defined as:
    class __object(object):
    def __getitem__(self, idx):
    return getattr(self, idx)

    Anyway, now I check like this:

    bool PyIsSequenceType(PyObject *obj)
    {
    if (!PySequence_Check(obj))
    return false;
    Py_ssize_t sz = PySequence_Size(obj);
    if (sz == -1 || PyErr_Occurred() != NULL)
    {
    PyErr_Clear();
    return false;
    }
    return true;
    }

    I don't like it, any other suggestions?

    --
    Elias
    "lallous" <> wrote in message news:hdr80a$vsg$...
    > Hello
    >
    > I have an a class defined as:
    >
    > class __object(object):
    > pass
    >
    > Now, I call a C function that takes a PyObject* and checks its type:
    >
    > if (PyString_Check(obj)) ...
    > if (PySequence_Check(obj)) ....
    >
    > Before doing the check, I print the passed object with PyObject_Str() and
    > get:
    >
    > passed object: <__main__.__object object at 0x040E4050>
    >
    > However, the C code returns true on the:
    > if (PySequence_Check(obj)) ....
    >
    >
    > Why? That is not a sequence?
    >
    lallous, Nov 16, 2009
    #2
    1. Advertising

  3. En Mon, 16 Nov 2009 07:44:34 -0300, lallous <> escribió:

    > Actually, the object class is defined as:
    > class __object(object):
    > def __getitem__(self, idx):
    > return getattr(self, idx)
    >
    > Anyway, now I check like this:
    >
    > bool PyIsSequenceType(PyObject *obj)
    > {
    > if (!PySequence_Check(obj))
    > return false;
    > Py_ssize_t sz = PySequence_Size(obj);
    > if (sz == -1 || PyErr_Occurred() != NULL)
    > {
    > PyErr_Clear();
    > return false;
    > }
    > return true;
    > }
    >
    > I don't like it, any other suggestions?


    Yes: find another name for the "thing" you're checking for. It's not the
    same as a "sequence" in the Python sense.

    Perhaps you want to consider your type a mapping? Sequences and mappings
    have a lot in common (mappings have length too.) In C you have separate
    slots tp_as_sequence, tp_as_mapping; but in Python code, __getitem__ is
    used for both meanings (and goes into both set of pointers.) tp_as_mapping
    takes precedence over tp_as_sequence.

    --
    Gabriel Genellina
    Gabriel Genellina, Nov 16, 2009
    #3
  4. En Mon, 16 Nov 2009 07:44:34 -0300, lallous <> escribió:

    > Actually, the object class is defined as:
    > class __object(object):
    > def __getitem__(self, idx):
    > return getattr(self, idx)
    >
    > Anyway, now I check like this:
    >
    > bool PyIsSequenceType(PyObject *obj)
    > {
    > if (!PySequence_Check(obj))
    > return false;
    > Py_ssize_t sz = PySequence_Size(obj);
    > if (sz == -1 || PyErr_Occurred() != NULL)
    > {
    > PyErr_Clear();
    > return false;
    > }
    > return true;
    > }
    >
    > I don't like it, any other suggestions?


    Yes: find another name for the "thing" you're checking for. It's not the
    same as a "sequence" in the Python sense.

    Perhaps you want to consider your type a mapping? Sequences and mappings
    have a lot in common (mappings have length too.) In C you have separate
    slots tp_as_sequence, tp_as_mapping; but in Python code, __getitem__ is
    used for both meanings (and goes into both set of pointers.) tp_as_mapping
    takes precedence over tp_as_sequence.

    --
    Gabriel Genellina
    Gabriel Genellina, Nov 16, 2009
    #4
    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. Nikolai Kirsebom

    Getting C++ object from PyObject *

    Nikolai Kirsebom, Sep 25, 2003, in forum: Python
    Replies:
    1
    Views:
    864
    Erwin S. Andreasen
    Sep 28, 2003
  2. Mike Hoegeman
    Replies:
    1
    Views:
    326
    Martin v. =?iso-8859-15?q?L=F6wis?=
    Dec 18, 2003
  3. Olivier Sessink
    Replies:
    0
    Views:
    380
    Olivier Sessink
    Feb 24, 2005
  4. Pradnyesh Sawant

    pyqt4 signal/slot using PyObject* and shortcut

    Pradnyesh Sawant, Apr 27, 2007, in forum: Python
    Replies:
    0
    Views:
    307
    Pradnyesh Sawant
    Apr 27, 2007
  5. pbienst

    C API: Getting PyObject by name

    pbienst, Sep 30, 2010, in forum: Python
    Replies:
    3
    Views:
    497
    pbienst
    Oct 2, 2010
Loading...

Share This Page