11using System ;
22using System . Collections . Concurrent ;
3+ using System . Diagnostics ;
34
45namespace Python . Runtime
56{
@@ -10,7 +11,14 @@ internal static class ImportHook
1011 {
1112 private static CLRModule root ;
1213 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+ }
1422
1523 private const string LoaderCode = @"
1624import importlib.abc
@@ -43,7 +51,7 @@ def find_spec(klass, fullname, paths=None, target=None):
4351 return importlib.machinery.ModuleSpec(fullname, DotNetLoader(), is_package=True)
4452 return None
4553 " ;
46- const string availableNsKey = "_available_namespaces" ;
54+ const string _available_namespaces = "_available_namespaces" ;
4755
4856 /// <summary>
4957 /// Initialization performed on startup of the Python runtime.
@@ -154,12 +162,11 @@ static void SetupNamespaceTracking()
154162 {
155163 throw PythonException . ThrowLastAsClrException ( ) ;
156164 }
157- if ( Runtime . PyDict_SetItemString ( root . DictRef , availableNsKey , newset ) != 0 )
158- {
159- throw PythonException . ThrowLastAsClrException ( ) ;
160- }
161165 }
162-
166+ if ( Runtime . PyDict_SetItemString ( root . DictRef , _available_namespaces , newset ) != 0 )
167+ {
168+ throw PythonException . ThrowLastAsClrException ( ) ;
169+ }
163170 }
164171
165172 /// <summary>
@@ -168,7 +175,7 @@ static void SetupNamespaceTracking()
168175 static void TeardownNameSpaceTracking ( )
169176 {
170177 // 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 ) ;
172179 }
173180
174181 static readonly ConcurrentQueue < string > addPending = new ( ) ;
@@ -190,7 +197,7 @@ internal static void AddNamespaceWithGIL(string name)
190197 var pyNs = Runtime . PyString_FromString ( name ) ;
191198 try
192199 {
193- var nsSet = Runtime . PyDict_GetItemString ( root . DictRef , availableNsKey ) ;
200+ var nsSet = Runtime . PyDict_GetItemString ( root . DictRef , _available_namespaces ) ;
194201 if ( ! ( nsSet . IsNull || nsSet . DangerousGetAddress ( ) == Runtime . PyNone ) )
195202 {
196203 if ( Runtime . PySet_Add ( nsSet , new BorrowedReference ( pyNs ) ) != 0 )
0 commit comments