[Python-Dev] Why does __getitem__ slot of builtin call sequence methods first?

Michael Hudson mwh at python.net
Sun Oct 2 22:36:01 CEST 2005


Travis Oliphant <oliphant at ee.byu.edu> writes:

> Thanks for the tip.  I think I figured out the problem, and it was my 
> misunderstanding of how types inherit in C that was the source of my 
> problem.  
>
> Basically, Python is doing what you would expect, the mp_item is used 
> for __getitem__ if both mp_item and sq_item are present.  However, the 
> addition of these descriptors  (and therefore the resolution of any 
> comptetion for __getitem__ calls) is done  *before*  the inheritance of 
> any slots takes place. 

Oof.  That'd do it.

> The new ndarray object inherits from a "big" array object that doesn't 
> define the sequence and buffer protocols (which have the size limiting 
> int dependencing in their interfaces).   The ndarray object has standard 
> tp_as_sequence and tp_as_buffer slots filled.  

I guess the reason this hasn't come up before is that non-trivial C
inheritance is still pretty rare.

> The easy fix was to initialize the tp_as_mapping slot before calling 
> PyType_Ready.    Hopefully, somebody else searching in the future for an 
> answer to their problem will find this discussion useful.  

Well, it sounds like a bug that should be easy to fix.  I can't think
of a reason to do slot wrapper generation before slot inheritance,
though I wouldn't like to bet more than a beer on not having missed
something...

Cheers,
mwh

-- 
  There are two kinds of large software systems: those that evolved
  from small systems and those that don't work.
                           -- Seen on slashdot.org, then quoted by amk


More information about the Python-Dev mailing list