From 1f74dcc1dde2b92b3cd06911ea191995fe9cd99a Mon Sep 17 00:00:00 2001 From: aparajit-pratap Date: Tue, 29 Oct 2019 13:32:04 -0400 Subject: [PATCH 1/5] convert BigInteger to Int64 wherever possible from Python --- src/DynamoUtilities/DataMarshaler.cs | 16 +++++++++++++++- src/DynamoUtilities/DynamoUtilities.csproj | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/DynamoUtilities/DataMarshaler.cs b/src/DynamoUtilities/DataMarshaler.cs index 7d7e86f3c3c..16d19f14bbd 100644 --- a/src/DynamoUtilities/DataMarshaler.cs +++ b/src/DynamoUtilities/DataMarshaler.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Dynamo.Utilities { @@ -86,7 +87,20 @@ public object Marshal(object obj) var applicable = marshalers.Where(pair => pair.Key.IsAssignableFrom(targetType)); if (!applicable.Any()) - return obj; + { + if (!(obj is BigInteger bigInt)) return obj; + + long int64; + try + { + int64 = (long)bigInt; + } + catch (OverflowException) + { + return bigInt; + } + return int64; + } // Find the marshaler that operates on the closest base type of the target type. var dispatchedMarshaler = diff --git a/src/DynamoUtilities/DynamoUtilities.csproj b/src/DynamoUtilities/DynamoUtilities.csproj index 065d649686f..eea7c19f2b0 100644 --- a/src/DynamoUtilities/DynamoUtilities.csproj +++ b/src/DynamoUtilities/DynamoUtilities.csproj @@ -49,6 +49,7 @@ + From 4b0f0901ff58aac2a9f430128fa2aee8678c520b Mon Sep 17 00:00:00 2001 From: aparajit-pratap Date: Mon, 4 Nov 2019 16:36:22 -0500 Subject: [PATCH 2/5] revert C#7.0 syntax --- src/DynamoUtilities/DataMarshaler.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/DynamoUtilities/DataMarshaler.cs b/src/DynamoUtilities/DataMarshaler.cs index 16d19f14bbd..02721950399 100644 --- a/src/DynamoUtilities/DataMarshaler.cs +++ b/src/DynamoUtilities/DataMarshaler.cs @@ -88,7 +88,12 @@ public object Marshal(object obj) if (!applicable.Any()) { - if (!(obj is BigInteger bigInt)) return obj; + BigInteger bigInt; + if (obj is BigInteger) + { + bigInt = (BigInteger) obj; + } + else return obj; long int64; try From 16cf9eb942b793558ec31fbdd63226f4e404c43b Mon Sep 17 00:00:00 2001 From: aparajit-pratap Date: Thu, 7 Nov 2019 21:14:21 -0500 Subject: [PATCH 3/5] add test --- .../Libraries/DynamoPythonTests/PythonEditTests.cs | 14 ++++++++++++++ .../Libraries/DynamoPythonTests/PythonEvalTests.cs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/test/Libraries/DynamoPythonTests/PythonEditTests.cs b/test/Libraries/DynamoPythonTests/PythonEditTests.cs index a31b291ca6a..2e63f6f682a 100644 --- a/test/Libraries/DynamoPythonTests/PythonEditTests.cs +++ b/test/Libraries/DynamoPythonTests/PythonEditTests.cs @@ -15,6 +15,7 @@ public class PythonEditTests : DynamoViewModelUnitTest protected override void GetLibrariesToPreload(List libraries) { libraries.Add("DesignScriptBuiltin.dll"); + libraries.Add("DSCoreNodes.dll"); libraries.Add("DSIronPython.dll"); base.GetLibrariesToPreload(libraries); } @@ -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"}); + } } } diff --git a/test/Libraries/DynamoPythonTests/PythonEvalTests.cs b/test/Libraries/DynamoPythonTests/PythonEvalTests.cs index cd67290e569..a554e03a9eb 100644 --- a/test/Libraries/DynamoPythonTests/PythonEvalTests.cs +++ b/test/Libraries/DynamoPythonTests/PythonEvalTests.cs @@ -103,5 +103,5 @@ public void SliceOperator_Output() Assert.AreEqual(expected, output); } -} + } } From 8226b8005249958283285455a96eb1dd950eaa1b Mon Sep 17 00:00:00 2001 From: aparajit-pratap Date: Thu, 7 Nov 2019 21:15:58 -0500 Subject: [PATCH 4/5] add test script --- test/core/python/BigIntegerToLong.dyn | 132 ++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 test/core/python/BigIntegerToLong.dyn diff --git a/test/core/python/BigIntegerToLong.dyn b/test/core/python/BigIntegerToLong.dyn new file mode 100644 index 00000000000..6c4d6394566 --- /dev/null +++ b/test/core/python/BigIntegerToLong.dyn @@ -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 + } +} \ No newline at end of file From 7f1dd7b0ad1d9dd03190ccbc2c7fd7be6dd15887 Mon Sep 17 00:00:00 2001 From: aparajit-pratap Date: Thu, 7 Nov 2019 21:43:56 -0500 Subject: [PATCH 5/5] address review comments --- src/DynamoUtilities/DataMarshaler.cs | 32 +++++++++++++--------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/DynamoUtilities/DataMarshaler.cs b/src/DynamoUtilities/DataMarshaler.cs index 02721950399..b8422de1828 100644 --- a/src/DynamoUtilities/DataMarshaler.cs +++ b/src/DynamoUtilities/DataMarshaler.cs @@ -26,6 +26,20 @@ public DataMarshaler() // Dictionary and IronPython.Runtime.PythonDictionary both implement IDictionary return dict.Keys.Cast().ToDictionary(key => Marshal(key), key => Marshal(dict[key])); }); + RegisterMarshaler( + (BigInteger bigInt) => + { + long int64; + try + { + int64 = (long)bigInt; + } + catch (OverflowException) + { + return bigInt; + } + return int64; + }); } /// @@ -88,23 +102,7 @@ public object Marshal(object obj) if (!applicable.Any()) { - BigInteger bigInt; - if (obj is BigInteger) - { - bigInt = (BigInteger) obj; - } - else return obj; - - long int64; - try - { - int64 = (long)bigInt; - } - catch (OverflowException) - { - return bigInt; - } - return int64; + return obj; } // Find the marshaler that operates on the closest base type of the target type.