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

load .net system.dll shim into python engine to enable compatibility with .Net8 #6

Merged
merged 1 commit into from
Feb 13, 2024
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
7 changes: 6 additions & 1 deletion DSIronPython/DSIronPython.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@

<PackageReference Include="IronPython" Version="2.7.12" />
<PackageReference Include="IronPython.StdLib" Version="2.7.12" />

<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />

</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>IronPythonTests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<Target Name="copypkgjson" AfterTargets="Build">
<Copy SourceFiles="pkg.json" DestinationFolder="$(OutputPath)..\" />
Expand Down
35 changes: 33 additions & 2 deletions DSIronPython/IronPythonEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Dynamo.Session;
using Dynamo.Utilities;
using IronPython.Hosting;

using Microsoft.Extensions.Configuration;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Utils;

Expand All @@ -25,6 +25,8 @@ namespace DSIronPython
[IsVisibleInDynamoLibrary(false)]
public class IronPythonEvaluator : Dynamo.PythonServices.PythonEngine
{
private bool CONFIG_ENABLE_NET48SHIMCOMPAT = false;

private const string DynamoPrintFuncName = "__dynamoprint__";
/// <summary> stores a copy of the previously executed code</summary>
private static string prev_code { get; set; }
Expand Down Expand Up @@ -120,7 +122,18 @@ public override object Evaluate(
if (code != prev_code)
{
ScriptEngine PythonEngine = Python.CreateEngine();
if (!string.IsNullOrEmpty(stdLib))
//to maintain compatability with ironPython code written in .netframework - we load the system.dll shim
//which loads many types which used to be in system.dll(mscorlib) like system.diagnostics.process
//which were moved in .Net. These types are now importable by python code without users needing to add
//clr.addreference.
//TODO consider a preference for this.
//TODO test smaller shims...netstd.dll

if (CONFIG_ENABLE_NET48SHIMCOMPAT)
{
PythonEngine.Runtime.LoadAssembly(Assembly.Load("System"));
}
if (!string.IsNullOrEmpty(stdLib))
{
paths = PythonEngine.GetSearchPaths().ToList();
paths.Add(stdLib);
Expand Down Expand Up @@ -366,5 +379,23 @@ private void OnEvaluationEnd( bool isSuccessful,

#endregion

internal IronPythonEvaluator(IConfiguration config = null)
{
//either inject a config or load from appsettings.json
if (config is not null)
{

}
else
{
var configPath = Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, "appsettings.json");
config = new ConfigurationBuilder().AddJsonFile(configPath, optional: true).Build();
}
var enableShimLoad = config.GetSection("config").GetChildren().FirstOrDefault(x => x.Key == nameof(CONFIG_ENABLE_NET48SHIMCOMPAT))?.Value;
if (bool.TryParse(enableShimLoad, out var parsed))
{
CONFIG_ENABLE_NET48SHIMCOMPAT = parsed;
}
}
}
}
5 changes: 5 additions & 0 deletions DSIronPython/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"config": {
"CONFIG_ENABLE_NET48SHIMCOMPAT": "true"
}
}
2 changes: 1 addition & 1 deletion DSIronPython/pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"license": "",
"file_hash": null,
"name": "DynamoIronPython2.7",
"version": "3.0.0",
"version": "3.1.0",
"description": "*** This is the official version of the IronPython Extension which will load the IronPython2 evaluation engine for Dynamo. *** \r\n\r\nIf you see the following warning 'The configured Python engine could not be found' when you try to run a Python Script node which is set to use IronPython2, the host application for Dynamo (Such as Revit or Civil 3d) will have chosen to exclude IronPython2 as default installed content and you will need to download this extension in order to enable IronPython2 again.\r\n\r\nYou have the ability to either download this IronPython2 Extension or use the alternative out-of-the-box CPython3 engine and migrate your legacy IronPython2 code to CPython3. While there are differences in language the Python basis is the same and we have provided a Migration Assistant to help you migrate your code to Python3.",
"group": "",
"keywords": [ "python", "ironpython" ],
Expand Down
2 changes: 1 addition & 1 deletion IronPythonExtension/IronPythonExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static void LoadPythonEngine(Assembly assembly)
{
if (assembly == null)
{
return;
throw new ArgumentNullException($"Error while loading python engine - assembly {PythonEvaluatorAssembly}.dll was not loaded successfully.");
}

// Currently we are using try-catch to validate loaded assembly and Singleton Instance method exist
Expand Down
Loading
Loading