@@ -25,6 +25,7 @@ internal class ClassBase : ManagedType, IDeserializationCallback
25
25
[ NonSerialized ]
26
26
internal List < string > dotNetMembers = new ( ) ;
27
27
internal Indexer ? indexer ;
28
+ internal MethodBinder ? del ;
28
29
internal readonly Dictionary < int , MethodObject > richcompare = new ( ) ;
29
30
internal MaybeType type ;
30
31
@@ -465,6 +466,11 @@ static int mp_ass_subscript_impl(BorrowedReference ob, BorrowedReference idx, Bo
465
466
// with the index arg (method binders expect arg tuples).
466
467
NewReference argsTuple = default ;
467
468
469
+ if ( v . IsNull )
470
+ {
471
+ return DelImpl ( ob , idx , cls ) ;
472
+ }
473
+
468
474
if ( ! Runtime . PyTuple_Check ( idx ) )
469
475
{
470
476
argsTuple = Runtime . PyTuple_New ( 1 ) ;
@@ -501,6 +507,44 @@ static int mp_ass_subscript_impl(BorrowedReference ob, BorrowedReference idx, Bo
501
507
return result . IsNull ( ) ? - 1 : 0 ;
502
508
}
503
509
510
+ /// Implements __delitem__ (del x[...]) for IList<T> and IDictionary<TKey, TValue>.
511
+ private static int DelImpl ( BorrowedReference ob , BorrowedReference idx , ClassBase cls )
512
+ {
513
+ if ( cls . del is null )
514
+ {
515
+ Exceptions . SetError ( Exceptions . TypeError , "object does not support item deletion" ) ;
516
+ return - 1 ;
517
+ }
518
+
519
+ if ( Runtime . PyTuple_Check ( idx ) )
520
+ {
521
+ Exceptions . SetError ( Exceptions . TypeError , "multi-index deletion not supported" ) ;
522
+ return - 1 ;
523
+ }
524
+
525
+ using var argsTuple = Runtime . PyTuple_New ( 1 ) ;
526
+ Runtime . PyTuple_SetItem ( argsTuple . Borrow ( ) , 0 , idx ) ;
527
+ using var result = cls . del . Invoke ( ob , argsTuple . Borrow ( ) , kw : null ) ;
528
+ if ( result . IsNull ( ) )
529
+ return - 1 ;
530
+
531
+ if ( Runtime . PyBool_CheckExact ( result . Borrow ( ) ) )
532
+ {
533
+ if ( Runtime . PyObject_IsTrue ( result . Borrow ( ) ) != 0 )
534
+ return 0 ;
535
+
536
+ Exceptions . SetError ( Exceptions . KeyError , "key not found" ) ;
537
+ return - 1 ;
538
+ }
539
+
540
+ if ( ! result . IsNone ( ) )
541
+ {
542
+ Exceptions . warn ( "unsupported return type for __delitem__" , Exceptions . TypeError ) ;
543
+ }
544
+
545
+ return 0 ;
546
+ }
547
+
504
548
static NewReference tp_call_impl ( BorrowedReference ob , BorrowedReference args , BorrowedReference kw )
505
549
{
506
550
BorrowedReference tp = Runtime . PyObject_TYPE ( ob ) ;
0 commit comments