Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
vm
  • Loading branch information
ShaharNaveh committed Jul 3, 2025
commit b8957d236c54a8941a5a0455840b8ac9a5acc183
22 changes: 11 additions & 11 deletions vm/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ pub(crate) enum Endianness {
impl Endianness {
/// Parse endianness
/// See also: https://docs.python.org/3/library/struct.html?highlight=struct#byte-order-size-and-alignment
fn parse<I>(chars: &mut Peekable<I>) -> Endianness
fn parse<I>(chars: &mut Peekable<I>) -> Self
where
I: Sized + Iterator<Item = u8>,
{
let e = match chars.peek() {
Some(b'@') => Endianness::Native,
Some(b'=') => Endianness::Host,
Some(b'<') => Endianness::Little,
Some(b'>') | Some(b'!') => Endianness::Big,
_ => return Endianness::Native,
Some(b'@') => Self::Native,
Some(b'=') => Self::Host,
Some(b'<') => Self::Little,
Some(b'>') | Some(b'!') => Self::Big,
_ => return Self::Native,
};
chars.next().unwrap();
e
Expand Down Expand Up @@ -267,7 +267,7 @@ impl FormatCode {
.and_then(|extra| offset.checked_add(extra))
.ok_or_else(|| OVERFLOW_MSG.to_owned())?;

let code = FormatCode {
let code = Self {
repeat: repeat as usize,
code,
info,
Expand Down Expand Up @@ -321,7 +321,7 @@ pub struct FormatSpec {
}

impl FormatSpec {
pub fn parse(fmt: &[u8], vm: &VirtualMachine) -> PyResult<FormatSpec> {
pub fn parse(fmt: &[u8], vm: &VirtualMachine) -> PyResult<Self> {
let mut chars = fmt.iter().copied().peekable();

// First determine "@", "<", ">","!" or "="
Expand All @@ -331,7 +331,7 @@ impl FormatSpec {
let (codes, size, arg_count) =
FormatCode::parse(&mut chars, endianness).map_err(|err| new_struct_error(vm, err))?;

Ok(FormatSpec {
Ok(Self {
endianness,
codes,
size,
Expand Down Expand Up @@ -544,7 +544,7 @@ impl Packable for f16 {
fn pack<E: ByteOrder>(vm: &VirtualMachine, arg: PyObjectRef, data: &mut [u8]) -> PyResult<()> {
let f_64 = *ArgIntoFloat::try_from_object(vm, arg)?;
// "from_f64 should be preferred in any non-`const` context" except it gives the wrong result :/
let f_16 = f16::from_f64_const(f_64);
let f_16 = Self::from_f64_const(f_64);
if f_16.is_infinite() != f_64.is_infinite() {
return Err(vm.new_overflow_error("float too large to pack with e format"));
}
Expand All @@ -554,7 +554,7 @@ impl Packable for f16 {

fn unpack<E: ByteOrder>(vm: &VirtualMachine, rdr: &[u8]) -> PyObjectRef {
let i = PackInt::unpack_int::<E>(rdr);
f16::from_bits(i).to_f64().to_pyobject(vm)
Self::from_bits(i).to_f64().to_pyobject(vm)
}
}

Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/asyncgenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl PyAsyncGen {
}

pub fn new(frame: FrameRef, name: PyStrRef) -> Self {
PyAsyncGen {
Self {
inner: Coro::new(frame, name),
running_async: AtomicCell::new(false),
}
Expand Down Expand Up @@ -76,7 +76,7 @@ impl PyAsyncGen {
#[pyclass]
impl PyRef<PyAsyncGen> {
#[pymethod]
fn __aiter__(self, _vm: &VirtualMachine) -> PyRef<PyAsyncGen> {
fn __aiter__(self, _vm: &VirtualMachine) -> Self {
self
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl ToPyObject for bool {
}

impl<'a> TryFromBorrowedObject<'a> for bool {
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult<bool> {
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult<Self> {
if obj.fast_isinstance(vm.ctx.types.int_type) {
Ok(get_value(obj))
} else {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/builtin_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl TryFromObject for NativeFunctionOrMethod {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let class = vm.ctx.types.builtin_function_or_method_type;
if obj.fast_isinstance(class) {
Ok(NativeFunctionOrMethod(unsafe { obj.downcast_unchecked() }))
Ok(Self(unsafe { obj.downcast_unchecked() }))
} else {
Err(vm.new_downcast_type_error(class, &obj))
}
Expand Down
20 changes: 10 additions & 10 deletions vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl PyByteArray {
}

fn from_inner(inner: PyBytesInner) -> Self {
PyByteArray {
Self {
inner: PyRwLock::new(inner),
exports: AtomicUsize::new(0),
}
Expand Down Expand Up @@ -339,7 +339,7 @@ impl PyByteArray {
&self,
options: ByteInnerPaddingOptions,
vm: &VirtualMachine,
) -> PyResult<PyByteArray> {
) -> PyResult<Self> {
Ok(self.inner().center(options, vm)?.into())
}

Expand All @@ -348,7 +348,7 @@ impl PyByteArray {
&self,
options: ByteInnerPaddingOptions,
vm: &VirtualMachine,
) -> PyResult<PyByteArray> {
) -> PyResult<Self> {
Ok(self.inner().ljust(options, vm)?.into())
}

Expand All @@ -357,7 +357,7 @@ impl PyByteArray {
&self,
options: ByteInnerPaddingOptions,
vm: &VirtualMachine,
) -> PyResult<PyByteArray> {
) -> PyResult<Self> {
Ok(self.inner().rjust(options, vm)?.into())
}

Expand All @@ -367,7 +367,7 @@ impl PyByteArray {
}

#[pymethod]
fn join(&self, iter: ArgIterable<PyBytesInner>, vm: &VirtualMachine) -> PyResult<PyByteArray> {
fn join(&self, iter: ArgIterable<PyBytesInner>, vm: &VirtualMachine) -> PyResult<Self> {
Ok(self.inner().join(iter, vm)?.into())
}

Expand Down Expand Up @@ -438,7 +438,7 @@ impl PyByteArray {
&self,
options: ByteInnerTranslateOptions,
vm: &VirtualMachine,
) -> PyResult<PyByteArray> {
) -> PyResult<Self> {
Ok(self.inner().translate(options, vm)?.into())
}

Expand Down Expand Up @@ -526,7 +526,7 @@ impl PyByteArray {
new: PyBytesInner,
count: OptionalArg<isize>,
vm: &VirtualMachine,
) -> PyResult<PyByteArray> {
) -> PyResult<Self> {
Ok(self.inner().replace(old, new, count, vm)?.into())
}

Expand All @@ -553,7 +553,7 @@ impl PyByteArray {
}

#[pymethod(name = "__mod__")]
fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyByteArray> {
fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
let formatted = self.inner().cformat(values, vm)?;
Ok(formatted.into())
}
Expand Down Expand Up @@ -667,7 +667,7 @@ impl PyRef<PyByteArray> {
self,
chars: OptionalOption<PyBytesInner>,
vm: &VirtualMachine,
) -> PyRef<PyByteArray> {
) -> Self {
let inner = self.inner();
let stripped = inner.lstrip(chars);
let elements = &inner.elements;
Expand All @@ -684,7 +684,7 @@ impl PyRef<PyByteArray> {
self,
chars: OptionalOption<PyBytesInner>,
vm: &VirtualMachine,
) -> PyRef<PyByteArray> {
) -> Self {
let inner = self.inner();
let stripped = inner.rstrip(chars);
let elements = &inner.elements;
Expand Down
22 changes: 11 additions & 11 deletions vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl PyBytes {
}

impl PyRef<PyBytes> {
fn repeat(self, count: isize, vm: &VirtualMachine) -> PyResult<PyRef<PyBytes>> {
fn repeat(self, count: isize, vm: &VirtualMachine) -> PyResult<Self> {
if count == 1 && self.class().is(vm.ctx.types.bytes_type) {
// Special case: when some `bytes` is multiplied by `1`,
// nothing really happens, we need to return an object itself
Expand Down Expand Up @@ -270,17 +270,17 @@ impl PyBytes {
}

#[pymethod]
fn center(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult<PyBytes> {
fn center(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult<Self> {
Ok(self.inner.center(options, vm)?.into())
}

#[pymethod]
fn ljust(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult<PyBytes> {
fn ljust(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult<Self> {
Ok(self.inner.ljust(options, vm)?.into())
}

#[pymethod]
fn rjust(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult<PyBytes> {
fn rjust(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult<Self> {
Ok(self.inner.rjust(options, vm)?.into())
}

Expand All @@ -290,7 +290,7 @@ impl PyBytes {
}

#[pymethod]
fn join(&self, iter: ArgIterable<PyBytesInner>, vm: &VirtualMachine) -> PyResult<PyBytes> {
fn join(&self, iter: ArgIterable<PyBytesInner>, vm: &VirtualMachine) -> PyResult<Self> {
Ok(self.inner.join(iter, vm)?.into())
}

Expand Down Expand Up @@ -359,7 +359,7 @@ impl PyBytes {
&self,
options: ByteInnerTranslateOptions,
vm: &VirtualMachine,
) -> PyResult<PyBytes> {
) -> PyResult<Self> {
Ok(self.inner.translate(options, vm)?.into())
}

Expand Down Expand Up @@ -451,7 +451,7 @@ impl PyBytes {
new: PyBytesInner,
count: OptionalArg<isize>,
vm: &VirtualMachine,
) -> PyResult<PyBytes> {
) -> PyResult<Self> {
Ok(self.inner.replace(old, new, count, vm)?.into())
}

Expand All @@ -467,7 +467,7 @@ impl PyBytes {
}

#[pymethod(name = "__mod__")]
fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyBytes> {
fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
let formatted = self.inner.cformat(values, vm)?;
Ok(formatted.into())
}
Expand Down Expand Up @@ -515,7 +515,7 @@ impl Py<PyBytes> {
#[pyclass]
impl PyRef<PyBytes> {
#[pymethod]
fn __bytes__(self, vm: &VirtualMachine) -> PyRef<PyBytes> {
fn __bytes__(self, vm: &VirtualMachine) -> Self {
if self.is(vm.ctx.types.bytes_type) {
self
} else {
Expand All @@ -524,7 +524,7 @@ impl PyRef<PyBytes> {
}

#[pymethod]
fn lstrip(self, chars: OptionalOption<PyBytesInner>, vm: &VirtualMachine) -> PyRef<PyBytes> {
fn lstrip(self, chars: OptionalOption<PyBytesInner>, vm: &VirtualMachine) -> Self {
let stripped = self.inner.lstrip(chars);
if stripped == self.as_bytes() {
self
Expand All @@ -534,7 +534,7 @@ impl PyRef<PyBytes> {
}

#[pymethod]
fn rstrip(self, chars: OptionalOption<PyBytesInner>, vm: &VirtualMachine) -> PyRef<PyBytes> {
fn rstrip(self, chars: OptionalOption<PyBytesInner>, vm: &VirtualMachine) -> Self {
let stripped = self.inner.rstrip(chars);
if stripped == self.as_bytes() {
self
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/classmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Constructor for PyClassMethod {
}

// Create PyClassMethod instance with the pre-populated dict
let classmethod = PyClassMethod {
let classmethod = Self {
callable: PyMutex::new(callable),
};

Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ impl Deref for PyCode {
}

impl PyCode {
pub fn new(code: CodeObject) -> PyCode {
PyCode { code }
pub fn new(code: CodeObject) -> Self {
Self { code }
}
}

Expand Down Expand Up @@ -335,7 +335,7 @@ impl PyCode {
}

#[pymethod]
pub fn replace(&self, args: ReplaceArgs, vm: &VirtualMachine) -> PyResult<PyCode> {
pub fn replace(&self, args: ReplaceArgs, vm: &VirtualMachine) -> PyResult<Self> {
let posonlyarg_count = match args.co_posonlyargcount {
OptionalArg::Present(posonlyarg_count) => posonlyarg_count,
OptionalArg::Missing => self.code.posonlyarg_count,
Expand Down Expand Up @@ -392,7 +392,7 @@ impl PyCode {
OptionalArg::Missing => self.code.varnames.iter().map(|s| s.to_object()).collect(),
};

Ok(PyCode {
Ok(Self {
code: CodeObject {
flags: CodeFlags::from_bits_truncate(flags),
posonlyarg_count,
Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl ToPyObject for Complex64 {

impl From<Complex64> for PyComplex {
fn from(value: Complex64) -> Self {
PyComplex { value }
Self { value }
}
}

Expand Down Expand Up @@ -161,7 +161,7 @@ impl Constructor for PyComplex {
OptionalArg::Missing => (Complex64::new(0.0, 0.0), false),
OptionalArg::Present(val) => {
let val = if cls.is(vm.ctx.types.complex_type) && imag_missing {
match val.downcast_exact::<PyComplex>(vm) {
match val.downcast_exact::<Self>(vm) {
Ok(c) => {
return Ok(c.into_pyref().into());
}
Expand Down Expand Up @@ -393,7 +393,7 @@ impl PyComplex {
#[pyclass]
impl PyRef<PyComplex> {
#[pymethod]
fn __complex__(self, vm: &VirtualMachine) -> PyRef<PyComplex> {
fn __complex__(self, vm: &VirtualMachine) -> Self {
if self.is(vm.ctx.types.complex_type) {
self
} else {
Expand All @@ -410,7 +410,7 @@ impl Comparable for PyComplex {
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
op.eq_only(|| {
let result = if let Some(other) = other.payload_if_subclass::<PyComplex>(vm) {
let result = if let Some(other) = other.payload_if_subclass::<Self>(vm) {
if zelf.value.re.is_nan()
&& zelf.value.im.is_nan()
&& other.value.re.is_nan()
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl PyCoroutine {
}

pub fn new(frame: FrameRef, name: PyStrRef) -> Self {
PyCoroutine {
Self {
inner: Coro::new(frame, name),
}
}
Expand Down
Loading