@@ -907,7 +907,7 @@ mod array {
907
907
range : OptionalRangeArgs ,
908
908
vm : & VirtualMachine ,
909
909
) -> PyResult < usize > {
910
- let ( start, stop) = range. saturate ( self . len ( ) , vm) ?;
910
+ let ( start, stop) = range. saturate ( self . __len__ ( ) , vm) ?;
911
911
self . read ( ) . index ( x, start, stop, vm)
912
912
}
913
913
@@ -976,29 +976,29 @@ mod array {
976
976
self . write ( ) . reverse ( )
977
977
}
978
978
979
- #[ pymethod( magic ) ]
980
- fn copy ( & self ) -> PyArray {
979
+ #[ pymethod]
980
+ fn __copy__ ( & self ) -> PyArray {
981
981
self . array . read ( ) . clone ( ) . into ( )
982
982
}
983
983
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__ ( )
987
987
}
988
988
989
- fn _getitem ( & self , needle : & PyObject , vm : & VirtualMachine ) -> PyResult {
989
+ fn getitem_inner ( & self , needle : & PyObject , vm : & VirtualMachine ) -> PyResult {
990
990
match SequenceIndex :: try_from_borrowed_object ( vm, needle, "array" ) ? {
991
991
SequenceIndex :: Int ( i) => self . read ( ) . getitem_by_index ( i, vm) ,
992
992
SequenceIndex :: Slice ( slice) => self . read ( ) . getitem_by_slice ( slice, vm) ,
993
993
}
994
994
}
995
995
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)
999
999
}
1000
1000
1001
- fn _setitem (
1001
+ fn setitem_inner (
1002
1002
zelf : & Py < Self > ,
1003
1003
needle : & PyObject ,
1004
1004
value : PyObjectRef ,
@@ -1035,30 +1035,30 @@ mod array {
1035
1035
}
1036
1036
}
1037
1037
1038
- #[ pymethod( magic ) ]
1039
- fn setitem (
1038
+ #[ pymethod]
1039
+ fn __setitem__ (
1040
1040
zelf : & Py < Self > ,
1041
1041
needle : PyObjectRef ,
1042
1042
value : PyObjectRef ,
1043
1043
vm : & VirtualMachine ,
1044
1044
) -> PyResult < ( ) > {
1045
- Self :: _setitem ( zelf, & needle, value, vm)
1045
+ Self :: setitem_inner ( zelf, & needle, value, vm)
1046
1046
}
1047
1047
1048
- fn _delitem ( & self , needle : & PyObject , vm : & VirtualMachine ) -> PyResult < ( ) > {
1048
+ fn delitem_inner ( & self , needle : & PyObject , vm : & VirtualMachine ) -> PyResult < ( ) > {
1049
1049
match SequenceIndex :: try_from_borrowed_object ( vm, needle, "array" ) ? {
1050
1050
SequenceIndex :: Int ( i) => self . try_resizable ( vm) ?. delitem_by_index ( i, vm) ,
1051
1051
SequenceIndex :: Slice ( slice) => self . try_resizable ( vm) ?. delitem_by_slice ( slice, vm) ,
1052
1052
}
1053
1053
}
1054
1054
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)
1058
1058
}
1059
1059
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 > > {
1062
1062
if let Some ( other) = other. payload :: < PyArray > ( ) {
1063
1063
self . read ( )
1064
1064
. add ( & other. read ( ) , vm)
@@ -1071,8 +1071,8 @@ mod array {
1071
1071
}
1072
1072
}
1073
1073
1074
- #[ pymethod( magic ) ]
1075
- fn iadd (
1074
+ #[ pymethod]
1075
+ fn __iadd__ (
1076
1076
zelf : PyRef < Self > ,
1077
1077
other : PyObjectRef ,
1078
1078
vm : & VirtualMachine ,
@@ -1091,28 +1091,28 @@ mod array {
1091
1091
}
1092
1092
1093
1093
#[ 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 > > {
1096
1096
self . read ( )
1097
1097
. mul ( value, vm)
1098
1098
. map ( |x| Self :: from ( x) . into_ref ( & vm. ctx ) )
1099
1099
}
1100
1100
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 > > {
1103
1103
zelf. try_resizable ( vm) ?. imul ( value, vm) ?;
1104
1104
Ok ( zelf)
1105
1105
}
1106
1106
1107
- #[ pymethod( magic ) ]
1108
- pub ( crate ) fn len ( & self ) -> usize {
1107
+ #[ pymethod]
1108
+ pub ( crate ) fn __len__ ( & self ) -> usize {
1109
1109
self . read ( ) . len ( )
1110
1110
}
1111
1111
1112
1112
fn array_eq ( & self , other : & Self , vm : & VirtualMachine ) -> PyResult < bool > {
1113
1113
// we cannot use zelf.is(other) for shortcut because if we contenting a
1114
1114
// 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__ ( ) {
1116
1116
return Ok ( false ) ;
1117
1117
}
1118
1118
let array_a = self . read ( ) ;
@@ -1133,14 +1133,14 @@ mod array {
1133
1133
Ok ( true )
1134
1134
}
1135
1135
1136
- #[ pymethod( magic ) ]
1137
- fn reduce_ex (
1136
+ #[ pymethod]
1137
+ fn __reduce_ex__ (
1138
1138
zelf : & Py < Self > ,
1139
1139
proto : usize ,
1140
1140
vm : & VirtualMachine ,
1141
1141
) -> PyResult < ( PyObjectRef , PyTupleRef , Option < PyDictRef > ) > {
1142
1142
if proto < 3 {
1143
- return Self :: reduce ( zelf, vm) ;
1143
+ return Self :: __reduce__ ( zelf, vm) ;
1144
1144
}
1145
1145
let array = zelf. read ( ) ;
1146
1146
let cls = zelf. class ( ) . to_owned ( ) ;
@@ -1157,8 +1157,8 @@ mod array {
1157
1157
) )
1158
1158
}
1159
1159
1160
- #[ pymethod( magic ) ]
1161
- fn reduce (
1160
+ #[ pymethod]
1161
+ fn __reduce__ (
1162
1162
zelf : & Py < Self > ,
1163
1163
vm : & VirtualMachine ,
1164
1164
) -> PyResult < ( PyObjectRef , PyTupleRef , Option < PyDictRef > ) > {
@@ -1179,8 +1179,8 @@ mod array {
1179
1179
) )
1180
1180
}
1181
1181
1182
- #[ pymethod( magic ) ]
1183
- fn contains ( & self , value : PyObjectRef , vm : & VirtualMachine ) -> bool {
1182
+ #[ pymethod]
1183
+ fn __contains__ ( & self , value : PyObjectRef , vm : & VirtualMachine ) -> bool {
1184
1184
let array = self . array . read ( ) ;
1185
1185
for element in array
1186
1186
. iter ( vm)
@@ -1272,7 +1272,7 @@ mod array {
1272
1272
let class = zelf. class ( ) ;
1273
1273
let class_name = class. name ( ) ;
1274
1274
if zelf. read ( ) . typecode ( ) == 'u' {
1275
- if zelf. len ( ) == 0 {
1275
+ if zelf. __len__ ( ) == 0 {
1276
1276
return Ok ( format ! ( "{class_name}('u')" ) ) ;
1277
1277
}
1278
1278
let to_unicode = zelf. tounicode ( vm) ?;
@@ -1303,16 +1303,18 @@ mod array {
1303
1303
impl AsMapping for PyArray {
1304
1304
fn as_mapping ( ) -> & ' static PyMappingMethods {
1305
1305
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
+ ) ) ,
1307
1309
subscript : atomic_func ! ( |mapping, needle, vm| {
1308
- PyArray :: mapping_downcast( mapping) . _getitem ( needle, vm)
1310
+ PyArray :: mapping_downcast( mapping) . getitem_inner ( needle, vm)
1309
1311
} ) ,
1310
1312
ass_subscript : atomic_func ! ( |mapping, needle, value, vm| {
1311
1313
let zelf = PyArray :: mapping_downcast( mapping) ;
1312
1314
if let Some ( value) = value {
1313
- PyArray :: _setitem ( zelf, needle, value, vm)
1315
+ PyArray :: setitem_inner ( zelf, needle, value, vm)
1314
1316
} else {
1315
- zelf. _delitem ( needle, vm)
1317
+ zelf. delitem_inner ( needle, vm)
1316
1318
}
1317
1319
} ) ,
1318
1320
} ;
@@ -1323,13 +1325,15 @@ mod array {
1323
1325
impl AsSequence for PyArray {
1324
1326
fn as_sequence ( ) -> & ' static PySequenceMethods {
1325
1327
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__ ( ) ) ) ,
1327
1329
concat : atomic_func ! ( |seq, other, vm| {
1328
1330
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( ) )
1330
1332
} ) ,
1331
1333
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( ) )
1333
1337
} ) ,
1334
1338
item : atomic_func ! ( |seq, i, vm| {
1335
1339
PyArray :: sequence_downcast( seq)
@@ -1346,15 +1350,15 @@ mod array {
1346
1350
} ) ,
1347
1351
contains : atomic_func ! ( |seq, target, vm| {
1348
1352
let zelf = PyArray :: sequence_downcast( seq) ;
1349
- Ok ( zelf. contains ( target. to_owned( ) , vm) )
1353
+ Ok ( zelf. __contains__ ( target. to_owned( ) , vm) )
1350
1354
} ) ,
1351
1355
inplace_concat : atomic_func ! ( |seq, other, vm| {
1352
1356
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( ) )
1354
1358
} ) ,
1355
1359
inplace_repeat : atomic_func ! ( |seq, n, vm| {
1356
1360
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( ) )
1358
1362
} ) ,
1359
1363
} ;
1360
1364
& AS_SEQUENCE
@@ -1388,15 +1392,15 @@ mod array {
1388
1392
1389
1393
#[ pyclass( with( IterNext , Iterable ) , flags( HAS_DICT ) ) ]
1390
1394
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 < ( ) > {
1393
1397
self . internal
1394
1398
. lock ( )
1395
- . set_state ( state, |obj, pos| pos. min ( obj. len ( ) ) , vm)
1399
+ . set_state ( state, |obj, pos| pos. min ( obj. __len__ ( ) ) , vm)
1396
1400
}
1397
1401
1398
- #[ pymethod( magic ) ]
1399
- fn reduce ( & self , vm : & VirtualMachine ) -> PyTupleRef {
1402
+ #[ pymethod]
1403
+ fn __reduce__ ( & self , vm : & VirtualMachine ) -> PyTupleRef {
1400
1404
self . internal
1401
1405
. lock ( )
1402
1406
. builtins_iter_reduce ( |x| x. clone ( ) . into ( ) , vm)
0 commit comments