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.
     
    lallous, Nov 16, 2009
    #1
    1. Advertisements

  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?
     
    lallous, Nov 16, 2009
    #2
    1. Advertisements

  3. 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, Nov 16, 2009
    #3
  4. 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, Nov 16, 2009
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.