| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent bc3265d commit 3f335db
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -112,3 +112,14 @@ import clr | |||
| 112 | 112 | } | |
| 113 | 113 | } | |
| 114 | 114 | } | |
| 115 | + | ||
| 116 | + // regression for https://github.com/pythonnet/pythonnet/issues/1601 | ||
| 117 | + // initialize fails if a class derived from IEnumerable is in global namespace | ||
| 118 | + public class PublicEnumerator : System.Collections.IEnumerable | ||
| 119 | + { | ||
| 120 | + public System.Collections.IEnumerator GetEnumerator() | ||
| 121 | + { | ||
| 122 | + return null; | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Collections.Concurrent; | |
| 3 | + using System.Diagnostics; | ||
| 3 | 4 | ||
| 4 | 5 | namespace Python.Runtime | |
| 5 | 6 | { | |
@@ -10,7 +11,14 @@ internal static class ImportHook | |||
| 10 | 11 | { | |
| 11 | 12 | private static CLRModule root; | |
| 12 | 13 | private static IntPtr py_clr_module; | |
| 13 | - static BorrowedReference ClrModuleReference => new BorrowedReference(py_clr_module); | ||
| 14 | + internal static BorrowedReference ClrModuleReference | ||
| 15 | + { | ||
| 16 | + get | ||
| 17 | + { | ||
| 18 | + Debug.Assert(py_clr_module != IntPtr.Zero); | ||
| 19 | + return new BorrowedReference(py_clr_module); | ||
| 20 | + } | ||
| 21 | + } | ||
| 14 | 22 | ||
| 15 | 23 | private const string LoaderCode = @" | |
| 16 | 24 | import importlib.abc | |
@@ -43,7 +51,7 @@ def find_spec(klass, fullname, paths=None, target=None): | |||
| 43 | 51 | return importlib.machinery.ModuleSpec(fullname, DotNetLoader(), is_package=True) | |
| 44 | 52 | return None | |
| 45 | 53 | "; | |
| 46 | - const string availableNsKey = "_available_namespaces"; | ||
| 54 | + const string _available_namespaces = "_available_namespaces"; | ||
| 47 | 55 | ||
| 48 | 56 | /// <summary> | |
| 49 | 57 | /// Initialization performed on startup of the Python runtime. | |
@@ -154,12 +162,11 @@ static void SetupNamespaceTracking() | |||
| 154 | 162 | { | |
| 155 | 163 | throw PythonException.ThrowLastAsClrException(); | |
| 156 | 164 | } | |
| 157 | - if (Runtime.PyDict_SetItemString(root.DictRef, availableNsKey, newset) != 0) | ||
| 158 | - { | ||
| 159 | - throw PythonException.ThrowLastAsClrException(); | ||
| 160 | - } | ||
| 161 | 165 | } | |
| 162 | - | ||
| 166 | + if (Runtime.PyDict_SetItemString(root.DictRef, _available_namespaces, newset) != 0) | ||
| 167 | + { | ||
| 168 | + throw PythonException.ThrowLastAsClrException(); | ||
| 169 | + } | ||
| 163 | 170 | } | |
| 164 | 171 | ||
| 165 | 172 | /// <summary> | |
@@ -168,7 +175,7 @@ static void SetupNamespaceTracking() | |||
| 168 | 175 | static void TeardownNameSpaceTracking() | |
| 169 | 176 | { | |
| 170 | 177 | // If the C# runtime isn't loaded, then there are no namespaces available | |
| 171 | - Runtime.PyDict_SetItemString(root.dict, availableNsKey, Runtime.PyNone); | ||
| 178 | + Runtime.PyDict_SetItemString(root.dict, _available_namespaces, Runtime.PyNone); | ||
| 172 | 179 | } | |
| 173 | 180 | ||
| 174 | 181 | static readonly ConcurrentQueue<string> addPending = new(); | |
@@ -190,7 +197,7 @@ internal static void AddNamespaceWithGIL(string name) | |||
| 190 | 197 | var pyNs = Runtime.PyString_FromString(name); | |
| 191 | 198 | try | |
| 192 | 199 | { | |
| 193 | - var nsSet = Runtime.PyDict_GetItemString(root.DictRef, availableNsKey); | ||
| 200 | + var nsSet = Runtime.PyDict_GetItemString(root.DictRef, _available_namespaces); | ||
| 194 | 201 | if (!(nsSet.IsNull || nsSet.DangerousGetAddress() == Runtime.PyNone)) | |
| 195 | 202 | { | |
| 196 | 203 | if (Runtime.PySet_Add(nsSet, new BorrowedReference(pyNs)) != 0) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -220,8 +220,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true, | |||
| 220 | 220 | } | |
| 221 | 221 | ||
| 222 | 222 | // Load the clr.py resource into the clr module | |
| 223 | - NewReference clr = Python.Runtime.ImportHook.GetCLRModule(); | ||
| 224 | - BorrowedReference clr_dict = Runtime.PyModule_GetDict(clr); | ||
| 223 | + BorrowedReference clr_dict = Runtime.PyModule_GetDict(ImportHook.ClrModuleReference); | ||
| 225 | 224 | ||
| 226 | 225 | var locals = new PyDict(); | |
| 227 | 226 | try | |
@@ -236,7 +235,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true, | |||
| 236 | 235 | ||
| 237 | 236 | LoadSubmodule(module_globals, "clr.interop", "interop.py"); | |
| 238 | 237 | ||
| 239 | - LoadMixins(module_globals); | ||
| 238 | + LoadMixins(module_globals); | ||
| 240 | 239 | ||
| 241 | 240 | // add the imported module to the clr module, and copy the API functions | |
| 242 | 241 | // and decorators into the main clr module. | |
@@ -257,6 +256,8 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true, | |||
| 257 | 256 | { | |
| 258 | 257 | locals.Dispose(); | |
| 259 | 258 | } | |
| 259 | + | ||
| 260 | + ImportHook.UpdateCLRModuleDict(); | ||
| 260 | 261 | } | |
| 261 | 262 | ||
| 262 | 263 | static BorrowedReference DefineModule(string name) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments