[issue35048] Can't reassign __class__ despite the assigned class having identical slots

2019-12-07 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

The relevant logic is in the compatible_for_assignment() function on line 3972 
in Objects/typeobject.c.

After checking that the tp_free slots are the same, it proceeds as follows:

/*
 It's tricky to tell if two arbitrary types are sufficiently compatible as
 to be interchangeable; e.g., even if they have the same tp_basicsize, they
 might have totally different struct fields. It's much easier to tell if a
 type and its supertype are compatible; e.g., if they have the same
 tp_basicsize, then that means they have identical fields. So to check
 whether two arbitrary types are compatible, we first find the highest
 supertype that each is compatible with, and then if those supertypes are
 compatible then the original types must also be compatible.
*/

So what is happening is that "class a" and "class b" aren't being directly 
compared to one another.  Instead, they are being compared to their parent 
"dict".   Since *dict* doesn't have __dict__ or __weakref__, "a" and "b" are 
deemed to have incompatible layouts.   See lines 3951 to 3954 in 
same_slots_added() in Objects/typeobject.c.

--
nosy: +benjamin.peterson, pitrou

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35048] Can't reassign __class__ despite the assigned class having identical slots

2019-12-07 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35048] Can't reassign __class__ despite the assigned class having identical slots

2019-12-07 Thread Mark Dickinson


Change by Mark Dickinson :


--
nosy: +mark.dickinson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35048] Can't reassign __class__ despite the assigned class having identical slots

2019-12-05 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35048] Can't reassign __class__ despite the assigned class having identical slots

2019-12-05 Thread Sterling Smith


Change by Sterling Smith :


--
nosy: +Sterling Smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35048] Can't reassign __class__ despite the assigned class having identical slots

2018-10-23 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35048] Can't reassign __class__ despite the assigned class having identical slots

2018-10-23 Thread శ్రీనివాస్ రెడ్డి తాటిపర్తి

Change by Srinivas  Reddy Thatiparthy(శ్రీనివాస్ రెడ్డి తాటిపర్తి) 
:


--
nosy: +thatiparthy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35048] Can't reassign __class__ despite the assigned class having identical slots

2018-10-23 Thread Dan Snider


New submission from Dan Snider :

>>> class a(dict): __slots__ = '__dict__', 'x'

>>> class b(dict): __slots__ = '__dict__', 'x'

>>> self = a(); self.__class__ = b
Traceback (most recent call last):
  File "", line 1, in 
self=a(); self.__class__ = b
TypeError: __class__ assignment: 'b' object layout differs from 'a'

This always occurs when __dict__ and/or __weakref__ are defined as slots, even 
when both classes have otherwise identical slots. This behavior appears to 
contradict what the docs say wrt to __class__ assignment, which is (in its 
entirety):

"__class__ assignment works only if both classes have the same __slots__. "

Not sure if this is just a case of ambiguous documentation and intentional 
behavior or not. Since two classes with identical slots will always have 
identical internal struct layouts, I can't see a reason for this error.

--
messages: 328296
nosy: bup
priority: normal
severity: normal
status: open
title: Can't reassign __class__ despite the assigned class having identical 
slots
versions: Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com