-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for BigInteger in CPython engine (#10830)
Support is added for encoding decoding BigInteger values from Python int that have a size that exceeds what can fit in a long. Also a BigInteger encoder/decoder is added to the custom encoders of Python.NET in order to support encoding and decoding in the context of function calls.
- Loading branch information
Showing
9 changed files
with
146 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Numerics; | ||
using Python.Runtime; | ||
|
||
namespace DSCPython.Encoders | ||
{ | ||
internal class BigIntegerEncoder : IPyObjectEncoder, IPyObjectDecoder | ||
{ | ||
public bool CanDecode(PyObject objectType, Type targetType) | ||
{ | ||
return targetType == typeof(BigInteger); | ||
} | ||
|
||
public bool CanEncode(Type type) | ||
{ | ||
return type == typeof(BigInteger); | ||
} | ||
|
||
public bool TryDecode<T>(PyObject pyObj, out T value) | ||
{ | ||
if (!PyLong.IsLongType(pyObj)) | ||
{ | ||
value = default; | ||
return false; | ||
} | ||
|
||
using (var pyLong = PyLong.AsLong(pyObj)) | ||
{ | ||
value = (T)(object)pyLong.ToBigInteger(); | ||
return true; | ||
} | ||
} | ||
|
||
public PyObject TryEncode(object value) | ||
{ | ||
return new PyLong(value.ToString()); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
47 changes: 47 additions & 0 deletions
47
test/Libraries/DynamoPythonTests/PythonEvalTestsWithLibraries.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Numerics; | ||
using Dynamo; | ||
using NUnit.Framework; | ||
using static DSIronPythonTests.IronPythonTests; | ||
|
||
namespace DynamoPythonTests | ||
{ | ||
public class PythonEvalTestsWithLibraries : DynamoModelTestBase | ||
{ | ||
protected override void GetLibrariesToPreload(List<string> libraries) | ||
{ | ||
libraries.Add("FFITarget.dll"); | ||
} | ||
|
||
public IEnumerable<PythonEvaluatorDelegate> Evaluators = new List<PythonEvaluatorDelegate> { | ||
DSCPython.CPythonEvaluator.EvaluatePythonScript, | ||
DSIronPython.IronPythonEvaluator.EvaluateIronPythonScript | ||
}; | ||
|
||
[Test] | ||
public void TestBigIntegerEncoding() | ||
{ | ||
string code = @" | ||
import sys | ||
import clr | ||
clr.AddReference('FFITarget') | ||
from FFITarget import DummyMath | ||
# Provide Python int as arguments: Python => .NET | ||
sum = DummyMath.Sum(11111111111111111111, 11111111111111111111) | ||
# sum contains a BigInteger and we use it in Python + operation: .NET => Python | ||
sum = sum + 1 | ||
OUT = sum | ||
"; | ||
var empty = new ArrayList(); | ||
var expected = BigInteger.Parse("22222222222222222223"); | ||
foreach (var pythonEvaluator in Evaluators) | ||
{ | ||
var result = pythonEvaluator(code, empty, empty); | ||
Assert.AreEqual(expected, result); | ||
} | ||
} | ||
} | ||
} |
This file contains 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