@@ -539,6 +539,13 @@ func dictContains(f *Frame, seq, value *Object) (*Object, *BaseException) {
539
539
return GetBool (item != nil ).ToObject (), nil
540
540
}
541
541
542
+ func dictCopy (f * Frame , args Args , _ KWArgs ) (* Object , * BaseException ) {
543
+ if raised := checkMethodArgs (f , "copy" , args , DictType ); raised != nil {
544
+ return nil , raised
545
+ }
546
+ return DictType .Call (f , args , nil )
547
+ }
548
+
542
549
func dictDelItem (f * Frame , o , key * Object ) * BaseException {
543
550
deleted , raised := toDictUnsafe (o ).DelItem (f , key )
544
551
if raised != nil {
@@ -634,28 +641,6 @@ func dictKeys(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
634
641
return toDictUnsafe (args [0 ]).Keys (f ).ToObject (), nil
635
642
}
636
643
637
- func dictPop (f * Frame , args Args , _ KWArgs ) (* Object , * BaseException ) {
638
- expectedTypes := []* Type {DictType , ObjectType , ObjectType }
639
- argc := len (args )
640
- if argc == 2 {
641
- expectedTypes = expectedTypes [:2 ]
642
- }
643
- if raised := checkMethodArgs (f , "pop" , args , expectedTypes ... ); raised != nil {
644
- return nil , raised
645
- }
646
- key := args [1 ]
647
- d := toDictUnsafe (args [0 ])
648
- item , raised := d .Pop (f , key )
649
- if raised == nil && item == nil {
650
- if argc > 2 {
651
- item = args [2 ]
652
- } else {
653
- raised = raiseKeyError (f , key )
654
- }
655
- }
656
- return item , raised
657
- }
658
-
659
644
func dictGetItem (f * Frame , o , key * Object ) (* Object , * BaseException ) {
660
645
item , raised := toDictUnsafe (o ).GetItem (f , key )
661
646
if raised != nil {
@@ -721,6 +706,48 @@ func dictNew(f *Frame, t *Type, _ Args, _ KWArgs) (*Object, *BaseException) {
721
706
return d .ToObject (), nil
722
707
}
723
708
709
+ func dictPop (f * Frame , args Args , _ KWArgs ) (* Object , * BaseException ) {
710
+ expectedTypes := []* Type {DictType , ObjectType , ObjectType }
711
+ argc := len (args )
712
+ if argc == 2 {
713
+ expectedTypes = expectedTypes [:2 ]
714
+ }
715
+ if raised := checkMethodArgs (f , "pop" , args , expectedTypes ... ); raised != nil {
716
+ return nil , raised
717
+ }
718
+ key := args [1 ]
719
+ d := toDictUnsafe (args [0 ])
720
+ item , raised := d .Pop (f , key )
721
+ if raised == nil && item == nil {
722
+ if argc > 2 {
723
+ item = args [2 ]
724
+ } else {
725
+ raised = raiseKeyError (f , key )
726
+ }
727
+ }
728
+ return item , raised
729
+ }
730
+
731
+ func dictPopItem (f * Frame , args Args , _ KWArgs ) (item * Object , raised * BaseException ) {
732
+ if raised := checkMethodArgs (f , "popitem" , args , DictType ); raised != nil {
733
+ return nil , raised
734
+ }
735
+ d := toDictUnsafe (args [0 ])
736
+ d .mutex .Lock (f )
737
+ iter := newDictEntryIterator (d )
738
+ entry := iter .next ()
739
+ if entry == nil {
740
+ raised = f .RaiseType (KeyErrorType , "popitem(): dictionary is empty" )
741
+ } else {
742
+ item = NewTuple (entry .key , entry .value ).ToObject ()
743
+ d .table .storeEntry (int (iter .index - 1 ), deletedEntry )
744
+ d .table .incUsed (- 1 )
745
+ d .incVersion ()
746
+ }
747
+ d .mutex .Unlock (f )
748
+ return item , raised
749
+ }
750
+
724
751
func dictRepr (f * Frame , o * Object ) (* Object , * BaseException ) {
725
752
d := toDictUnsafe (o )
726
753
if f .reprEnter (d .ToObject ()) {
@@ -825,6 +852,7 @@ func dictValues(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
825
852
826
853
func initDictType (dict map [string ]* Object ) {
827
854
dict ["clear" ] = newBuiltinFunction ("clear" , dictClear ).ToObject ()
855
+ dict ["copy" ] = newBuiltinFunction ("copy" , dictCopy ).ToObject ()
828
856
dict ["get" ] = newBuiltinFunction ("get" , dictGet ).ToObject ()
829
857
dict ["has_key" ] = newBuiltinFunction ("has_key" , dictHasKey ).ToObject ()
830
858
dict ["items" ] = newBuiltinFunction ("items" , dictItems ).ToObject ()
@@ -833,6 +861,7 @@ func initDictType(dict map[string]*Object) {
833
861
dict ["itervalues" ] = newBuiltinFunction ("itervalues" , dictIterValues ).ToObject ()
834
862
dict ["keys" ] = newBuiltinFunction ("keys" , dictKeys ).ToObject ()
835
863
dict ["pop" ] = newBuiltinFunction ("pop" , dictPop ).ToObject ()
864
+ dict ["popitem" ] = newBuiltinFunction ("popitem" , dictPopItem ).ToObject ()
836
865
dict ["setdefault" ] = newBuiltinFunction ("setdefault" , dictSetDefault ).ToObject ()
837
866
dict ["update" ] = newBuiltinFunction ("update" , dictUpdate ).ToObject ()
838
867
dict ["values" ] = newBuiltinFunction ("values" , dictValues ).ToObject ()
0 commit comments