Repository navigation
Raise TypeError instead of recursing on unsupported operator directions - #152
Merged
Martin-Molinero merged 3 commits intoOct 16, 2026
Conversation
PyType_Ready adds a slot wrapper for the forward and the reflected method of each number slot FixupSlots sets, when the type dict lacks them. A type declaring an operator in one direction only, like x + x, got a __radd__ wrapper that calls the very slot that dispatched to it, so 1.0 + x, 1 + x or sum([x]) recursed until RecursionError instead of raising TypeError. The same happened with [Flags] enums, whose bitwise operators are forward only (1 & flags). The wrappers without a CLR operator behind them are now removed after PyType_Ready, so the slot returns NotImplemented and Python raises its usual TypeError. A Python subclass can still define the missing method. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Martin-Molinero
force-pushed
the
bug-reflected-operator-recursion
branch
from
October 16, 2026 15:31
7cc12e9 to
83fd880
Compare
The cleanup now deletes a member only when it is a wrapper_descriptor, the object PyType_Ready adds, instead of anything that is not a managed object. It is best effort: a failed delete clears its error and any exception is swallowed, since a wrapper left behind only keeps the previous behavior. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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
An operation a CLR type supports in one direction only now raises
TypeError, like any unsupported Python operation. Before, it recursed untilRecursionError.x + x1.0 + x,1 + x,sum([x]),x + 1.0RecursionError(exceptx + 1.0)TypeError1.0 + xx + 1.0,x + xRecursionErrorTypeError[Flags]enum1 & flags,1 | flagsRecursionErrorTypeErrorThe supported directions are unchanged. A Python subclass can still define the missing method, e.g. its own
__radd__.Why
InitClassBasefills the type dict with the CLR operators, e.g.__add__. ThenFixupSlotsinstalls the genericnb_addslot, andPyType_Readyadds a slot wrapper for each forward and reflected name the dict lacks, e.g.__radd__. With no CLR overload behind it, that wrapper calls back into the same slot that dispatched to it:slot_nb_add→__radd__wrapper →nb_add→slot_nb_add→ …So
1.0 + xloops untilRecursionError. Code that catchesTypeErrorto fall back (sum(), numeric helpers) gets aRecursionErrorinstead. This happens on master56b3b6e7with a plain class that has no implicit conversion. It's not related to #150/#151, though I found it while reviewing #151.How
After
PyType_Ready,OperatorMethod.RemoveUnboundSlotWrappersgoes through the slotsFixupSlotsinstalled for the type. It removes the forward and reflected entries that are CPython slot wrappers (wrapper_descriptor, the objectsPyType_Readyadds). CLR operators, and any CLR member named like a dunder, are pythonnet objects and stay. With no__radd__to find, the slot returnsNotImplemented, so Python raises its usualTypeError.Only operator slots this type installed are touched. Native slots implemented by pythonnet itself (
nb_bool, in-place event slots) and comparison wrappers are left alone.The cleanup is best effort and never fails the type creation: a failed delete clears its Python error, and any exception is swallowed. A wrapper left behind only keeps the previous behaviour.
Cost: about 1 µs per class, once when its Python type is created, which itself takes ~150–215 µs. There's no cost per operator call.
Where
src/runtime/Types/OperatorMethod.cs:RemoveUnboundSlotWrapperssrc/runtime/TypeManager.cs: called afterPyType_ReadyinInitializeClasssrc/embed_tests/TestOperator.cs: testsTests
New in
TestOperator:UnsupportedReflectedOperationRaisesTypeErrorUnsupportedForwardOperationRaisesTypeErrorUnsupportedReflectedFlagsOperationRaisesTypeErrorPythonSubclassCanStillDefineTheReflectedOperationClrMemberNamedLikeTheReflectedMethodIsKept: a C# method literally named__radd__is kept and usedThe first four fail on master and pass with the fix. The last one pins that only CPython's wrappers are removed.
Full embed suite: 998 passed, 0 failed (net10.0, Python 3.11).
Same wrappers removed after narrowing the rule: across 1,154 types from .NET's own assemblies, the same 79 types end up with an operator in one direction only, e.g. every
[Flags]enum,DateTime,TimeSpan,Decimal,Int128and theVector/Matrixtypes.Lean Python regression algorithms: 339/339 passed with this build dropped into Lean
master154e50720c, following.github/workflows/lean-python-regression-tests.yml.🤖 Generated with Claude Code