Skip to content
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

Convert BigInteger to Int64 when possible from Python #10096

Merged
merged 5 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/DynamoUtilities/DataMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace Dynamo.Utilities
{
Expand All @@ -25,6 +26,20 @@ public DataMarshaler()
// Dictionary<TKey, TValue> and IronPython.Runtime.PythonDictionary both implement IDictionary
return dict.Keys.Cast<object>().ToDictionary(key => Marshal(key), key => Marshal(dict[key]));
});
RegisterMarshaler(
(BigInteger bigInt) =>
{
long int64;
try
{
int64 = (long)bigInt;
}
catch (OverflowException)
{
return bigInt;
}
return int64;
});
}

/// <summary>
Expand Down Expand Up @@ -86,7 +101,9 @@ public object Marshal(object obj)
var applicable = marshalers.Where(pair => pair.Key.IsAssignableFrom(targetType));

if (!applicable.Any())
{
return obj;
}

// Find the marshaler that operates on the closest base type of the target type.
var dispatchedMarshaler =
Expand Down
1 change: 1 addition & 0 deletions src/DynamoUtilities/DynamoUtilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
QilongTang marked this conversation as resolved.
Show resolved Hide resolved
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
14 changes: 14 additions & 0 deletions test/Libraries/DynamoPythonTests/PythonEditTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class PythonEditTests : DynamoViewModelUnitTest
protected override void GetLibrariesToPreload(List<string> libraries)
{
libraries.Add("DesignScriptBuiltin.dll");
libraries.Add("DSCoreNodes.dll");
libraries.Add("DSIronPython.dll");
base.GetLibrariesToPreload(libraries);
}
Expand Down Expand Up @@ -212,5 +213,18 @@ private void UpdatePythonNodeContent(ModelBase pythonNode, string value)

ViewModel.ExecuteCommand(command);
}

[Test]
public void BigInteger_CanBeMarshaledAsInt64()
{
// open test graph
var examplePath = Path.Combine(TestDirectory, @"core\python", "BigIntegerToLong.dyn");
ViewModel.OpenCommand.Execute(examplePath);

var guid = "23088248d7b1441abbc5ada07fcdf154";

AssertPreviewValue(guid,
new[] {"System.Int64", "System.Double", "System.Int64", "System.Int64", "System.Numerics.BigInteger"});
}
}
}
2 changes: 1 addition & 1 deletion test/Libraries/DynamoPythonTests/PythonEvalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,5 @@ public void SliceOperator_Output()

Assert.AreEqual(expected, output);
}
}
}
}
132 changes: 132 additions & 0 deletions test/core/python/BigIntegerToLong.dyn
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"Uuid": "13bd6959-7de4-49d4-98c9-fe6c75aced8c",
"IsCustomNode": false,
"Description": null,
"Name": "BigIntegerToLong",
"ElementResolver": {
"ResolutionMap": {}
},
"Inputs": [],
"Outputs": [],
"Nodes": [
{
"ConcreteType": "PythonNodeModels.PythonNode, PythonNodeModels",
"NodeType": "PythonScriptNode",
"Code": "# Load the Python Standard and DesignScript Libraries\r\nimport sys\r\nimport clr\r\n\r\n# The inputs to this node will be stored as a list in the IN variables.\r\ndataEnteringNode = IN\r\n\r\n# Place your code below this line\r\n\r\n# Assign your output to the OUT variable.\r\nOUT = [2059130726864, 2*10e13, int(2*10e13), -9223372036854775808, 9223372036854775808]",
"VariableInputPorts": true,
"Id": "547d1dd9203746bbaa2b7bd448c8124a",
"Inputs": [
{
"Id": "f39b2d33b15a4634b8c24228f2e985b3",
"Name": "IN[0]",
"Description": "Input #0",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Outputs": [
{
"Id": "20ae72e54022449fb5547939aa05cbd2",
"Name": "OUT",
"Description": "Result of the python script",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Disabled",
"Description": "Runs an embedded IronPython script."
},
{
"ConcreteType": "Dynamo.Graph.Nodes.ZeroTouch.DSFunction, DynamoCore",
"NodeType": "FunctionNode",
"FunctionSignature": "DSCore.Object.Type@var",
"Id": "23088248d7b1441abbc5ada07fcdf154",
"Inputs": [
{
"Id": "4fc5a9ec1dac43c298f2fb819b6c0c44",
"Name": "obj",
"Description": "An object.\n\nvar",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Outputs": [
{
"Id": "ea6928c8a2814de193d882a42613ba21",
"Name": "string",
"Description": "Type of object.",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Auto",
"Description": "Returns the type of object represented as string.\n\nObject.Type (obj: var): string"
}
],
"Connectors": [
{
"Start": "20ae72e54022449fb5547939aa05cbd2",
"End": "4fc5a9ec1dac43c298f2fb819b6c0c44",
"Id": "49b834023f51443b81132f6231bb8a52"
}
],
"Dependencies": [],
"NodeLibraryDependencies": [],
"Bindings": [],
"View": {
"Dynamo": {
"ScaleFactor": 1.0,
"HasRunWithoutCrash": true,
"IsVisibleInDynamoLibrary": true,
"Version": "2.5.0.6757",
"RunType": "Automatic",
"RunPeriod": "1000"
},
"Camera": {
"Name": "Background Preview",
"EyeX": -17.0,
"EyeY": 24.0,
"EyeZ": 50.0,
"LookX": 12.0,
"LookY": -13.0,
"LookZ": -58.0,
"UpX": 0.0,
"UpY": 1.0,
"UpZ": 0.0
},
"NodeViews": [
{
"ShowGeometry": true,
"Name": "Python Script",
"Id": "547d1dd9203746bbaa2b7bd448c8124a",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 201.0,
"Y": 293.0
},
{
"ShowGeometry": true,
"Name": "Object.Type",
"Id": "23088248d7b1441abbc5ada07fcdf154",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 436.0,
"Y": 367.0
}
],
"Annotations": [],
"X": 0.0,
"Y": 0.0,
"Zoom": 1.0
}
}