Python - "object layout"
Asked Answered
C

2

7

can somebody describe the following exception? What is the "object layout" and how it is defined? Thanks

Traceback (most recent call last):
  File "test_gui.py", line 5, in <module>
    suite = AlgorithmEngine('gui_suite')
  File "/home/honza/Research/Voiar/algorithm.py", line 169, in __init__
    self.algorithms = self._initAlgorithms()
  File "/home/honza/Research/Voiar/algorithm.py", line 232, in _initAlgorithms
    self._initGUIAlgorithm(obj)
  File "/home/honza/Research/Voiar/algorithm.py", line 218, in _initGUIAlgorithm
    cls.__bases__ = bases
TypeError: __bases__ assignment: 'QWidget' object layout differs from 'GUIAlgorithm'
Clouet answered 22/7, 2010 at 12:27 Comment(1)
Possibly related: autoreload gratuitous "object layout differs" errorPedro
I
7

It means that you tried to change the type of an object (by assigning to __bases__) and the new type wasn't compatible with the old one. This happens when the underlying C data structure isn't the same for both types.

See http://www.mail-archive.com/[email protected]/msg52950.html for a list of differences between types that might trigger this exception.

Inexhaustible answered 22/7, 2010 at 12:38 Comment(3)
It is slightly disappointing how many caveats in general one runs into when going into the (exposed) internals of Python--the consistency and cleanness of Python just breaks down there. This here is just one example.Wildon
@ErikKaplun replacing __bases__ on the fly is hackery and cannot therefore contribute to the overall impression of a language :)Gilbertogilbertson
Gonna add going from a pure Python class at the start to adding in collections.abc.Mapping in the hierarchy to assist with making my original __class__ behave more like a dict. Assigning to self.__class__ (again a pure Python class) then give this exact error. Take away subclassing collections.abc.Mapping and error goes away.Volgograd
G
0

In my case, the error occurred when I tried to use change the __class__ of an object that also has __slots__, like this:

class Base:
    __slots__ = ('a', 'b', 'c')

class Child(Base):
    pass


obj = Base()
obj.__class__ = Child  
# -> TypeError: __class__ assignment: 'Child' object layout differs from 'Base'
Gilbertogilbertson answered 29/8, 2020 at 17:50 Comment(1)
Child should have __slots__ = tuple()Sennar

© 2022 - 2024 — McMap. All rights reserved.