Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C api question and determining PyObject type

2 views
Skip to first unread message

lallous

unread,
Nov 16, 2009, 5:03:27 AM11/16/09
to
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

unread,
Nov 16, 2009, 5:44:34 AM11/16/09
to
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" <lal...@lgwm.org> wrote in message news:hdr80a$vsg$1...@aioe.org...

Gabriel Genellina

unread,
Nov 16, 2009, 11:03:16 AM11/16/09
to pytho...@python.org
En Mon, 16 Nov 2009 07:44:34 -0300, lallous <lal...@lgwm.org> 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

unread,
Nov 16, 2009, 11:03:16 AM11/16/09
to pytho...@python.org
En Mon, 16 Nov 2009 07:44:34 -0300, lallous <lal...@lgwm.org> 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

0 new messages