PySequence_Check on class instance

Michael Hudson mwh at python.net
Fri Mar 5 05:19:11 EST 2004


Sebastian Tusk <sebastian.tusk at silver-style.com> writes:

> 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

-- 
  After a heavy night I travelled on, my face toward home - the comma
  being by no means guaranteed.           -- paraphrased from cam.misc



More information about the Python-list mailing list