Skip to content
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
4 changes: 2 additions & 2 deletions common/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ pub mod levenshtein {
if a == b { CASE_COST } else { MOVE_COST }
}

pub fn levenshtein_distance(a: &str, b: &str, max_cost: usize) -> usize {
pub fn levenshtein_distance(a: &[u8], b: &[u8], max_cost: usize) -> usize {
thread_local! {
#[allow(clippy::declare_interior_mutable_const)]
static BUFFER: RefCell<[usize; MAX_STRING_SIZE]> = const {
Expand All @@ -499,7 +499,7 @@ pub mod levenshtein {
return 0;
}

let (mut a_bytes, mut b_bytes) = (a.as_bytes(), b.as_bytes());
let (mut a_bytes, mut b_bytes) = (a, b);
let (mut a_begin, mut a_end) = (0usize, a.len());
let (mut b_begin, mut b_end) = (0usize, b.len());

Expand Down
5 changes: 3 additions & 2 deletions vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ pub(crate) fn init(context: &Context) {
}

impl PyByteArray {
#[deprecated(note = "use PyByteArray::from(...).into_ref() instead")]
pub fn new_ref(data: Vec<u8>, ctx: &Context) -> PyRef<Self> {
PyRef::new_ref(Self::from(data), ctx.types.bytearray_type.to_owned(), None)
Self::from(data).into_ref(ctx)
}

const fn from_inner(inner: PyBytesInner) -> Self {
Expand Down Expand Up @@ -328,7 +329,7 @@ impl PyByteArray {

#[pyclassmethod]
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
let bytes = PyBytesInner::fromhex(string.as_str(), vm)?;
let bytes = PyBytesInner::fromhex(string.as_bytes(), vm)?;
let bytes = vm.ctx.new_bytes(bytes);
let args = vec![bytes.into()].into();
PyType::call(&cls, args, vm)
Expand Down
5 changes: 3 additions & 2 deletions vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ impl Constructor for PyBytes {
}

impl PyBytes {
#[deprecated(note = "use PyBytes::from(...).into_ref() instead")]
pub fn new_ref(data: Vec<u8>, ctx: &Context) -> PyRef<Self> {
PyRef::new_ref(Self::from(data), ctx.types.bytes_type.to_owned(), None)
Self::from(data).into_ref(ctx)
}

fn _getitem(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
Expand Down Expand Up @@ -264,7 +265,7 @@ impl PyBytes {

#[pyclassmethod]
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
let bytes = PyBytesInner::fromhex(string.as_str(), vm)?;
let bytes = PyBytesInner::fromhex(string.as_bytes(), vm)?;
let bytes = vm.ctx.new_bytes(bytes).into();
PyType::call(&cls, vec![bytes].into(), vm)
}
Expand Down
9 changes: 2 additions & 7 deletions vm/src/builtins/classmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,9 @@ impl Initializer for PyClassMethod {
}

impl PyClassMethod {
#[deprecated(note = "use PyClassMethod::from(...).into_ref() instead")]
pub fn new_ref(callable: PyObjectRef, ctx: &Context) -> PyRef<Self> {
PyRef::new_ref(
Self {
callable: PyMutex::new(callable),
},
ctx.types.classmethod_type.to_owned(),
None,
)
Self::from(callable).into_ref(ctx)
}
}

Expand Down
39 changes: 18 additions & 21 deletions vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ pub struct PyComplex {
value: Complex64,
}

impl PyComplex {
pub const fn to_complex64(self) -> Complex64 {
self.value
}
}

impl PyPayload for PyComplex {
#[inline]
fn class(ctx: &Context) -> &'static Py<PyType> {
Expand Down Expand Up @@ -234,13 +228,30 @@ impl Constructor for PyComplex {
}

impl PyComplex {
#[deprecated(note = "use PyComplex::from(...).into_ref() instead")]
pub fn new_ref(value: Complex64, ctx: &Context) -> PyRef<Self> {
PyRef::new_ref(Self::from(value), ctx.types.complex_type.to_owned(), None)
Self::from(value).into_ref(ctx)
}

pub const fn to_complex64(self) -> Complex64 {
self.value
}

pub const fn to_complex(&self) -> Complex64 {
self.value
}

fn number_op<F, R>(a: &PyObject, b: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
where
F: FnOnce(Complex64, Complex64, &VirtualMachine) -> R,
R: ToPyResult,
{
if let (Some(a), Some(b)) = (to_op_complex(a, vm)?, to_op_complex(b, vm)?) {
op(a, b, vm).to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
}
}

#[pyclass(
Expand Down Expand Up @@ -503,20 +514,6 @@ impl Representable for PyComplex {
}
}

impl PyComplex {
fn number_op<F, R>(a: &PyObject, b: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
where
F: FnOnce(Complex64, Complex64, &VirtualMachine) -> R,
R: ToPyResult,
{
if let (Some(a), Some(b)) = (to_op_complex(a, vm)?, to_op_complex(b, vm)?) {
op(a, b, vm).to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
}
}

#[derive(FromArgs)]
pub struct ComplexArgs {
#[pyarg(any, optional)]
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ impl PyPayload for PyDict {
}

impl PyDict {
#[deprecated(note = "use PyDict::default().into_ref() instead")]
pub fn new_ref(ctx: &Context) -> PyRef<Self> {
PyRef::new_ref(Self::default(), ctx.types.dict_type.to_owned(), None)
Self::default().into_ref(ctx)
}

/// escape hatch to access the underlying data structure directly. prefer adding a method on
Expand Down
16 changes: 8 additions & 8 deletions vm/src/builtins/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,9 @@ impl PyBoundMethod {
Self { object, function }
}

#[deprecated(note = "Use `Self::new(object, function).into_ref(ctx)` instead")]
pub fn new_ref(object: PyObjectRef, function: PyObjectRef, ctx: &Context) -> PyRef<Self> {
PyRef::new_ref(
Self::new(object, function),
ctx.types.bound_method_type.to_owned(),
None,
)
Self::new(object, function).into_ref(ctx)
}
}

Expand Down Expand Up @@ -859,10 +856,13 @@ impl Representable for PyBoundMethod {
vm.get_attribute_opt(zelf.function.clone(), "__name__")?
};
let func_name: Option<PyStrRef> = func_name.and_then(|o| o.downcast().ok());
let formatted_func_name = match func_name {
Some(name) => name.to_string(),
None => "?".to_string(),
};
let object_repr = zelf.object.repr(vm)?;
Ok(format!(
"<bound method {} of {}>",
func_name.as_ref().map_or("?", |s| s.as_str()),
&zelf.object.repr(vm)?.as_str(),
"<bound method {formatted_func_name} of {object_repr}>",
))
}
}
Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/genericalias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ impl PyGenericAlias {
.get_attribute_opt(obj.clone(), identifier!(vm, __args__))?
.is_some()
{
return Ok(obj.repr(vm)?.as_str().to_string());
return Ok(obj.repr(vm)?.to_string());
}

match (
vm.get_attribute_opt(obj.clone(), identifier!(vm, __qualname__))?
.and_then(|o| o.downcast_ref::<PyStr>().map(|n| n.as_str().to_string())),
.and_then(|o| o.downcast_ref::<PyStr>().map(|n| n.to_string())),
vm.get_attribute_opt(obj.clone(), identifier!(vm, __module__))?
.and_then(|o| o.downcast_ref::<PyStr>().map(|m| m.as_str().to_string())),
.and_then(|o| o.downcast_ref::<PyStr>().map(|m| m.to_string())),
) {
(None, _) | (_, None) => Ok(obj.repr(vm)?.as_str().to_string()),
(None, _) | (_, None) => Ok(obj.repr(vm)?.to_string()),
(Some(qualname), Some(module)) => Ok(if module == "builtins" {
qualname
} else {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ impl ToPyObject for Vec<PyObjectRef> {
}

impl PyList {
#[deprecated(note = "use PyList::from(...).into_ref() instead")]
pub fn new_ref(elements: Vec<PyObjectRef>, ctx: &Context) -> PyRef<Self> {
PyRef::new_ref(Self::from(elements), ctx.types.list_type.to_owned(), None)
Self::from(elements).into_ref(ctx)
}

pub fn borrow_vec(&self) -> PyMappedRwLockReadGuard<'_, [PyObjectRef]> {
Expand Down
10 changes: 0 additions & 10 deletions vm/src/builtins/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ impl PyPayload for PyNamespace {

impl DefaultConstructor for PyNamespace {}

impl PyNamespace {
pub fn new_ref(ctx: &Context) -> PyRef<Self> {
PyRef::new_ref(
Self {},
ctx.types.namespace_type.to_owned(),
Some(ctx.new_dict()),
)
}
}

#[pyclass(
flags(BASETYPE, HAS_DICT),
with(Constructor, Initializer, Comparable, Representable)
Expand Down
5 changes: 2 additions & 3 deletions vm/src/builtins/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ pub struct PySet {
}

impl PySet {
#[deprecated(note = "Use `PySet::default().into_ref(ctx)` instead")]
pub fn new_ref(ctx: &Context) -> PyRef<Self> {
// Initialized empty, as calling __hash__ is required for adding each object to the set
// which requires a VM context - this is done in the set code itself.
PyRef::new_ref(Self::default(), ctx.types.set_type.to_owned(), None)
Self::default().into_ref(ctx)
}

pub fn elements(&self) -> Vec<PyObjectRef> {
Expand Down
14 changes: 7 additions & 7 deletions vm/src/builtins/staticmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ impl Constructor for PyStaticMethod {
}

impl PyStaticMethod {
pub fn new(callable: PyObjectRef) -> Self {
Self {
callable: PyMutex::new(callable),
}
}
#[deprecated(note = "use PyStaticMethod::new(...).into_ref() instead")]
pub fn new_ref(callable: PyObjectRef, ctx: &Context) -> PyRef<Self> {
PyRef::new_ref(
Self {
callable: PyMutex::new(callable),
},
ctx.types.staticmethod_type.to_owned(),
None,
)
Self::new(callable).into_ref(ctx)
}
}

Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,10 @@ impl PyStr {
unsafe { AsciiString::from_ascii_unchecked(bytes) }.into()
}

#[deprecated(note = "use PyStr::from(...).into_ref() instead")]
pub fn new_ref(zelf: impl Into<Self>, ctx: &Context) -> PyRef<Self> {
let zelf = zelf.into();
PyRef::new_ref(zelf, ctx.types.str_type.to_owned(), None)
zelf.into_ref(ctx)
}

fn new_substr(&self, s: Wtf8Buf) -> Self {
Expand Down
1 change: 1 addition & 0 deletions vm/src/builtins/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl<R> PyTuple<R> {
}

impl PyTuple<PyObjectRef> {
// Do not deprecate this. empty_tuple must be checked.
pub fn new_ref(elements: Vec<PyObjectRef>, ctx: &Context) -> PyRef<Self> {
if elements.is_empty() {
ctx.empty_tuple.clone()
Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ impl PyUnion {
.get_attribute_opt(obj.clone(), identifier!(vm, __args__))?
.is_some()
{
return Ok(obj.repr(vm)?.as_str().to_string());
return Ok(obj.repr(vm)?.to_string());
}

match (
vm.get_attribute_opt(obj.clone(), identifier!(vm, __qualname__))?
.and_then(|o| o.downcast_ref::<PyStr>().map(|n| n.as_str().to_string())),
.and_then(|o| o.downcast_ref::<PyStr>().map(|n| n.to_string())),
vm.get_attribute_opt(obj.clone(), identifier!(vm, __module__))?
.and_then(|o| o.downcast_ref::<PyStr>().map(|m| m.as_str().to_string())),
.and_then(|o| o.downcast_ref::<PyStr>().map(|m| m.to_string())),
) {
(None, _) | (_, None) => Ok(obj.repr(vm)?.as_str().to_string()),
(None, _) | (_, None) => Ok(obj.repr(vm)?.to_string()),
(Some(qualname), Some(module)) => Ok(if module == "builtins" {
qualname
} else {
Expand Down
8 changes: 4 additions & 4 deletions vm/src/bytes_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,11 @@ impl PyBytesInner {
bytes_to_hex(self.elements.as_slice(), sep, bytes_per_sep, vm)
}

pub fn fromhex(string: &str, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
let mut iter = string.bytes().enumerate();
let mut bytes: Vec<u8> = Vec::with_capacity(string.len() / 2);
pub fn fromhex(bytes: &[u8], vm: &VirtualMachine) -> PyResult<Vec<u8>> {
let mut iter = bytes.iter().enumerate();
let mut bytes: Vec<u8> = Vec::with_capacity(bytes.len() / 2);
let i = loop {
let (i, b) = match iter.next() {
let (i, &b) = match iter.next() {
Some(val) => val,
None => {
return Ok(bytes);
Expand Down
4 changes: 2 additions & 2 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,15 +756,15 @@ impl ExecutingFrame<'_> {
Ok(None)
}
bytecode::Instruction::BuildSet { size } => {
let set = PySet::new_ref(&vm.ctx);
let set = PySet::default().into_ref(&vm.ctx);
for element in self.pop_multiple(size.get(arg) as usize) {
set.add(element, vm)?;
}
self.push_value(set.into());
Ok(None)
}
bytecode::Instruction::BuildSetFromTuples { size } => {
let set = PySet::new_ref(&vm.ctx);
let set = PySet::default().into_ref(&vm.ctx);
for element in self.pop_multiple(size.get(arg) as usize) {
// SAFETY: trust compiler
let tup = unsafe { element.downcast_unchecked::<PyTuple>() };
Expand Down
11 changes: 4 additions & 7 deletions vm/src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ impl CachedPyStrRef {
}
}

pub struct PyInterned<T>
where
T: PyPayload,
{
pub struct PyInterned<T> {
inner: Py<T>,
}

Expand Down Expand Up @@ -173,14 +170,14 @@ impl<T: PyPayload> std::hash::Hash for PyInterned<T> {
}
}

impl<T: PyPayload> AsRef<Py<T>> for PyInterned<T> {
impl<T> AsRef<Py<T>> for PyInterned<T> {
#[inline(always)]
fn as_ref(&self) -> &Py<T> {
&self.inner
}
}

impl<T: PyPayload> Deref for PyInterned<T> {
impl<T> Deref for PyInterned<T> {
type Target = Py<T>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
Expand All @@ -197,7 +194,7 @@ impl<T: PyPayload> PartialEq for PyInterned<T> {

impl<T: PyPayload> Eq for PyInterned<T> {}

impl<T: PyPayload + std::fmt::Debug> std::fmt::Debug for PyInterned<T> {
impl<T: std::fmt::Debug + PyPayload> std::fmt::Debug for PyInterned<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&**self, f)?;
write!(f, "@{:p}", self.as_ptr())
Expand Down
2 changes: 1 addition & 1 deletion vm/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ macro_rules! extend_class {
macro_rules! py_namespace {
( $vm:expr, { $($name:expr => $value:expr),* $(,)* }) => {
{
let namespace = $crate::builtins::PyNamespace::new_ref(&$vm.ctx);
let namespace = $crate::object::PyPayload::into_ref($crate::builtins::PyNamespace {}, &$vm.ctx);
let obj = $crate::object::AsObject::as_object(&namespace);
$(
obj.generic_setattr($vm.ctx.intern_str($name), $crate::function::PySetterValue::Assign($value.into()), $vm).unwrap();
Expand Down
Loading