I'll have a look into the recursion (which I can reproduce and is a bug), but we don't actually by default provide conversions from/to Python decimals. You must be running a non-upstream version of Python.NET or have more code that you are not showing.
Pythonnet version: 3.0.3
Pythonnet version: 3.11
Dotnet 6.0: Windows and Linux
For a codec I am using a naive string based conversion of cs System.Decimal to py decimal.Decimal and the inverse, in our use case the possible loss of precision should not be a concern.
I have traced it down to the __rsub__ operation on the cs type.
Operation
Status
cs - py
Working
cs.__sub__(py)
Working
py - cs
RecursionError
py.__sub__(cs)
NotImplemented
cs.__rsub__(py)
RecursionError
The same happens with multiply and addition right-hand methods as well. As per the python rules for operators the forward operators are tried first and if they are NotImplemented for a type the right hand ones are tried where the RecursionError occurs. The codec conversion does not get called for the python decimal foward operators which results in the python decimal conversion code being called which throws the NotImplemented exception
Activity
dariooo512 commented on Sep 19, 2023
I have the same issue:
same setup, here is my case:
var offers = new [] { new {Discount = 2.0m}}; Runtime.PythonDLL = @"C:\Python311\python311.dll"; PythonEngine.Initialize(); PythonEngine.BeginAllowThreads(); using (Py.GIL()) { File.WriteAllText("script.py", @" def execute(offers): total_discount = 0 for offer in offers: total_discount += offer.Discount average_discount = total_discount / len(offers) return average_discount "); var script = Py.Import("script"); var result = script.InvokeMethod("execute", offers.ToPython()); }maximum recursion depth exceeded while calling a Python object
File [...], line 4, in execute
total_discount += offer.Discount
hope it helps
filmor commented on Sep 23, 2023
I'll have a look into the recursion (which I can reproduce and is a bug), but we don't actually by default provide conversions from/to Python decimals. You must be running a non-upstream version of Python.NET or have more code that you are not showing.
gertdreyer commented on Feb 21, 2024
I have the same issue.
Pythonnet version: 3.0.3
Pythonnet version: 3.11
Dotnet 6.0: Windows and Linux
For a codec I am using a naive string based conversion of cs System.Decimal to py decimal.Decimal and the inverse, in our use case the possible loss of precision should not be a concern.
I have traced it down to the __rsub__ operation on the cs type.
The same happens with multiply and addition right-hand methods as well. As per the python rules for operators the forward operators are tried first and if they are NotImplemented for a type the right hand ones are tried where the RecursionError occurs. The codec conversion does not get called for the python decimal foward operators which results in the python decimal conversion code being called which throws the NotImplemented exception
Codec: