File tree Expand file tree Collapse file tree 4 files changed +31
-12
lines changed Expand file tree Collapse file tree 4 files changed +31
-12
lines changed Original file line number Diff line number Diff line change @@ -360,21 +360,33 @@ public static void tp_dealloc(IntPtr ob)
360
360
361
361
public static int tp_clear ( IntPtr ob )
362
362
{
363
- ManagedType self = GetManagedObject ( ob ) ;
363
+ var self = ( CLRObject ) GetManagedObject ( ob ) ;
364
364
365
- bool isTypeObject = Runtime . PyObject_TYPE ( ob ) == Runtime . PyCLRMetaType ;
366
- if ( ! isTypeObject )
367
- {
368
- ClearObjectDict ( ob ) ;
365
+ if ( self . clearReentryGuard ) return 0 ;
369
366
370
- int baseClearResult = BaseUnmanagedClear ( ob ) ;
371
- if ( baseClearResult != 0 )
367
+ // workaround for https://bugs.python.org/issue45266
368
+ self . clearReentryGuard = true ;
369
+
370
+ try
371
+ {
372
+ bool isTypeObject = Runtime . PyObject_TYPE ( ob ) == Runtime . PyCLRMetaType ;
373
+ if ( ! isTypeObject )
372
374
{
373
- return baseClearResult ;
375
+ int baseClearResult = BaseUnmanagedClear ( ob ) ;
376
+ if ( baseClearResult != 0 )
377
+ {
378
+ return baseClearResult ;
379
+ }
380
+
381
+ ClearObjectDict ( ob ) ;
374
382
}
383
+ if ( self is not null ) self . tpHandle = IntPtr . Zero ;
384
+ return 0 ;
385
+ }
386
+ finally
387
+ {
388
+ self . clearReentryGuard = false ;
375
389
}
376
- if ( self is not null ) self . tpHandle = IntPtr . Zero ;
377
- return 0 ;
378
390
}
379
391
380
392
static unsafe int BaseUnmanagedClear ( IntPtr ob )
Original file line number Diff line number Diff line change @@ -9,10 +9,11 @@ namespace Python.Runtime
9
9
internal class CLRObject : ManagedType
10
10
{
11
11
internal object inst ;
12
+ internal bool clearReentryGuard ;
12
13
13
14
internal CLRObject ( object ob , IntPtr tp )
14
15
{
15
- System . Diagnostics . Debug . Assert ( tp != IntPtr . Zero ) ;
16
+ Debug . Assert ( tp != IntPtr . Zero ) ;
16
17
IntPtr py = Runtime . PyType_GenericAlloc ( tp , 0 ) ;
17
18
18
19
tpHandle = tp ;
Original file line number Diff line number Diff line change @@ -145,7 +145,7 @@ internal static bool IsInstanceOfManagedType(IntPtr ob)
145
145
146
146
internal static bool IsManagedType ( BorrowedReference type )
147
147
{
148
- var flags = ( TypeFlags ) Util . ReadCLong ( type . DangerousGetAddress ( ) , TypeOffset . tp_flags ) ;
148
+ var flags = PyType . GetFlags ( type ) ;
149
149
return ( flags & TypeFlags . HasClrInstance ) != 0 ;
150
150
}
151
151
Original file line number Diff line number Diff line change @@ -103,6 +103,12 @@ internal IntPtr GetSlot(TypeSlotID slot)
103
103
return Exceptions . ErrorCheckIfNull ( result ) ;
104
104
}
105
105
106
+ internal static TypeFlags GetFlags ( BorrowedReference type )
107
+ {
108
+ Debug . Assert ( TypeOffset . tp_flags > 0 ) ;
109
+ return ( TypeFlags ) Util . ReadCLong ( type . DangerousGetAddress ( ) , TypeOffset . tp_flags ) ;
110
+ }
111
+
106
112
internal static BorrowedReference GetBase ( BorrowedReference type )
107
113
{
108
114
Debug . Assert ( IsType ( type ) ) ;
You can’t perform that action at this time.
0 commit comments