Repository navigation
Fix pickeling - #717
Fix pickeling#717Vizonex wants to merge 21 commits into
Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
# Conflicts: # frozenlist/__init__.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #717 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 3 3
Lines 419 438 +19
Branches 10 11 +1
=========================================
+ Hits 419 438 +19
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@Vizonex would you bring this back up to date? |
|
@bdraco Sorry for the wait. I can. |
This comment has been minimized.
This comment has been minimized.
for more information, see https://pre-commit.ci
[Medium impact] The PR changes pickle round trips for the public FrozenList type. The PR is not ready to merge because imports with extensions disabled, circular lists, and subclass restoration still break. Reviews (5) · Last reviewed commit: "Merge branch 'master' into fix-pickeling" · Reviewed by Greptile |
| from ._frozenlist import FrozenList as CFrozenList # type: ignore | ||
| except ImportError: # pragma: no cover | ||
| pass | ||
| FrozenList = PyFrozenList |
There was a problem hiding this comment.
Disabling extensions breaks imports
With FROZENLIST_NO_EXTENSIONS=1, this assignment is skipped, and the renamed class no longer defines FrozenList. A fresh from frozenlist import FrozenList raises ImportError, including in the configured pure-Python test runs. Set FrozenList = PyFrozenList before the conditional, then replace it when the extension loads.
| def __reduce_ex__(self, protocol): | ||
| return type(self).__unreduce_ex__, (self._frozen, self._items) | ||
|
|
||
| PyFrozenList = FrozenList | ||
| @classmethod | ||
| def __unreduce_ex__(cls, frozen: bool, _items: list): | ||
| fl = cls(_items) |
There was a problem hiding this comment.
Passing _items as constructor arguments loses contents when a list contains itself. For x = PyFrozenList([1, 2]); x.append(x), the default pickle round trip returns an empty list, and deepcopy replaces the self-reference with a different list. This also violates the requirement in AGENTS.md that both implementations behave alike, since Cython's __deepcopy__ still works. Create the object before restoring its items, and fix both reducers.
Context Used: AGENTS.md (source)
| return type(self).__unreduce_ex__, (self.frozen, self._items) | ||
|
|
||
| @classmethod | ||
| def __unreduce_ex__(cls, frozen: bool, _items: list): | ||
| fl = cls(_items) |
There was a problem hiding this comment.
Calling cls(_items) assumes every subclass accepts items in its constructor, while the reducer also drops extra subclass state. aiosignal.Signal takes an owner instead. Unpickling calls Signal(callbacks), drops its callbacks, and replaces _owner with that list. Preserve subclass state and restore it without passing items to the subclass constructor.
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
…into fix-pickeling
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
| @@ -0,0 +1,2 @@ | |||
| Fixed pickling in Cython Extension. | |||
What do these changes do?
I forgot I had made a fork for trying to fix pickling but I seemed to never bother to add it after the issuer told me there was no need but it seems it needs to come back. This PR adds in a new argument to freeze a frozenlist right away which is a tradeoff but it was the only way I could seem to get pickling to work correctly.
Are there changes in behavior for the user?
Related issue number
fixes #683
Checklist
CONTRIBUTORS.txtCHANGESfolder<issue_id>.<category>for example (588.bugfix)issue_idchange it to the pr id after creating the pr.bugfix: Signifying a bug fix..feature: Signifying a new feature..breaking: Signifying a breaking change or removal of something public..doc: Signifying a documentation improvement..packaging: Signifying a packaging or tooling change that may be relevant to downstreams..contrib: Signifying an improvement to the contributor/development experience..misc: Anything that does not fit the above; usually, something not of interest to users.