[ python-Bugs-1034178 ] Can't inherit slots from new-style classes implemented in C

SourceForge.net noreply at sourceforge.net
Mon Sep 27 11:46:20 CEST 2004


Bugs item #1034178, was opened at 2004-09-25 02:33
Message generated for change (Comment added) made by ncoghlan
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1034178&group_id=5470

Category: Python Interpreter Core
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Phil Thompson (philthompson10)
Assigned to: Nobody/Anonymous (nobody)
Summary: Can't inherit slots from new-style classes implemented in C

Initial Comment:
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. 

----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2004-09-27 19:46

Message:
Logged In: YES 
user_id=1038590

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())
0
>>> foo.__len__
<slot wrapper '__len__' of 'dict' objects>

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

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.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1034178&group_id=5470


More information about the Python-bugs-list mailing list