-
Notifications
You must be signed in to change notification settings - Fork 748
RecursionError when substracting System Decimal from Python Decimal #2240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I have the same issue: same setup, here is my case:
maximum recursion depth exceeded while calling a Python object hope it helps |
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. |
I have the same issue. Pythonnet version: 3.0.3 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
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: public class DecimalCodec : IPyObjectDecoder, IPyObjectEncoder
{
public bool CanDecode(PyType objectType, Type targetType)
{
return objectType.Name == "decimal.Decimal" && targetType == typeof(decimal);
}
public bool CanEncode(Type type)
{
return typeof(decimal) == type;
}
public bool TryDecode<T>(PyObject pyObj, out T? value)
{
if (typeof(T) != typeof(decimal))
{
value = default(T);
return false;
}
try
{
string decstr = (pyObj as dynamic).__str__();
bool parseSuccess = decimal.TryParse(decstr, out decimal csdeci);
value = (T)Convert.ChangeType(csdeci, typeof(decimal));
return parseSuccess;
}
catch (Exception ex)
{
value = default(T);
return false;
}
}
public PyObject? TryEncode(object value)
{
if (typeof(decimal) != value.GetType())
{
return null;
};
try
{
dynamic pydeci = PyModule.Import("decimal");
dynamic thedecimal = pydeci.Decimal(value.ToString());
return thedecimal;
}
catch (Exception ex)
{
return null;
}
}
} |
Environment
Details
Trying to substract System Decimal from Python Decimal (the other way works fine)
TODO
import both system decimal and python decimal
create a separate variable from each type
subtract System Decimal from Python Decimal
Traceback (most recent call last):
File "", line 1, in
RecursionError: maximum recursion depth exceeded while calling a Python object
The text was updated successfully, but these errors were encountered: