Skip to content

Commit 88e54a8

Browse files
authored
use config for shim (#6)
remove some tests that rely on dynamo for test discovery
1 parent f188544 commit 88e54a8

10 files changed

+104
-452
lines changed

DSIronPython/DSIronPython.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@
1919

2020
<PackageReference Include="IronPython" Version="2.7.12" />
2121
<PackageReference Include="IronPython.StdLib" Version="2.7.12" />
22-
22+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
2323

2424
</ItemGroup>
2525
<ItemGroup>
2626
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
2727
<_Parameter1>IronPythonTests</_Parameter1>
2828
</AssemblyAttribute>
2929
</ItemGroup>
30+
<ItemGroup>
31+
<None Update="appsettings.json">
32+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
33+
</None>
34+
</ItemGroup>
3035

3136
<Target Name="copypkgjson" AfterTargets="Build">
3237
<Copy SourceFiles="pkg.json" DestinationFolder="$(OutputPath)..\" />

DSIronPython/IronPythonEvaluator.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using Dynamo.Session;
1313
using Dynamo.Utilities;
1414
using IronPython.Hosting;
15-
15+
using Microsoft.Extensions.Configuration;
1616
using Microsoft.Scripting.Hosting;
1717
using Microsoft.Scripting.Utils;
1818

@@ -25,6 +25,8 @@ namespace DSIronPython
2525
[IsVisibleInDynamoLibrary(false)]
2626
public class IronPythonEvaluator : Dynamo.PythonServices.PythonEngine
2727
{
28+
private bool CONFIG_ENABLE_NET48SHIMCOMPAT = false;
29+
2830
private const string DynamoPrintFuncName = "__dynamoprint__";
2931
/// <summary> stores a copy of the previously executed code</summary>
3032
private static string prev_code { get; set; }
@@ -120,7 +122,18 @@ public override object Evaluate(
120122
if (code != prev_code)
121123
{
122124
ScriptEngine PythonEngine = Python.CreateEngine();
123-
if (!string.IsNullOrEmpty(stdLib))
125+
//to maintain compatability with ironPython code written in .netframework - we load the system.dll shim
126+
//which loads many types which used to be in system.dll(mscorlib) like system.diagnostics.process
127+
//which were moved in .Net. These types are now importable by python code without users needing to add
128+
//clr.addreference.
129+
//TODO consider a preference for this.
130+
//TODO test smaller shims...netstd.dll
131+
132+
if (CONFIG_ENABLE_NET48SHIMCOMPAT)
133+
{
134+
PythonEngine.Runtime.LoadAssembly(Assembly.Load("System"));
135+
}
136+
if (!string.IsNullOrEmpty(stdLib))
124137
{
125138
paths = PythonEngine.GetSearchPaths().ToList();
126139
paths.Add(stdLib);
@@ -366,5 +379,23 @@ private void OnEvaluationEnd( bool isSuccessful,
366379

367380
#endregion
368381

382+
internal IronPythonEvaluator(IConfiguration config = null)
383+
{
384+
//either inject a config or load from appsettings.json
385+
if (config is not null)
386+
{
387+
388+
}
389+
else
390+
{
391+
var configPath = Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, "appsettings.json");
392+
config = new ConfigurationBuilder().AddJsonFile(configPath, optional: true).Build();
393+
}
394+
var enableShimLoad = config.GetSection("config").GetChildren().FirstOrDefault(x => x.Key == nameof(CONFIG_ENABLE_NET48SHIMCOMPAT))?.Value;
395+
if (bool.TryParse(enableShimLoad, out var parsed))
396+
{
397+
CONFIG_ENABLE_NET48SHIMCOMPAT = parsed;
398+
}
399+
}
369400
}
370401
}

DSIronPython/appsettings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"config": {
3+
"CONFIG_ENABLE_NET48SHIMCOMPAT": "true"
4+
}
5+
}

DSIronPython/pkg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"license": "",
33
"file_hash": null,
44
"name": "DynamoIronPython2.7",
5-
"version": "3.0.0",
5+
"version": "3.1.0",
66
"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.",
77
"group": "",
88
"keywords": [ "python", "ironpython" ],

IronPythonExtension/IronPythonExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static void LoadPythonEngine(Assembly assembly)
9595
{
9696
if (assembly == null)
9797
{
98-
return;
98+
throw new ArgumentNullException($"Error while loading python engine - assembly {PythonEvaluatorAssembly}.dll was not loaded successfully.");
9999
}
100100

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

0 commit comments

Comments
 (0)