[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: I do not think there is a bug. If we ever decide to change the behavior, the changes can only go in future Python version. And if we decide to make changes, I think that it would be better to make the pure Python implementation using __new__() instead of _

[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Raymond Hettinger
Change by Raymond Hettinger : -- nosy: +eric.snow ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https://mai

[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Erez Zinman
Erez Zinman added the comment: Just to be clear, what I did is (works with both "copy" and "pickle"): ``` def _new_and_init(cls): inst = cls.__new__(cls) OrderedDict.__init__(inst) return inst class A(OrderedDict): def __reduce__(self): # This fixes a bug in Python

[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Erez Zinman
Erez Zinman added the comment: The first argument can be remedied by creating a new method that deals with that (something like `def _new_init(cls)`, and passing the `cls` as argument). The third argument is not really an argument - there is a bug that needs a precedent to be solved. Concer

[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: First, closures cannot be pickled. You have to pass self.__class__ as an argument. Second, it will break compatibility with older Python versions. Pickles created in new Python could not be unpickled in older Pythons. Third, it is the behavior which does

[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Erez Zinman
Erez Zinman added the comment: small correction: meant `self.__class__` in the above function. Also, it is based on https://github.com/python/cpython/blob/76553e5d2eae3e8a47406a6de4f354fe33ff370b/Lib/collections/__init__.py#L302 -- ___ Python trac

[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Erez Zinman
Erez Zinman added the comment: Looking at the implementation of `__init__` (https://github.com/python/cpython/blob/76553e5d2eae3e8a47406a6de4f354fe33ff370b/Lib/collections/__init__.py#L109), it seems that you can fix this bug while retaining backward compatibility if you would use `__new__`

[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: So we are restricted by backward compatibility with the original pure Python implementation. Sometimes it is feasible to break backward compatibility, but there should be very good reasons for it. -- ___ Python

[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: It would break the code which expect that __init__() be called (I do not know if there are much such code, but still). It also would not work with the original pure Python implementation of OrderedDict, which initializes the OrderedDict object in __init__(

[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Erez Zinman
Erez Zinman added the comment: Proposed solution - change OrderedDict's `__reduce_ex__ ` function. The code below works like expected if I override `__reduce_ex__` like so: ``` def __reduce_ex__(self, protocol): ret = list(super().__reduce_ex__(protocol)) ret[0] = type(se

[issue41751] Error copying an instance of a subclass of OrderedDict

2020-09-09 Thread Erez Zinman
New submission from Erez Zinman : This bug occurs when copying/pickling an ordered-dict subtype that has default items. The initialization function that's returned is **not** `object.__new__` so the default items are set when the copied/pickled item is created. The problem I encountered is th