PySequence_Check on class instance

S

Sebastian Tusk

PySequence_Check returns 1 for instances of a simple class. Even if this
class doesn't implement __len__ and __getitem__.

The following code thus fails with an exception for classes that doesn't
implement the sequence protocol.

if (PySequence_Check( instance )) {
int size = PySequence_Size( instance );
}

Is this intended behaviour or a bug?


Regards,
Sebastian
 
M

Michael Hudson

Sebastian Tusk said:
PySequence_Check returns 1 for instances of a simple class. Even if
this class doesn't implement __len__ and __getitem__.

The following code thus fails with an exception for classes that
doesn't implement the sequence protocol.

if (PySequence_Check( instance )) {
int size = PySequence_Size( instance );
}

Is this intended behaviour or a bug?

Um, it's what I'd expect :)

Note that you have to check for an exception after PySequence_Size if
you suspect that a user-defined type is involved *anyway* because the
__len__() method might be buggy.

Do you know about the PySequence_Fast API? That's often less of a
pain than doing this sort of thing yourself...

Cheers,
mwh
 

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

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top