← Winnitude! Search  ·  Reader Mode (4492 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 79 LINKS

#2240 Recursion error when doing reversed python operations on C# types by gertdreyer · Pull Request #2327 · pythonnet/pythonnet

Repository navigation

#2240 Recursion error when doing reversed python operations on C# types - #2327


Merged
lostmsu merged 5 commits into pythonnet:master from gertdreyer:bugfix/recursionError Feb 26, 2024
Merged

#2240 Recursion error when doing reversed python operations on C# types #2327 lostmsu merged 5 commits into pythonnet:master from gertdreyer:bugfix/recursionError

Conversation

gertdreyer commented Feb 23, 2024



Copy link Copy Markdown
Contributor

What does this fix? Explain your changes.

Reverse operators were not implemented correctly for C# types which resulted in Recursion errors when embedding C# in Python. Changed the model binding process to add reverse operators for all applicable types. Added a flag to MethodBinder that signals the operation is a reversed operation and that the arguments to the operator needs to be reversed.

Added tests and an wrapper object for int without overloaded operator methods and a wrapper codec to test the reverse operations (and forward operator on the same type for safety).
...

Does this close any currently open issues?

This should resolve #2240 .
...

Any other comments?

...

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Ensure you have signed the .NET Foundation CLA
  • Add yourself to AUTHORS
  • Updated the CHANGELOG
gertdreyer added 2 commits February 23, 2024 09:35 Tests for operators on type CS type with codec 5d551fb Bugfix: RecursionError when reverse/righthand operations invoked. e.g... e7a3aba
.... __rsub__, __rmul__
gertdreyer changed the title Bugfix/recursion error #2240 Recursion error when doing reversed python operations on C# types Feb 23, 2024 Update Authors and Changelog c256746 gertdreyer force-pushed the bugfix/recursionError branch from 1ae96dd to c256746
Compare
February 23, 2024 13:34 lostmsu reviewed Feb 24, 2024
View reviewed changes

lostmsu left a comment · edited Loading



Copy link Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more .

Can you explain what the issue was (e.g. what sequence of actions caused the error, and how codecs were a part of it) and how this fix addresses this scenario?

gertdreyer commented Feb 26, 2024



Copy link Copy Markdown
Contributor

Author

Changes made...

I traced it down to the nb_add, nb_mul, etc slots only having the forward operators defined/linked for C# types being used in Python, this somehow caused the RecursionError when any reverse operators are called as they are probably just pointing to random pointers.

py * cs calls py.__mul__(cs) which is returns not implemented for the cs type and codecs does not get called prior to the mul call which results in cs.__rmul__(py) being called which caused the recursion error. I do not believe that there are problems with the codec system but it is needed to test this due to being on both sides of the CLR boundary.

It seems there were only catered for forward operators when both are the different (with overloaded operators, i.e. OwnInt - Int64 and Int64 - OwnInt). Thus all the code to detect whether a call is a reverse operator tested based on calling and passed types. Thus when called with the same types it could never detect it was a reversed operation even if it were defined. This needed some changes to the MethodBinding and ModelBinding to ensure the reversed calls are linked to the right C# operators and called with the arguments swapped.

gertdreyer force-pushed the bugfix/recursionError branch from d4e1fe9 to 4ea33fe
Compare
February 26, 2024 11:26 Normalized names. Added HashCode and Equals to testing objects 5277ed9 gertdreyer force-pushed the bugfix/recursionError branch from 4ea33fe to 5277ed9
Compare
February 26, 2024 11:30 lostmsu reviewed Feb 26, 2024
View reviewed changes
Comment thread src/runtime/MethodBinder.cs
Outdated
/// <param name="argsReversed">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param>
/// <returns>A Binding if successful. Otherwise null.</returns>
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw , bool argsReversed = false )
gertdreyer force-pushed the bugfix/recursionError branch from 3409d88 to 38fa8f2
Compare
February 26, 2024 18:58 Cleanup Codec/Argument Reversing Passing. ed2505c gertdreyer force-pushed the bugfix/recursionError branch from 38fa8f2 to ed2505c
Compare
February 26, 2024 19:01 lostmsu approved these changes Feb 26, 2024
View reviewed changes
lostmsu merged commit 71ca063 into pythonnet : master Feb 26, 2024 dependabot
Bot
mentioned this pull request Jan 16, 2026 Bump pythonnet from 3.0.3 to 3.0.5 mattemangia/Deep3DStudio#99 Closed Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment

Labels

None yet

2 participants