@@ -907,7 +907,7 @@ mod array {
907907 range : OptionalRangeArgs ,
908908 vm : & VirtualMachine ,
909909 ) -> PyResult < usize > {
910- let ( start, stop) = range. saturate ( self . len ( ) , vm) ?;
910+ let ( start, stop) = range. saturate ( self . __len__ ( ) , vm) ?;
911911 self . read ( ) . index ( x, start, stop, vm)
912912 }
913913
@@ -976,29 +976,29 @@ mod array {
976976 self . write ( ) . reverse ( )
977977 }
978978
979- #[ pymethod( magic ) ]
980- fn copy ( & self ) -> PyArray {
979+ #[ pymethod]
980+ fn __copy__ ( & self ) -> PyArray {
981981 self . array . read ( ) . clone ( ) . into ( )
982982 }
983983
984- #[ pymethod( magic ) ]
985- fn deepcopy ( & self , _memo : PyObjectRef ) -> PyArray {
986- self . copy ( )
984+ #[ pymethod]
985+ fn __deepcopy__ ( & self , _memo : PyObjectRef ) -> PyArray {
986+ self . __copy__ ( )
987987 }
988988
989- fn _getitem ( & self , needle : & PyObject , vm : & VirtualMachine ) -> PyResult {
989+ fn getitem_inner ( & self , needle : & PyObject , vm : & VirtualMachine ) -> PyResult {
990990 match SequenceIndex :: try_from_borrowed_object ( vm, needle, "array" ) ? {
991991 SequenceIndex :: Int ( i) => self . read ( ) . getitem_by_index ( i, vm) ,
992992 SequenceIndex :: Slice ( slice) => self . read ( ) . getitem_by_slice ( slice, vm) ,
993993 }
994994 }
995995
996- #[ pymethod( magic ) ]
997- fn getitem ( & self , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
998- self . _getitem ( & needle, vm)
996+ #[ pymethod]
997+ fn __getitem__ ( & self , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
998+ self . getitem_inner ( & needle, vm)
999999 }
10001000
1001- fn _setitem (
1001+ fn setitem_inner (
10021002 zelf : & Py < Self > ,
10031003 needle : & PyObject ,
10041004 value : PyObjectRef ,
@@ -1035,30 +1035,30 @@ mod array {
10351035 }
10361036 }
10371037
1038- #[ pymethod( magic ) ]
1039- fn setitem (
1038+ #[ pymethod]
1039+ fn __setitem__ (
10401040 zelf : & Py < Self > ,
10411041 needle : PyObjectRef ,
10421042 value : PyObjectRef ,
10431043 vm : & VirtualMachine ,
10441044 ) -> PyResult < ( ) > {
1045- Self :: _setitem ( zelf, & needle, value, vm)
1045+ Self :: setitem_inner ( zelf, & needle, value, vm)
10461046 }
10471047
1048- fn _delitem ( & self , needle : & PyObject , vm : & VirtualMachine ) -> PyResult < ( ) > {
1048+ fn delitem_inner ( & self , needle : & PyObject , vm : & VirtualMachine ) -> PyResult < ( ) > {
10491049 match SequenceIndex :: try_from_borrowed_object ( vm, needle, "array" ) ? {
10501050 SequenceIndex :: Int ( i) => self . try_resizable ( vm) ?. delitem_by_index ( i, vm) ,
10511051 SequenceIndex :: Slice ( slice) => self . try_resizable ( vm) ?. delitem_by_slice ( slice, vm) ,
10521052 }
10531053 }
10541054
1055- #[ pymethod( magic ) ]
1056- fn delitem ( & self , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
1057- self . _delitem ( & needle, vm)
1055+ #[ pymethod]
1056+ fn __delitem__ ( & self , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
1057+ self . delitem_inner ( & needle, vm)
10581058 }
10591059
1060- #[ pymethod( magic ) ]
1061- fn add ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyRef < Self > > {
1060+ #[ pymethod]
1061+ fn __add__ ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyRef < Self > > {
10621062 if let Some ( other) = other. payload :: < PyArray > ( ) {
10631063 self . read ( )
10641064 . add ( & other. read ( ) , vm)
@@ -1071,8 +1071,8 @@ mod array {
10711071 }
10721072 }
10731073
1074- #[ pymethod( magic ) ]
1075- fn iadd (
1074+ #[ pymethod]
1075+ fn __iadd__ (
10761076 zelf : PyRef < Self > ,
10771077 other : PyObjectRef ,
10781078 vm : & VirtualMachine ,
@@ -1091,28 +1091,28 @@ mod array {
10911091 }
10921092
10931093 #[ pymethod( name = "__rmul__" ) ]
1094- #[ pymethod( magic ) ]
1095- fn mul ( & self , value : isize , vm : & VirtualMachine ) -> PyResult < PyRef < Self > > {
1094+ #[ pymethod]
1095+ fn __mul__ ( & self , value : isize , vm : & VirtualMachine ) -> PyResult < PyRef < Self > > {
10961096 self . read ( )
10971097 . mul ( value, vm)
10981098 . map ( |x| Self :: from ( x) . into_ref ( & vm. ctx ) )
10991099 }
11001100
1101- #[ pymethod( magic ) ]
1102- fn imul ( zelf : PyRef < Self > , value : isize , vm : & VirtualMachine ) -> PyResult < PyRef < Self > > {
1101+ #[ pymethod]
1102+ fn __imul__ ( zelf : PyRef < Self > , value : isize , vm : & VirtualMachine ) -> PyResult < PyRef < Self > > {
11031103 zelf. try_resizable ( vm) ?. imul ( value, vm) ?;
11041104 Ok ( zelf)
11051105 }
11061106
1107- #[ pymethod( magic ) ]
1108- pub ( crate ) fn len ( & self ) -> usize {
1107+ #[ pymethod]
1108+ pub ( crate ) fn __len__ ( & self ) -> usize {
11091109 self . read ( ) . len ( )
11101110 }
11111111
11121112 fn array_eq ( & self , other : & Self , vm : & VirtualMachine ) -> PyResult < bool > {
11131113 // we cannot use zelf.is(other) for shortcut because if we contenting a
11141114 // float value NaN we always return False even they are the same object.
1115- if self . len ( ) != other. len ( ) {
1115+ if self . __len__ ( ) != other. __len__ ( ) {
11161116 return Ok ( false ) ;
11171117 }
11181118 let array_a = self . read ( ) ;
@@ -1133,14 +1133,14 @@ mod array {
11331133 Ok ( true )
11341134 }
11351135
1136- #[ pymethod( magic ) ]
1137- fn reduce_ex (
1136+ #[ pymethod]
1137+ fn __reduce_ex__ (
11381138 zelf : & Py < Self > ,
11391139 proto : usize ,
11401140 vm : & VirtualMachine ,
11411141 ) -> PyResult < ( PyObjectRef , PyTupleRef , Option < PyDictRef > ) > {
11421142 if proto < 3 {
1143- return Self :: reduce ( zelf, vm) ;
1143+ return Self :: __reduce__ ( zelf, vm) ;
11441144 }
11451145 let array = zelf. read ( ) ;
11461146 let cls = zelf. class ( ) . to_owned ( ) ;
@@ -1157,8 +1157,8 @@ mod array {
11571157 ) )
11581158 }
11591159
1160- #[ pymethod( magic ) ]
1161- fn reduce (
1160+ #[ pymethod]
1161+ fn __reduce__ (
11621162 zelf : & Py < Self > ,
11631163 vm : & VirtualMachine ,
11641164 ) -> PyResult < ( PyObjectRef , PyTupleRef , Option < PyDictRef > ) > {
@@ -1179,8 +1179,8 @@ mod array {
11791179 ) )
11801180 }
11811181
1182- #[ pymethod( magic ) ]
1183- fn contains ( & self , value : PyObjectRef , vm : & VirtualMachine ) -> bool {
1182+ #[ pymethod]
1183+ fn __contains__ ( & self , value : PyObjectRef , vm : & VirtualMachine ) -> bool {
11841184 let array = self . array . read ( ) ;
11851185 for element in array
11861186 . iter ( vm)
@@ -1272,7 +1272,7 @@ mod array {
12721272 let class = zelf. class ( ) ;
12731273 let class_name = class. name ( ) ;
12741274 if zelf. read ( ) . typecode ( ) == 'u' {
1275- if zelf. len ( ) == 0 {
1275+ if zelf. __len__ ( ) == 0 {
12761276 return Ok ( format ! ( "{class_name}('u')" ) ) ;
12771277 }
12781278 let to_unicode = zelf. tounicode ( vm) ?;
@@ -1303,16 +1303,18 @@ mod array {
13031303 impl AsMapping for PyArray {
13041304 fn as_mapping ( ) -> & ' static PyMappingMethods {
13051305 static AS_MAPPING : PyMappingMethods = PyMappingMethods {
1306- length : atomic_func ! ( |mapping, _vm| Ok ( PyArray :: mapping_downcast( mapping) . len( ) ) ) ,
1306+ length : atomic_func ! ( |mapping, _vm| Ok (
1307+ PyArray :: mapping_downcast( mapping) . __len__( )
1308+ ) ) ,
13071309 subscript : atomic_func ! ( |mapping, needle, vm| {
1308- PyArray :: mapping_downcast( mapping) . _getitem ( needle, vm)
1310+ PyArray :: mapping_downcast( mapping) . getitem_inner ( needle, vm)
13091311 } ) ,
13101312 ass_subscript : atomic_func ! ( |mapping, needle, value, vm| {
13111313 let zelf = PyArray :: mapping_downcast( mapping) ;
13121314 if let Some ( value) = value {
1313- PyArray :: _setitem ( zelf, needle, value, vm)
1315+ PyArray :: setitem_inner ( zelf, needle, value, vm)
13141316 } else {
1315- zelf. _delitem ( needle, vm)
1317+ zelf. delitem_inner ( needle, vm)
13161318 }
13171319 } ) ,
13181320 } ;
@@ -1323,13 +1325,15 @@ mod array {
13231325 impl AsSequence for PyArray {
13241326 fn as_sequence ( ) -> & ' static PySequenceMethods {
13251327 static AS_SEQUENCE : PySequenceMethods = PySequenceMethods {
1326- length : atomic_func ! ( |seq, _vm| Ok ( PyArray :: sequence_downcast( seq) . len ( ) ) ) ,
1328+ length : atomic_func ! ( |seq, _vm| Ok ( PyArray :: sequence_downcast( seq) . __len__ ( ) ) ) ,
13271329 concat : atomic_func ! ( |seq, other, vm| {
13281330 let zelf = PyArray :: sequence_downcast( seq) ;
1329- PyArray :: add ( zelf, other. to_owned( ) , vm) . map( |x| x. into( ) )
1331+ PyArray :: __add__ ( zelf, other. to_owned( ) , vm) . map( |x| x. into( ) )
13301332 } ) ,
13311333 repeat : atomic_func ! ( |seq, n, vm| {
1332- PyArray :: sequence_downcast( seq) . mul( n, vm) . map( |x| x. into( ) )
1334+ PyArray :: sequence_downcast( seq)
1335+ . __mul__( n, vm)
1336+ . map( |x| x. into( ) )
13331337 } ) ,
13341338 item : atomic_func ! ( |seq, i, vm| {
13351339 PyArray :: sequence_downcast( seq)
@@ -1346,15 +1350,15 @@ mod array {
13461350 } ) ,
13471351 contains : atomic_func ! ( |seq, target, vm| {
13481352 let zelf = PyArray :: sequence_downcast( seq) ;
1349- Ok ( zelf. contains ( target. to_owned( ) , vm) )
1353+ Ok ( zelf. __contains__ ( target. to_owned( ) , vm) )
13501354 } ) ,
13511355 inplace_concat : atomic_func ! ( |seq, other, vm| {
13521356 let zelf = PyArray :: sequence_downcast( seq) . to_owned( ) ;
1353- PyArray :: iadd ( zelf, other. to_owned( ) , vm) . map( |x| x. into( ) )
1357+ PyArray :: __iadd__ ( zelf, other. to_owned( ) , vm) . map( |x| x. into( ) )
13541358 } ) ,
13551359 inplace_repeat : atomic_func ! ( |seq, n, vm| {
13561360 let zelf = PyArray :: sequence_downcast( seq) . to_owned( ) ;
1357- PyArray :: imul ( zelf, n, vm) . map( |x| x. into( ) )
1361+ PyArray :: __imul__ ( zelf, n, vm) . map( |x| x. into( ) )
13581362 } ) ,
13591363 } ;
13601364 & AS_SEQUENCE
@@ -1388,15 +1392,15 @@ mod array {
13881392
13891393 #[ pyclass( with( IterNext , Iterable ) , flags( HAS_DICT ) ) ]
13901394 impl PyArrayIter {
1391- #[ pymethod( magic ) ]
1392- fn setstate ( & self , state : PyObjectRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
1395+ #[ pymethod]
1396+ fn __setstate__ ( & self , state : PyObjectRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
13931397 self . internal
13941398 . lock ( )
1395- . set_state ( state, |obj, pos| pos. min ( obj. len ( ) ) , vm)
1399+ . set_state ( state, |obj, pos| pos. min ( obj. __len__ ( ) ) , vm)
13961400 }
13971401
1398- #[ pymethod( magic ) ]
1399- fn reduce ( & self , vm : & VirtualMachine ) -> PyTupleRef {
1402+ #[ pymethod]
1403+ fn __reduce__ ( & self , vm : & VirtualMachine ) -> PyTupleRef {
14001404 self . internal
14011405 . lock ( )
14021406 . builtins_iter_reduce ( |x| x. clone ( ) . into ( ) , vm)
0 commit comments