[issue26906] format(object.__reduce__) fails intermittently

Serhiy Storchaka report at bugs.python.org
Wed May 4 13:57:17 EDT 2016


Serhiy Storchaka added the comment:

The problem is that format() fails for instances of some classes, because the type still is not initialized. The simplest example -- list iterator.

>>> format(iter([]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Type listiterator doesn't define __format__

After forcing type initialization (for example by getting any type's attribute), format() becomes working.

>>> type(iter([])).foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'listiterator' has no attribute 'foo'
>>> format(iter([]))
'<listiterator object at 0xb708d0ec>'

I afraid that format() is just one example, and there are other functions or operators that don't work or work incorrectly if the type was not initialized.

init_types-2.7.patch adds explicit initialization of 38 types (I didn't check that all of them need this, but I suppose they do). This is large patch, and I'm not sure that it fixes all types.

Other way is to try to initialize the type just in _PyType_Lookup if it is not initialized. This is simple change, but a comment in _PyType_Lookup warns me. I found that this solution was already applied as an attempt to fix issue551412, but then reverted. Since you seem to be the most knowledgeable with this code, I'm asking you what was wrong with this approach and how we can fix this.

Python 3.x also suffers from this bug, but it is reproduced with less types. For example it isn't reproduced for list iterator. I don't know why.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26906>
_______________________________________


More information about the Python-bugs-list mailing list