Skip to content

Commit cd73445

Browse files
committed
Added load resource stream hook
1 parent 30ff963 commit cd73445

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/runtime/pythonengine.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -7,11 +7,18 @@
77

88
namespace Python.Runtime
99
{
10+
public class ResolveStreamResouceArgs : EventArgs
11+
{
12+
public string Name { get; set; }
13+
}
14+
1015
/// <summary>
1116
/// This class provides the public interface of the Python runtime.
1217
/// </summary>
1318
public class PythonEngine : IDisposable
1419
{
20+
public static event Func<ResolveStreamResouceArgs, Stream> StreamResouceResolve;
21+
1522
private static DelegateManager delegateManager;
1623
private static bool initialized;
1724
private static IntPtr _pythonHome = IntPtr.Zero;
@@ -202,8 +209,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true)
202209
IntPtr builtins = Runtime.PyEval_GetBuiltins();
203210
Runtime.PyDict_SetItemString(module_globals, "__builtins__", builtins);
204211

205-
Assembly assembly = Assembly.GetExecutingAssembly();
206-
using (Stream stream = assembly.GetManifestResourceStream("clr.py"))
212+
using (Stream stream = LoadResource("clr.py"))
207213
using (var reader = new StreamReader(stream))
208214
{
209215
// add the contents of clr.py to the module
@@ -534,6 +540,26 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
534540
}
535541
}
536542
}
543+
544+
private static Stream LoadResource(string name)
545+
{
546+
var args = new ResolveStreamResouceArgs()
547+
{
548+
Name = name
549+
};
550+
Stream stream;
551+
foreach (Func<ResolveStreamResouceArgs, Stream> resolver in StreamResouceResolve.GetInvocationList())
552+
{
553+
stream = resolver(args);
554+
if (stream != null)
555+
{
556+
return stream;
557+
}
558+
}
559+
Assembly assembly = Assembly.GetExecutingAssembly();
560+
stream = assembly.GetManifestResourceStream("clr.py");
561+
return stream;
562+
}
537563
}
538564

539565
public enum RunFlagType

0 commit comments

Comments
 (0)