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 )
0 commit comments