🌐 AI搜索 & 代理 主页
Skip to content
Prev Previous commit
Next Next commit
refactored LoadExtraModules for Mixins into LoadSubmodule + LoadMixins
  • Loading branch information
lostmsu committed Sep 22, 2021
commit 6b20409df7b5d5787d165c587d91b7f37a815b1f
12 changes: 12 additions & 0 deletions src/runtime/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,17 @@ internal static void WriteCLong(IntPtr type, int offset, Int64 flags)
/// </summary>
internal static IntPtr Coalesce(this IntPtr primary, IntPtr fallback)
=> primary == IntPtr.Zero ? fallback : primary;

/// <summary>
/// Gets substring after last occurrence of <paramref name="symbol"/>
/// </summary>
internal static string AfterLast(this string str, char symbol)
{
if (str is null)
throw new ArgumentNullException(nameof(str));

int last = str.LastIndexOf(symbol);
return last >= 0 ? str.Substring(last + 1) : null;
}
}
}
37 changes: 23 additions & 14 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -235,7 +236,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true,
Exec(clr_py, module_globals, locals.Reference);
}

LoadExtraModules(module_globals);
LoadMixins(module_globals);

// add the imported module to the clr module, and copy the API functions
// and decorators into the main clr module.
Expand Down Expand Up @@ -267,23 +268,31 @@ static BorrowedReference DefineModule(string name)
return module;
}

static void LoadExtraModules(BorrowedReference targetModuleDict)
static void LoadMixins(BorrowedReference targetModuleDict)
{
Assembly assembly = Assembly.GetExecutingAssembly();
foreach (string nested in new[] { "collections" })
foreach (string nested in new[] {"collections"})
{
var module = DefineModule("clr._extras." + nested);
var module_globals = Runtime.PyModule_GetDict(module);
string resourceName = typeof(PythonEngine).Namespace + ".Mixins." + nested + ".py";
using (var stream = assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(stream))
{
string pyCode = reader.ReadToEnd();
Exec(pyCode, module_globals.DangerousGetAddress(), module_globals.DangerousGetAddress());
}
LoadSubmodule(targetModuleDict,
fullName: "clr._extras." + nested,
resourceName: typeof(PythonEngine).Namespace + ".Mixins." + nested + ".py");
}
}

Runtime.PyDict_SetItemString(targetModuleDict, nested, module);
static void LoadSubmodule(BorrowedReference targetModuleDict, string fullName, string resourceName)
{
string memberName = fullName.AfterLast('.');
Debug.Assert(memberName != null);
Assembly assembly = Assembly.GetExecutingAssembly();
var module = DefineModule(fullName);
var module_globals = Runtime.PyModule_GetDict(module);
using (var stream = assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(stream))
{
string pyCode = reader.ReadToEnd();
Exec(pyCode, module_globals.DangerousGetAddress(), module_globals.DangerousGetAddress());
}

Runtime.PyDict_SetItemString(targetModuleDict, memberName, module);
}

static void OnDomainUnload(object _, EventArgs __)
Expand Down