Skip to content

Commit de5ddcd

Browse files
committed
Move PyInt general_op() and modpow() to Py<PyInt>
The power slot calls them on Py<PyInt> without going through the payload. Assisted-by: Claude Code:claude-opus-5-5
1 parent b258ae2 commit de5ddcd

1 file changed

Lines changed: 28 additions & 28 deletions

File tree

‎crates/vm/src/builtins/int.rs‎

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,6 @@ impl PyInt {
303303
pub const fn as_bigint(&self) -> &BigInt {
304304
&self.value
305305
}
306-
307-
#[inline]
308-
fn general_op<F>(&self, other: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
309-
where
310-
F: Fn(&BigInt, &BigInt) -> PyResult,
311-
{
312-
if let Some(other) = other.downcast_ref::<Self>() {
313-
op(&self.value, other.as_bigint())
314-
} else {
315-
Ok(vm.ctx.not_implemented())
316-
}
317-
}
318306
}
319307

320308
impl Py<PyInt> {
@@ -431,25 +419,24 @@ impl Py<PyInt> {
431419
.map(|other| op(self.as_bigint(), other.as_bigint()));
432420
PyArithmeticValue::from_option(r)
433421
}
434-
}
435422

436-
#[derive(FromArgs)]
437-
struct RoundArgs {
438-
#[pyarg(positional, optional)]
439-
ndigits: Option<PyIntRef>,
440-
}
423+
#[inline]
424+
fn general_op<F>(&self, other: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
425+
where
426+
F: Fn(&BigInt, &BigInt) -> PyResult,
427+
{
428+
if let Some(other) = other.downcast_ref::<PyInt>() {
429+
op(self.as_bigint(), other.as_bigint())
430+
} else {
431+
Ok(vm.ctx.not_implemented())
432+
}
433+
}
441434

442-
#[pyclass(
443-
itemsize = 4,
444-
flags(BASETYPE, _MATCH_SELF),
445-
with(PyRef, Comparable, Hashable, Constructor, AsNumber, Representable)
446-
)]
447-
impl PyInt {
448435
fn modpow(&self, other: &PyObject, modulus: &PyObject, vm: &VirtualMachine) -> PyResult {
449-
if other.downcast_ref::<Self>().is_none() {
436+
if other.downcast_ref::<PyInt>().is_none() {
450437
return Ok(vm.ctx.not_implemented());
451438
}
452-
let modulus = match modulus.downcast_ref::<Self>() {
439+
let modulus = match modulus.downcast_ref::<PyInt>() {
453440
Some(val) => val.as_bigint(),
454441
None => return Ok(vm.ctx.not_implemented()),
455442
};
@@ -488,7 +475,20 @@ impl PyInt {
488475
vm,
489476
)
490477
}
478+
}
479+
480+
#[derive(FromArgs)]
481+
struct RoundArgs {
482+
#[pyarg(positional, optional)]
483+
ndigits: Option<PyIntRef>,
484+
}
491485

486+
#[pyclass(
487+
itemsize = 4,
488+
flags(BASETYPE, _MATCH_SELF),
489+
with(PyRef, Comparable, Hashable, Constructor, AsNumber, Representable)
490+
)]
491+
impl PyInt {
492492
#[pymethod]
493493
fn __round__(zelf: PyRef<Self>, args: RoundArgs, vm: &VirtualMachine) -> PyRef<Self> {
494494
if let Some(ndigits) = args.ndigits {
@@ -778,9 +778,9 @@ impl PyInt {
778778
power: Some(|a, b, c, vm| {
779779
if let Some(a) = a.downcast_ref::<Self>() {
780780
if vm.is_none(c) {
781-
a.payload.general_op(b, |a, b| inner_pow(a, b, vm), vm)
781+
a.general_op(b, |a, b| inner_pow(a, b, vm), vm)
782782
} else {
783-
a.payload.modpow(b, c, vm)
783+
a.modpow(b, c, vm)
784784
}
785785
} else {
786786
Ok(vm.ctx.not_implemented())

0 commit comments

Comments
 (0)