Skip to content

Commit e698316

Browse files
committed
Remove arithmatic pymethods
1 parent f8199d7 commit e698316

30 files changed

+217
-276
lines changed

crates/stdlib/src/array.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,8 +1090,6 @@ mod array {
10901090
Ok(zelf)
10911091
}
10921092

1093-
#[pymethod(name = "__rmul__")]
1094-
#[pymethod]
10951093
fn __mul__(&self, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
10961094
self.read()
10971095
.mul(value, vm)

crates/stdlib/src/cmath.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ mod cmath {
7070

7171
#[pyfunction]
7272
fn asin(z: ArgIntoComplex) -> Complex64 {
73-
z.asin()
73+
let result = z.asin();
74+
if z.im == 0.0 && result.im.abs() < 1e-15 {
75+
Complex64::new(result.re, 0.0)
76+
} else {
77+
result
78+
}
7479
}
7580

7681
#[pyfunction]
@@ -80,7 +85,13 @@ mod cmath {
8085

8186
#[pyfunction]
8287
fn acos(z: ArgIntoComplex) -> Complex64 {
83-
z.acos()
88+
let result = z.acos();
89+
// For real inputs, ensure imaginary part is exactly 0 when it's negligibly small
90+
if z.im == 0.0 && result.im.abs() < 1e-15 {
91+
Complex64::new(result.re, 0.0)
92+
} else {
93+
result
94+
}
8495
}
8596

8697
#[pyfunction]

crates/vm/src/builtins/bytearray.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl PyByteArray {
206206
self.inner().capacity()
207207
}
208208

209-
#[pymethod]
209+
// __len__ exposed via AsSequence as wrapper_descriptor
210210
fn __len__(&self) -> usize {
211211
self.borrow_buf().len()
212212
}
@@ -216,12 +216,12 @@ impl PyByteArray {
216216
size_of::<Self>() + self.borrow_buf().len() * size_of::<u8>()
217217
}
218218

219-
#[pymethod]
219+
// __add__ exposed via AsSequence as wrapper_descriptor
220220
fn __add__(&self, other: ArgBytesLike) -> Self {
221221
self.inner().add(&other.borrow_buf()).into()
222222
}
223223

224-
#[pymethod]
224+
// __contains__ exposed via AsSequence as wrapper_descriptor
225225
fn __contains__(
226226
&self,
227227
needle: Either<PyBytesInner, PyIntRef>,
@@ -230,7 +230,7 @@ impl PyByteArray {
230230
self.inner().contains(needle, vm)
231231
}
232232

233-
#[pymethod]
233+
// __iadd__ exposed via AsSequence as wrapper_descriptor
234234
fn __iadd__(
235235
zelf: PyRef<Self>,
236236
other: ArgBytesLike,
@@ -242,12 +242,12 @@ impl PyByteArray {
242242
Ok(zelf)
243243
}
244244

245-
#[pymethod]
245+
// __getitem__ exposed via AsMapping as wrapper_descriptor
246246
fn __getitem__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
247247
self._getitem(&needle, vm)
248248
}
249249

250-
#[pymethod]
250+
// __delitem__ exposed via AsMapping as wrapper_descriptor
251251
pub fn __delitem__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
252252
self._delitem(&needle, vm)
253253
}
@@ -525,25 +525,24 @@ impl PyByteArray {
525525
self.inner().title().into()
526526
}
527527

528-
#[pymethod(name = "__rmul__")]
529-
#[pymethod]
528+
// __mul__ and __rmul__ exposed via AsSequence as wrapper_descriptor
530529
fn __mul__(&self, value: ArgSize, vm: &VirtualMachine) -> PyResult<Self> {
531530
self.repeat(value.into(), vm)
532531
}
533532

534-
#[pymethod]
533+
// __imul__ exposed via AsSequence as wrapper_descriptor
535534
fn __imul__(zelf: PyRef<Self>, value: ArgSize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
536535
Self::irepeat(&zelf, value.into(), vm)?;
537536
Ok(zelf)
538537
}
539538

540-
#[pymethod(name = "__mod__")]
539+
// __mod__ exposed via AsNumber as wrapper_descriptor
541540
fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
542541
let formatted = self.inner().cformat(values, vm)?;
543542
Ok(formatted.into())
544543
}
545544

546-
#[pymethod]
545+
// __rmod__ exposed via AsNumber as wrapper_descriptor
547546
fn __rmod__(&self, _values: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
548547
vm.ctx.not_implemented()
549548
}
@@ -562,7 +561,7 @@ impl PyByteArray {
562561

563562
#[pyclass]
564563
impl Py<PyByteArray> {
565-
#[pymethod]
564+
// __setitem__ exposed via AsMapping as wrapper_descriptor
566565
fn __setitem__(
567566
&self,
568567
needle: PyObjectRef,

crates/vm/src/builtins/bytes.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ impl PyRef<PyBytes> {
204204
)
205205
)]
206206
impl PyBytes {
207+
// Exposed via AsSequence.length slot
207208
#[inline]
208-
#[pymethod]
209209
pub const fn __len__(&self) -> usize {
210210
self.inner.len()
211211
}
@@ -225,12 +225,12 @@ impl PyBytes {
225225
size_of::<Self>() + self.len() * size_of::<u8>()
226226
}
227227

228-
#[pymethod]
228+
// Exposed via AsSequence.concat slot
229229
fn __add__(&self, other: ArgBytesLike) -> Vec<u8> {
230230
self.inner.add(&other.borrow_buf())
231231
}
232232

233-
#[pymethod]
233+
// Exposed via AsSequence.contains slot
234234
fn __contains__(
235235
&self,
236236
needle: Either<PyBytesInner, PyIntRef>,
@@ -244,7 +244,7 @@ impl PyBytes {
244244
PyBytesInner::maketrans(from, to, vm)
245245
}
246246

247-
#[pymethod]
247+
// Exposed via AsMapping.subscript slot
248248
fn __getitem__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
249249
self._getitem(&needle, vm)
250250
}
@@ -513,23 +513,17 @@ impl PyBytes {
513513
self.inner.title().into()
514514
}
515515

516-
#[pymethod(name = "__rmul__")]
517-
#[pymethod]
516+
// Exposed via AsSequence.repeat slot
518517
fn __mul__(zelf: PyRef<Self>, value: ArgIndex, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
519518
zelf.repeat(value.try_to_primitive(vm)?, vm)
520519
}
521520

522-
#[pymethod(name = "__mod__")]
523-
fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
521+
// Exposed via AsNumber.remainder slot
522+
pub(crate) fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
524523
let formatted = self.inner.cformat(values, vm)?;
525524
Ok(formatted.into())
526525
}
527526

528-
#[pymethod]
529-
fn __rmod__(&self, _values: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
530-
vm.ctx.not_implemented()
531-
}
532-
533527
#[pymethod]
534528
fn __getnewargs__(&self, vm: &VirtualMachine) -> PyTupleRef {
535529
let param: Vec<PyObjectRef> = self.elements().map(|x| x.to_pyobject(vm)).collect();

crates/vm/src/builtins/dict.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl PyDict {
212212
}
213213
}
214214

215-
#[pymethod]
215+
// __len__ exposed via AsMapping as wrapper_descriptor
216216
pub fn __len__(&self) -> usize {
217217
self.entries.len()
218218
}
@@ -222,12 +222,12 @@ impl PyDict {
222222
std::mem::size_of::<Self>() + self.entries.sizeof()
223223
}
224224

225-
#[pymethod]
225+
// __contains__ exposed via AsSequence as wrapper_descriptor
226226
fn __contains__(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
227227
self.entries.contains(vm, &*key)
228228
}
229229

230-
#[pymethod]
230+
// __delitem__ exposed via AsMapping as wrapper_descriptor
231231
fn __delitem__(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
232232
self.inner_delitem(&*key, vm)
233233
}
@@ -237,7 +237,7 @@ impl PyDict {
237237
self.entries.clear()
238238
}
239239

240-
#[pymethod]
240+
// __setitem__ exposed via AsMapping as wrapper_descriptor
241241
fn __setitem__(
242242
&self,
243243
key: PyObjectRef,
@@ -294,7 +294,7 @@ impl PyDict {
294294
Ok(())
295295
}
296296

297-
#[pymethod]
297+
// __or__ exposed via AsNumber as wrapper_descriptor
298298
fn __or__(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
299299
let other_dict: Result<PyDictRef, _> = other.downcast();
300300
if let Ok(other) = other_dict {
@@ -375,7 +375,7 @@ impl Py<PyDict> {
375375
Ok(Implemented(true))
376376
}
377377

378-
#[pymethod]
378+
// __getitem__ exposed via AsMapping as wrapper_descriptor
379379
#[cfg_attr(feature = "flame-it", flame("PyDictRef"))]
380380
fn __getitem__(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult {
381381
self.inner_getitem(&*key, vm)
@@ -404,13 +404,13 @@ impl PyRef<PyDict> {
404404
PyDictReverseKeyIterator::new(self)
405405
}
406406

407-
#[pymethod]
407+
// __ior__ exposed via AsNumber as wrapper_descriptor
408408
fn __ior__(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
409409
self.merge_object(other, vm)?;
410410
Ok(self)
411411
}
412412

413-
#[pymethod]
413+
// __ror__ exposed via AsNumber as wrapper_descriptor
414414
fn __ror__(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
415415
let other_dict: Result<Self, _> = other.downcast();
416416
if let Ok(other) = other_dict {
@@ -1143,7 +1143,7 @@ impl ViewSetOps for PyDictKeys {}
11431143
)
11441144
)]
11451145
impl PyDictKeys {
1146-
#[pymethod]
1146+
// __contains__ exposed via AsSequence as wrapper_descriptor
11471147
fn __contains__(zelf: PyObjectRef, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
11481148
zelf.sequence_unchecked().contains(&key, vm)
11491149
}
@@ -1208,7 +1208,7 @@ impl ViewSetOps for PyDictItems {}
12081208
)
12091209
)]
12101210
impl PyDictItems {
1211-
#[pymethod]
1211+
// __contains__ exposed via AsSequence as wrapper_descriptor
12121212
fn __contains__(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
12131213
zelf.sequence_unchecked().contains(&needle, vm)
12141214
}

crates/vm/src/builtins/genericalias.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl PyGenericAlias {
189189
}
190190
}
191191

192-
#[pymethod]
192+
// Exposed via AsMapping.subscript slot
193193
fn __getitem__(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
194194
let new_args = subs_parameters(
195195
zelf.to_owned().into(),
@@ -236,12 +236,12 @@ impl PyGenericAlias {
236236
Err(vm.new_type_error("issubclass() argument 2 cannot be a parameterized generic"))
237237
}
238238

239-
#[pymethod]
239+
// Exposed via AsNumber.or slot (right variant)
240240
fn __ror__(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
241241
type_::or_(other, zelf, vm)
242242
}
243243

244-
#[pymethod]
244+
// Exposed via AsNumber.or slot
245245
fn __or__(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
246246
type_::or_(zelf, other, vm)
247247
}

crates/vm/src/builtins/getset.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl PyGetSet {
118118
)))
119119
}
120120
}
121-
#[pymethod]
121+
// Exposed via slots.descr_set slot
122122
fn __set__(
123123
zelf: PyObjectRef,
124124
obj: PyObjectRef,
@@ -127,7 +127,7 @@ impl PyGetSet {
127127
) -> PyResult<()> {
128128
Self::descr_set(&zelf, obj, PySetterValue::Assign(value), vm)
129129
}
130-
#[pymethod]
130+
// Exposed via slots.descr_set slot (delete variant)
131131
fn __delete__(zelf: PyObjectRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
132132
Self::descr_set(&zelf, obj, PySetterValue::Delete, vm)
133133
}

crates/vm/src/builtins/list.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl PyList {
144144
Ok(Self::from(elements).into_ref(&vm.ctx))
145145
}
146146

147-
#[pymethod]
147+
// __add__ exposed via AsSequence as wrapper_descriptor
148148
fn __add__(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
149149
self.concat(&other, vm)
150150
}
@@ -159,7 +159,7 @@ impl PyList {
159159
Ok(zelf.to_owned().into())
160160
}
161161

162-
#[pymethod]
162+
// __iadd__ exposed via AsSequence as wrapper_descriptor
163163
fn __iadd__(
164164
zelf: PyRef<Self>,
165165
other: PyObjectRef,
@@ -180,8 +180,8 @@ impl PyList {
180180
Self::from(self.borrow_vec().to_vec()).into_ref(&vm.ctx)
181181
}
182182

183+
// __len__ exposed via AsSequence as wrapper_descriptor
183184
#[allow(clippy::len_without_is_empty)]
184-
#[pymethod]
185185
pub fn __len__(&self) -> usize {
186186
self.borrow_vec().len()
187187
}
@@ -215,7 +215,7 @@ impl PyList {
215215
}
216216
}
217217

218-
#[pymethod]
218+
// __getitem__ exposed via AsMapping as wrapper_descriptor
219219
fn __getitem__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
220220
self._getitem(&needle, vm)
221221
}
@@ -230,7 +230,7 @@ impl PyList {
230230
}
231231
}
232232

233-
#[pymethod]
233+
// __setitem__ exposed via AsMapping as wrapper_descriptor
234234
fn __setitem__(
235235
&self,
236236
needle: PyObjectRef,
@@ -240,13 +240,12 @@ impl PyList {
240240
self._setitem(&needle, value, vm)
241241
}
242242

243-
#[pymethod]
244-
#[pymethod(name = "__rmul__")]
243+
// __mul__ and __rmul__ exposed via AsSequence as wrapper_descriptor
245244
fn __mul__(&self, n: ArgSize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
246245
self.repeat(n.into(), vm)
247246
}
248247

249-
#[pymethod]
248+
// __imul__ exposed via AsSequence as wrapper_descriptor
250249
fn __imul__(zelf: PyRef<Self>, n: ArgSize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
251250
Self::irepeat(zelf, n.into(), vm)
252251
}
@@ -256,7 +255,7 @@ impl PyList {
256255
self.mut_count(vm, &needle)
257256
}
258257

259-
#[pymethod]
258+
// __contains__ exposed via AsSequence as wrapper_descriptor
260259
pub(crate) fn __contains__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
261260
self.mut_contains(vm, &needle)
262261
}
@@ -314,7 +313,7 @@ impl PyList {
314313
}
315314
}
316315

317-
#[pymethod]
316+
// __delitem__ exposed via AsMapping as wrapper_descriptor
318317
fn __delitem__(&self, subscript: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
319318
self._delitem(&subscript, vm)
320319
}

0 commit comments

Comments
 (0)