This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Can't inherit slots from new-style classes implemented in C
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ncoghlan, philthompson10
Priority: normal Keywords:

Created on 2004-09-24 16:33 by philthompson10, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
typeobject.c.diff philthompson10, 2004-09-24 16:33 Patch to typeobject.c to fix slot inheritance
Messages (3)
msg22540 - (view) Author: Phil Thompson (philthompson10) Date: 2004-09-24 16:33
I have a new-style class implemented in C which defines slots 
(eg. puts a C function pointer in mp_length). It does not create a 
__len__ entry in the class dictionary. If I sub-class it in Python 
then the slot is not inherited. 
The function pointer is copied correctly by inherit_slots() (in 
typeobject.c). The problem occurs in update_one_slot(). The call 
to _PyType_Lookup() fails because the slot does not have an 
entry in the class dictionary. This causes the valid function 
pointer to be overwritten with the value of "specific" which is 0. 
The following patch fixes the problem - but I'm not sure it is the 
correct fix. 
msg22541 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2004-09-27 09:46
Logged In: YES 

Without seeing the original code, I'm not convinced this is
a Python problem. This is due to the fact that inheriting
from builtin's and standard library objects defined in C
works fine.

For instance, inheriting dict's __len__ is shown here:
>>> class foo(dict): pass
>>> len(foo())
>>> foo.__len__
<slot wrapper '__len__' of 'dict' objects>

But grep shows the dict source makes no reference to
'__len__'  directly:
[...@localhost src]$ grep -Hc "__len__" Objects/dictobject.c

The way it works is that PyType_Ready generates the
appropriate __dict__ entries for all of the special methods
that are defined at the C level (i.e. have non-null pointers).

If you aren't invoking PyType_Ready before using your class,
then it won't work properly. (This invocation is usually
made in the C module's initialisation method). See the
"Extending and Embedding" docs for all the gory details.
msg22542 - (view) Author: Phil Thompson (philthompson10) Date: 2004-09-29 18:10
Logged In: YES 

Yes, it's a bug in my code. The problem was that PyType_Ready() was 
being called too soon (rather than not at all). I needed to do some 
initialisation before PyType_Ready() was called and moving it from the 
tp_new slot to the tp_alloc slot solved. 
Apologies for the premature report. 
Date User Action Args
2022-04-11 14:56:07adminsetgithub: 40950
2004-09-24 16:33:41philthompson10create