Skip to content

Commit

Permalink
8150: DYN-8176: Fixes from PythonNet3 (#15774)
Browse files Browse the repository at this point in the history
  • Loading branch information
twastvedt authored Jan 23, 2025
1 parent cbd859f commit e14e089
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
90 changes: 53 additions & 37 deletions src/Libraries/DSCPython/CPythonEvaluator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Autodesk.DesignScript.Runtime;
Expand Down Expand Up @@ -145,6 +146,8 @@ public class CPythonEvaluator : Dynamo.PythonServices.PythonEngine

private static PyScope globalScope;
private static DynamoLogger dynamoLogger;
private static string path;

internal static DynamoLogger DynamoLogger {
get
{ // Session is null when running unit tests.
Expand Down Expand Up @@ -261,60 +264,66 @@ public override object Evaluate(
{
PythonEngine.Initialize();
PythonEngine.BeginAllowThreads();

}

using (Py.GIL())
using (PyScope scope = Py.CreateScope())
{
if (globalScope == null)
{
globalScope = CreateGlobalScope();
}
scope.Exec("import sys" + Environment.NewLine + "path = str(sys.path)");
path = scope.Get<string>("path");
}
}

using (PyScope scope = Py.CreateScope())
using (Py.GIL())
{
globalScope ??= CreateGlobalScope();
using (PyScope scope = Py.CreateScope())
{
if (path is not null)
{
// Reset the 'sys.path' value to the default python paths on node evaluation.
var pythonNodeSetupCode = "import sys" + Environment.NewLine + "sys.path = sys.path[0:3]";
// Reset the 'sys.path' value to the default python paths on node evaluation. See https://github.com/DynamoDS/Dynamo/pull/10977.
var pythonNodeSetupCode = "import sys" + Environment.NewLine + $"sys.path = {path}";
scope.Exec(pythonNodeSetupCode);
}

ProcessAdditionalBindings(scope, bindingNames, bindingValues);
ProcessAdditionalBindings(scope, bindingNames, bindingValues);

int amt = Math.Min(bindingNames.Count, bindingValues.Count);
int amt = Math.Min(bindingNames.Count, bindingValues.Count);

for (int i = 0; i < amt; i++)
{
scope.Set((string)bindingNames[i], InputMarshaler.Marshal(bindingValues[i]).ToPython());
}
for (int i = 0; i < amt; i++)
{
scope.Set((string)bindingNames[i], InputMarshaler.Marshal(bindingValues[i]).ToPython());
}

try
{
OnEvaluationBegin(scope, code, bindingValues);
scope.Exec(code);
var result = scope.Contains("OUT") ? scope.Get("OUT") : null;
try
{
OnEvaluationBegin(scope, code, bindingValues);
scope.Exec(code);
var result = scope.Contains("OUT") ? scope.Get("OUT") : null;

return OutputMarshaler.Marshal(result);
return OutputMarshaler.Marshal(result);
}
catch (Exception e)
{
evaluationSuccess = false;
var traceBack = GetTraceBack(e);
if (!string.IsNullOrEmpty(traceBack))
{
// Throw a new error including trace back info added to the message
throw new InvalidOperationException($"{e.Message} {traceBack}", e);
}
catch (Exception e)
else
{
evaluationSuccess = false;
var traceBack = GetTraceBack(e);
if (!string.IsNullOrEmpty(traceBack))
{
// Throw a new error including trace back info added to the message
throw new InvalidOperationException($"{e.Message} {traceBack}", e);
}
else
{
#pragma warning disable CA2200 // Rethrow to preserve stack details
throw e;
throw e;
#pragma warning restore CA2200 // Rethrow to preserve stack details
}
}
finally
{
OnEvaluationEnd(evaluationSuccess, scope, code, bindingValues);
}
}
finally
{
OnEvaluationEnd(evaluationSuccess, scope, code, bindingValues);
}
}
}
}

public static object EvaluatePythonScript(
Expand Down Expand Up @@ -476,6 +485,13 @@ internal override void RegisterHostDataMarshalers()
}
}
var unmarshalled = pyObj.AsManagedObject(typeof(object));

// Avoid calling this marshaler infinitely.
if (unmarshalled is PyObject)
{
return unmarshalled;
}

return dataMarshalerToUse.Marshal(unmarshalled);
}
}
Expand Down
2 changes: 0 additions & 2 deletions test/Libraries/DynamoPythonTests/PythonEditTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,6 @@ public void VerifySysPathValueForCPythonEngine()
var sysPathList = GetFlattenedPreviewValues(firstPythonNodeGUID);

// Verify that the custom path is added to the 'sys.path'.
Assert.AreEqual(sysPathList.Count(), 4);
Assert.AreEqual(sysPathList.Last(), "C:\\Program Files\\dotnet");

// Change the python engine for the 2nd node and verify that the custom path is not reflected in the 2nd node.
Expand All @@ -642,7 +641,6 @@ public void VerifySysPathValueForCPythonEngine()
var pynode = nodeModel as PythonNode;
UpdatePythonEngineAndRun(pynode, PythonEngineManager.CPython3EngineName);
sysPathList = GetFlattenedPreviewValues(secondPythonNodeGUID);
Assert.AreEqual(sysPathList.Count(), 3);
Assert.AreNotEqual(sysPathList.Last(), "C:\\Program Files\\dotnet");
}

Expand Down

0 comments on commit e14e089

Please sign in to comment.