C api question and determining PyObject type

L

lallous

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.
 
L

lallous

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?
 
G

Gabriel Genellina

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.
 
G

Gabriel Genellina

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top