Skip to content
Open
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
1 change: 0 additions & 1 deletion Lib/test/test_funcattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ def f():


class CellTest(unittest.TestCase):
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_comparison(self):
# These tests are here simply to exercise the comparison code;
# their presence should not be interpreted as providing any
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_reprlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ def test_nesting(self):
eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]")
eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]")

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_cell(self):
def get_cell():
x = 42
Expand Down
48 changes: 45 additions & 3 deletions crates/vm/src/builtins/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
class::PyClassImpl,
common::wtf8::{Wtf8Buf, wtf8_concat},
frame::{FrameObject, FrameObjectRef},
function::{FuncArgs, OptionalArg, PyComparisonValue, PySetterValue},
function::{Either, FuncArgs, OptionalArg, PyComparisonValue, PySetterValue},
scope::Scope,
types::{
Callable, Comparable, Constructor, GetAttr, GetDescriptor, Hashable, PyComparisonOp,
Expand Down Expand Up @@ -1507,7 +1507,7 @@ impl Representable for PyBoundMethod {
}
}

#[pyclass(module = false, name = "cell", traverse)]
#[pyclass(module = false, name = "cell", unhashable = true, traverse)]
#[derive(Debug, Default)]
pub(crate) struct PyCell {
contents: PyMutex<Option<PyObjectRef>>,
Expand All @@ -1530,8 +1530,26 @@ impl Constructor for PyCell {
}
}

#[pyclass(with(Constructor))]
#[pyclass(with(Constructor, Representable))]
impl PyCell {
#[pyslot]
fn slot_richcompare(
zelf: &PyObject,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<Either<PyObjectRef, PyComparisonValue>> {
let (Some(zelf), Some(other)) = (zelf.downcast_ref::<Self>(), other.downcast_ref::<Self>())
else {
return Ok(Either::B(PyComparisonValue::NotImplemented));
};
// compare cells by contents; empty cells come before anything else
match (zelf.get(), other.get()) {
(Some(a), Some(b)) => a.rich_compare(b, op, vm).map(Either::A),
(a, b) => Ok(Either::B(op.eval_ord(b.is_none().cmp(&a.is_none())).into())),
}
}

pub(crate) const fn new(contents: Option<PyObjectRef>) -> Self {
Self {
contents: PyMutex::new(contents),
Expand Down Expand Up @@ -1561,6 +1579,30 @@ impl PyCell {
}
}

impl Representable for PyCell {
#[inline]
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
let id = zelf.get_id();
Ok(match zelf.get() {
Some(value) => {
let type_name = value.class().slot_name();
// CPython renders the type name with "%.80s", which reads at
// most 80 bytes and drops a character left incomplete by the cut.
let mut end = type_name.len().min(80);
while !type_name.is_char_boundary(end) {
end -= 1;
}
format!(
"<cell at {id:#x}: {} object at {:#x}>",
&type_name[..end],
value.get_id()
)
}
None => format!("<cell at {id:#x}: empty>"),
})
}
}

/// Vectorcall implementation for PyFunction (PEP 590).
/// Takes owned args to avoid cloning when filling fastlocals.
pub(crate) fn vectorcall_function(
Expand Down
Loading