@@ -108,9 +108,9 @@ internal static void RestoreRuntimeData(RuntimeDataStorage storage)
108108 storage . GetValue ( "py_clr_module" , out py_clr_module ) ;
109109 var rootHandle = storage . GetValue < IntPtr > ( "root" ) ;
110110 root = ( CLRModule ) ManagedType . GetManagedObject ( rootHandle ) ;
111- IntPtr dict = Runtime . PyImport_GetModuleDict ( ) ;
112- Runtime . PyDict_SetItemString ( dict , "CLR" , py_clr_module ) ;
113- Runtime . PyDict_SetItemString ( dict , "clr" , py_clr_module ) ;
111+ BorrowedReference dict = Runtime . PyImport_GetModuleDict ( ) ;
112+ Runtime . PyDict_SetItemString ( dict . DangerousGetAddress ( ) , "CLR" , py_clr_module ) ;
113+ Runtime . PyDict_SetItemString ( dict . DangerousGetAddress ( ) , "clr" , py_clr_module ) ;
114114 SetupNamespaceTracking ( ) ;
115115 }
116116
@@ -122,15 +122,15 @@ internal static void RestoreRuntimeData(RuntimeDataStorage storage)
122122 /// </summary>
123123 static void SetupNamespaceTracking ( )
124124 {
125- var newset = Runtime . PySet_New ( IntPtr . Zero ) ;
125+ var newset = Runtime . PySet_New ( new BorrowedReference ( IntPtr . Zero ) ) ;
126126 try
127127 {
128128 foreach ( var ns in AssemblyManager . GetNamespaces ( ) )
129129 {
130130 var pyNs = Runtime . PyString_FromString ( ns ) ;
131131 try
132132 {
133- if ( Runtime . PySet_Add ( newset , pyNs ) != 0 )
133+ if ( Runtime . PySet_Add ( newset , new BorrowedReference ( pyNs ) ) != 0 )
134134 {
135135 throw new PythonException ( ) ;
136136 }
@@ -141,14 +141,14 @@ static void SetupNamespaceTracking ()
141141 }
142142 }
143143
144- if ( Runtime . PyDict_SetItemString ( root . dict , availableNsKey , newset ) != 0 )
144+ if ( Runtime . PyDict_SetItemString ( root . dict , availableNsKey , newset . DangerousGetAddress ( ) ) != 0 )
145145 {
146146 throw new PythonException ( ) ;
147147 }
148148 }
149149 finally
150150 {
151- Runtime . XDecref ( newset ) ;
151+ newset . Dispose ( ) ;
152152 }
153153
154154 AssemblyManager . namespaceAdded += OnNamespaceAdded ;
@@ -163,17 +163,13 @@ static void TeardownNameSpaceTracking()
163163 {
164164 AssemblyManager . namespaceAdded -= OnNamespaceAdded ;
165165 // If the C# runtime isn't loaded, then there is no namespaces available
166- if ( ( Runtime . PyDict_DelItemString ( root . dict , availableNsKey ) != 0 ) &&
166+ if ( ( Runtime . PyDict_DelItemString ( new BorrowedReference ( root . dict ) , availableNsKey ) != 0 ) &&
167167 ( Exceptions . ExceptionMatches ( Exceptions . KeyError ) ) )
168168 {
169169 // Trying to remove a key that's not in the dictionary
170170 // raises an error. We don't care about it.
171171 Runtime . PyErr_Clear ( ) ;
172172 }
173- else if ( Exceptions . ErrorOccurred ( ) )
174- {
175- throw new PythonException ( ) ;
176- }
177173 }
178174
179175 static void OnNamespaceAdded ( string name )
@@ -183,10 +179,10 @@ static void OnNamespaceAdded (string name)
183179 var pyNs = Runtime . PyString_FromString ( name ) ;
184180 try
185181 {
186- var nsSet = Runtime . PyDict_GetItemString ( root . dict , availableNsKey ) ;
187- if ( nsSet != IntPtr . Zero )
182+ var nsSet = Runtime . PyDict_GetItemString ( new BorrowedReference ( root . dict ) , availableNsKey ) ;
183+ if ( ! nsSet . IsNull )
188184 {
189- if ( Runtime . PySet_Add ( nsSet , pyNs ) != 0 )
185+ if ( Runtime . PySet_Add ( nsSet , new BorrowedReference ( pyNs ) ) != 0 )
190186 {
191187 throw new PythonException ( ) ;
192188 }
@@ -211,16 +207,15 @@ internal static void UpdateCLRModuleDict()
211207 // update the module dictionary with the contents of the root dictionary
212208 root . LoadNames ( ) ;
213209 BorrowedReference py_mod_dict = Runtime . PyModule_GetDict ( ClrModuleReference ) ;
214- using ( var clr_dict = Runtime . PyObject_GenericGetDict ( root . ObjectReference ) )
215- {
216- Runtime . PyDict_Update ( py_mod_dict , clr_dict ) ;
217- }
210+ using var clr_dict = Runtime . PyObject_GenericGetDict ( root . ObjectReference ) ;
211+
212+ Runtime . PyDict_Update ( py_mod_dict , clr_dict ) ;
218213 }
219214
220215 /// <summary>
221216 /// Return the clr python module (new reference)
222217 /// </summary>
223- public static unsafe NewReference GetCLRModule ( BorrowedReference fromList = default )
218+ public static unsafe NewReference GetCLRModule ( )
224219 {
225220 UpdateCLRModuleDict ( ) ;
226221 Runtime . XIncref ( py_clr_module ) ;
0 commit comments