Fix Attachment.__getattr__ recursion and copy/pickle support - #329
Open
Flerpharos wants to merge 1 commit into
Open
Fix Attachment.__getattr__ recursion and copy/pickle support#329Flerpharos wants to merge 1 commit into
Flerpharos wants to merge 1 commit into
Conversation
…tion _backing is looked up via getattr() itself, so __getattr__ recursed infinitely whenever _backing wasn't yet in __dict__ (copy, deepcopy, pickle, or a partially-constructed instance). Delegating dunder names (e.g. __getstate__) to the backing stream also hijacked copy/pickle's own reduction protocol. Guard both cases and raise AttributeError.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #320
Summary
Attachment.__getattr__readself._backingviagetattr, which re-entered__getattr__and recursed toRecursionErrorwhenever_backingwasn't yet set (partially-constructed instance, or an instance created bycopy/deepcopy/picklebypassing__init__).__getattr__was still delegating dunder lookups (e.g.__getstate__,__reduce_ex__) to the backing stream. That hijackedcopy/pickle's own reduction protocol with the backingBytesIO's state instead ofAttachment's, socopy.copy/copy.deepcopy/picklestill failed (with aValueError, not aRecursionError). Excluding dunder names from delegation fixes this; nothing in the codebase relies on dunder delegation throughAttachment.Test plan
uv run pytest --ignore=tests/test_integration.py -q(434 passed)uv run ruff check .AttributeErroron missing_backing, and successfulcopy.copy,copy.deepcopy, andpickle.loads(pickle.dumps(...))round-trips.