Skip to content

Fix str.__str__ and PyRef::into_exact_or for int() or str() pattern #4732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
57 changes: 27 additions & 30 deletions vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
},
protocol::{PyNumber, PyNumberMethods},
types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp, Representable},
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult,
TryFromBorrowedObject, VirtualMachine,
};
use num_bigint::{BigInt, Sign};
Expand Down Expand Up @@ -321,7 +321,7 @@ impl PyInt {

#[pyclass(
flags(BASETYPE),
with(Comparable, Hashable, Constructor, AsNumber, Representable)
with(PyRef, Comparable, Hashable, Constructor, AsNumber, Representable)
)]
impl PyInt {
#[pymethod(name = "__radd__")]
Expand Down Expand Up @@ -521,11 +521,6 @@ impl PyInt {
Ok(zelf)
}

#[pymethod(magic)]
fn int(zelf: PyRef<Self>) -> PyRef<Self> {
zelf
}

#[pymethod(magic)]
fn pos(&self) -> BigInt {
self.value.clone()
Expand All @@ -537,23 +532,23 @@ impl PyInt {
}

#[pymethod(magic)]
fn trunc(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<Self> {
Self::clone_if_subclass(zelf, vm)
fn trunc(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRefExact<Self> {
zelf.int(vm)
}

#[pymethod(magic)]
fn floor(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<Self> {
Self::clone_if_subclass(zelf, vm)
fn floor(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRefExact<Self> {
zelf.int(vm)
}

#[pymethod(magic)]
fn ceil(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<Self> {
Self::clone_if_subclass(zelf, vm)
fn ceil(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRefExact<Self> {
zelf.int(vm)
}

#[pymethod(magic)]
fn index(zelf: PyRef<Self>) -> PyRef<Self> {
zelf
fn index(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRefExact<Self> {
zelf.int(vm)
}

#[pymethod(magic)]
Expand Down Expand Up @@ -589,8 +584,8 @@ impl PyInt {
}

#[pymethod]
fn conjugate(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<Self> {
Self::clone_if_subclass(zelf, vm)
fn conjugate(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRefExact<Self> {
zelf.int(vm)
}

#[pyclassmethod]
Expand Down Expand Up @@ -659,18 +654,9 @@ impl PyInt {
Ok(bytes.into())
}

#[inline]
fn clone_if_subclass(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<Self> {
if zelf.class().is(vm.ctx.types.int_type) {
return zelf;
}

vm.ctx.new_bigint(&zelf.value)
}

#[pygetset]
fn real(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<Self> {
Self::clone_if_subclass(zelf, vm)
fn real(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRefExact<Self> {
zelf.int(vm)
}

#[pygetset]
Expand All @@ -679,8 +665,8 @@ impl PyInt {
}

#[pygetset]
fn numerator(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<Self> {
Self::clone_if_subclass(zelf, vm)
fn numerator(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRefExact<Self> {
zelf.int(vm)
}

#[pygetset]
Expand All @@ -701,6 +687,17 @@ impl PyInt {
}
}

#[pyclass]
impl PyRef<PyInt> {
#[pymethod(magic)]
fn int(self, vm: &VirtualMachine) -> PyRefExact<PyInt> {
self.into_exact_or(&vm.ctx, |zelf| unsafe {
// TODO: this is actually safe. we need better interface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should definitely start documenting all the unsafe better at some point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's the allow-by-default undocumented_unsafe_blocks lint, in case we want to activate that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That clippy option looks very good for us because we use a lot of unsafe blocks.

For this function, I'd like to make the basic PyRef creation functions to return PyRefExact first and turn them to PyRef later. In that case, everything will be safe by both semantics and rust grammars.

PyRefExact::new_unchecked(vm.ctx.new_bigint(&zelf.value))
})
}
}

impl Comparable for PyInt {
fn cmp(
zelf: &Py<Self>,
Expand Down
17 changes: 12 additions & 5 deletions vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ impl PyStr {
#[pyclass(
flags(BASETYPE),
with(
PyRef,
AsMapping,
AsNumber,
AsSequence,
Expand Down Expand Up @@ -481,11 +482,6 @@ impl PyStr {
Self::repeat(zelf, value.into(), vm)
}

#[pymethod(magic)]
fn str(zelf: PyRef<Self>) -> PyStrRef {
zelf
}

#[inline]
pub(crate) fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
rustpython_common::str::repr(self.as_str())
Expand Down Expand Up @@ -1270,6 +1266,17 @@ impl PyStr {
}
}

#[pyclass]
impl PyRef<PyStr> {
#[pymethod(magic)]
fn str(self, vm: &VirtualMachine) -> PyRefExact<PyStr> {
self.into_exact_or(&vm.ctx, |zelf| unsafe {
// Creating a copy with same kind is safe
PyStr::new_str_unchecked(zelf.bytes.to_vec(), zelf.kind.kind()).into_exact_ref(&vm.ctx)
})
}
}

impl PyStrRef {
pub fn concat_in_place(&mut self, other: &str, vm: &VirtualMachine) {
// TODO: call [A]Rc::get_mut on the str to try to mutate the data in place
Expand Down
15 changes: 15 additions & 0 deletions vm/src/object/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::common::{
use crate::{
builtins::{PyBaseExceptionRef, PyStrInterned, PyType},
convert::{IntoPyException, ToPyObject, ToPyResult, TryFromObject},
vm::Context,
VirtualMachine,
};
use std::{borrow::Borrow, fmt, marker::PhantomData, ops::Deref, ptr::null_mut};
Expand Down Expand Up @@ -107,6 +108,20 @@ impl<T: PyPayload> std::borrow::ToOwned for PyExact<T> {
}
}

impl<T: PyPayload> PyRef<T> {
pub fn into_exact_or(
self,
ctx: &Context,
f: impl FnOnce(Self) -> PyRefExact<T>,
) -> PyRefExact<T> {
if self.class().is(T::class(ctx)) {
unsafe { PyRefExact::new_unchecked(self) }
} else {
f(self)
}
}
}

/// PyRef but guaranteed not to be a subtype instance
#[derive(Debug)]
#[repr(transparent)]
Expand Down