@@ -311,42 +311,27 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr
311
311
object assembly = null ;
312
312
object namespaceStr = null ;
313
313
314
- var disposeList = new List < PyObject > ( ) ;
315
- try
314
+ using ( var assemblyKey = new PyString ( "__assembly__" ) )
316
315
{
317
- var assemblyKey = new PyObject ( Converter . ToPython ( "__assembly__" , typeof ( string ) ) ) ;
318
- disposeList . Add ( assemblyKey ) ;
319
- if ( 0 != Runtime . PyMapping_HasKey ( py_dict , assemblyKey . Handle ) )
316
+ IntPtr assemblyPtr = Runtime . PyDict_GetItem ( py_dict , assemblyKey . Handle ) ;
317
+ if ( assemblyPtr == IntPtr . Zero ) return IntPtr . Zero ;
318
+
319
+ if ( ! Converter . ToManagedValue ( assemblyPtr , typeof ( string ) , out assembly , false ) )
320
320
{
321
- var pyAssembly = new PyObject ( Runtime . PyDict_GetItem ( py_dict , assemblyKey . Handle ) ) ;
322
- Runtime . XIncref ( pyAssembly . Handle ) ;
323
- disposeList . Add ( pyAssembly ) ;
324
- if ( ! Converter . ToManagedValue ( pyAssembly . Handle , typeof ( string ) , out assembly , false ) )
325
- {
326
- throw new InvalidCastException ( "Couldn't convert __assembly__ value to string" ) ;
327
- }
321
+ return Exceptions . RaiseTypeError ( "Couldn't convert __assembly__ value to string" ) ;
328
322
}
329
323
330
- var namespaceKey = new PyObject ( Converter . ToPythonImplicit ( "__namespace__" ) ) ;
331
- disposeList . Add ( namespaceKey ) ;
332
- if ( 0 != Runtime . PyMapping_HasKey ( py_dict , namespaceKey . Handle ) )
324
+ using ( var namespaceKey = new PyString ( "__namespace__" ) )
333
325
{
334
- var pyNamespace = new PyObject ( Runtime . PyDict_GetItem ( py_dict , namespaceKey . Handle ) ) ;
335
- Runtime . XIncref ( pyNamespace . Handle ) ;
336
- disposeList . Add ( pyNamespace ) ;
337
- if ( ! Converter . ToManagedValue ( pyNamespace . Handle , typeof ( string ) , out namespaceStr , false ) )
326
+ IntPtr pyNamespace = Runtime . PyDict_GetItem ( py_dict , namespaceKey . Handle ) ;
327
+ if ( pyNamespace == IntPtr . Zero ) return IntPtr . Zero ;
328
+
329
+ if ( ! Converter . ToManagedValue ( pyNamespace , typeof ( string ) , out namespaceStr , false ) )
338
330
{
339
- throw new InvalidCastException ( "Couldn't convert __namespace__ value to string" ) ;
331
+ return Exceptions . RaiseTypeError ( "Couldn't convert __namespace__ value to string" ) ;
340
332
}
341
333
}
342
334
}
343
- finally
344
- {
345
- foreach ( PyObject o in disposeList )
346
- {
347
- o . Dispose ( ) ;
348
- }
349
- }
350
335
351
336
// create the new managed type subclassing the base managed type
352
337
var baseClass = ManagedType . GetManagedObject ( py_base_type ) as ClassBase ;
@@ -370,7 +355,7 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr
370
355
// by default the class dict will have all the C# methods in it, but as this is a
371
356
// derived class we want the python overrides in there instead if they exist.
372
357
IntPtr cls_dict = Marshal . ReadIntPtr ( py_type , TypeOffset . tp_dict ) ;
373
- Runtime . PyDict_Update ( cls_dict , py_dict ) ;
358
+ PythonException . ThrowIfIsNotZero ( Runtime . PyDict_Update ( cls_dict , py_dict ) ) ;
374
359
Runtime . XIncref ( py_type ) ;
375
360
// Update the __classcell__ if it exists
376
361
var cell = new BorrowedReference ( Runtime . PyDict_GetItemString ( cls_dict , "__classcell__" ) ) ;