Repository navigation
Fix self being None for a method of a temporary instance - #909
Merged
Merged
Conversation
A bound method held only a weak reference to its instance, so a method called on a temporary instance, such as Foo().method(), got self as None once the instance was freed before the call. The weak reference dates from when every method was bound and stored on the instance at construction, which would have made a reference cycle. Since #790, a new bound method is made on each attribute access and isn't stored on the instance, so hold a strong reference, as Python's bound methods do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
Contributor
|
Looks good to me. Both new tests fail without the change and pass with it; full suite, ruff, pylint and mypy are clean. I'm working on a rework of class handling and arrived at the same fix there. |
dmamelin
approved these changes
Nov 21, 2026
Member
Author
|
Thanks @dmamelin. |
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.
What was broken
A method called on a temporary instance got
selfasNone:Storing the instance first (
foo = Foo(); foo.value()) worked, which is why this went unnoticed. I found it while reviewing #907.Why
EvalFuncVar.__get__binds a method to its instance by creating anEvalFuncVarClassInst, and that held only aweakrefto the instance. InFoo().value(), nothing else refers to the temporary instance once the method has been looked up. So the instance is freed before the call, and the weak reference returnsNone.The weak reference goes back to the design before #790. Back then,
call_funcbound every method when the instance was created and stored the bound copies on the instance withsetattr. A strong reference from those copies back to the instance would have created a reference cycle, so instances would only be freed by the cyclic garbage collector and not as soon as the last reference went away. #790 switched to the descriptor protocol: a new bound method is made on each attribute access, and it isn't stored on the instance. That removed the cycle, but the weak reference stayed.What changed
EvalFuncVarClassInstnow holds a strong reference to the instance, as Python's own bound methods do. Nothing else used the weak reference, so theweakrefimport is gone. Storing a bound method on its own instance (self.cb = self.method) now creates a cycle, exactly as it does in Python.Testing
Two new cases in
evalTests, with expected values checked against CPython:Temp().name(),Temp().value(), and a bound method of a temporary instance called later. Without the fix, this fails with'NoneType' object has no attribute 'v'.__del__of a temporary instance runs right afterTemp().method(), and a stored bound method keeps its instance alive until it's deleted. This guards against the strong reference keeping instances alive too long.The existing
del ctest also checks that__del__runs promptly. As a mutation check, I made__get__cache bound methods on the instance, which creates a cycle, and that test failed. So prompt deletion remains covered.The full suite (206 tests), ruff, pylint and mypy pass.
🤖 Generated with Claude Code