@@ -25,6 +25,7 @@ internal class ClassBase : ManagedType, IDeserializationCallback
2525 [ NonSerialized ]
2626 internal List < string > dotNetMembers = new ( ) ;
2727 internal Indexer ? indexer ;
28+ internal MethodBinder ? del ;
2829 internal readonly Dictionary < int , MethodObject > richcompare = new ( ) ;
2930 internal MaybeType type ;
3031
@@ -465,6 +466,11 @@ static int mp_ass_subscript_impl(BorrowedReference ob, BorrowedReference idx, Bo
465466 // with the index arg (method binders expect arg tuples).
466467 NewReference argsTuple = default ;
467468
469+ if ( v . IsNull )
470+ {
471+ return DelImpl ( ob , idx , cls ) ;
472+ }
473+
468474 if ( ! Runtime . PyTuple_Check ( idx ) )
469475 {
470476 argsTuple = Runtime . PyTuple_New ( 1 ) ;
@@ -501,6 +507,44 @@ static int mp_ass_subscript_impl(BorrowedReference ob, BorrowedReference idx, Bo
501507 return result . IsNull ( ) ? - 1 : 0 ;
502508 }
503509
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 , "indices must be integers" ) ;
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+
504548 static NewReference tp_call_impl ( BorrowedReference ob , BorrowedReference args , BorrowedReference kw )
505549 {
506550 BorrowedReference tp = Runtime . PyObject_TYPE ( ob ) ;
0 commit comments