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
1 change: 0 additions & 1 deletion Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ class NotFuture: pass

coro.close(); gen_coro.close() # silence warnings

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_isroutine(self):
# method
self.assertTrue(inspect.isroutine(git.argue))
Expand Down
13 changes: 9 additions & 4 deletions crates/vm/src/builtins/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ pub fn init(ctx: &Context) {
pub enum SlotFunc {
Init(InitFunc),
Hash(HashFunc),
Str(StringifyFunc),
Repr(StringifyFunc),
Iter(IterFunc),
IterNext(IterNextFunc),
Expand All @@ -409,6 +410,7 @@ impl std::fmt::Debug for SlotFunc {
match self {
SlotFunc::Init(_) => write!(f, "SlotFunc::Init(...)"),
SlotFunc::Hash(_) => write!(f, "SlotFunc::Hash(...)"),
SlotFunc::Str(_) => write!(f, "SlotFunc::Str(...)"),
SlotFunc::Repr(_) => write!(f, "SlotFunc::Repr(...)"),
SlotFunc::Iter(_) => write!(f, "SlotFunc::Iter(...)"),
SlotFunc::IterNext(_) => write!(f, "SlotFunc::IterNext(...)"),
Expand All @@ -433,11 +435,14 @@ impl SlotFunc {
let hash = func(&obj, vm)?;
Ok(vm.ctx.new_int(hash).into())
}
SlotFunc::Repr(func) => {
SlotFunc::Repr(func) | SlotFunc::Str(func) => {
if !args.args.is_empty() || !args.kwargs.is_empty() {
return Err(
vm.new_type_error("__repr__() takes no arguments (1 given)".to_owned())
);
let name = match self {
SlotFunc::Repr(_) => "__repr__",
SlotFunc::Str(_) => "__str__",
_ => unreachable!(),
};
return Err(vm.new_type_error(format!("{name}() takes no arguments (1 given)")));
}
let s = func(&obj, vm)?;
Ok(s.into())
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ impl PyBaseObject {
}

/// Return str(self).
#[pymethod]
fn __str__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyStrRef> {
#[pyslot]
fn slot_str(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> {
// FIXME: try tp_repr first and fallback to object.__repr__
zelf.repr(vm)
}
Comment on lines +428 to 432
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Minor: Clarify the FIXME comment.

The conversion to a slot-based implementation is correct, but the FIXME comment is confusing. It mentions "try tp_repr first" but this is the __str__ slot implementation. While the fallback to repr is correct per CPython behavior (where object.__str__ calls object.__repr__), the comment should clarify this intent.

🔎 Suggested comment improvement
-    // FIXME: try tp_repr first and fallback to object.__repr__
+    // Default __str__ implementation falls back to __repr__
+    // CPython behavior: object.__str__() calls object.__repr__()
     fn slot_str(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[pyslot]
fn slot_str(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> {
// FIXME: try tp_repr first and fallback to object.__repr__
zelf.repr(vm)
}
#[pyslot]
fn slot_str(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> {
// Default __str__ implementation falls back to __repr__
// CPython behavior: object.__str__() calls object.__repr__()
zelf.repr(vm)
}
🤖 Prompt for AI Agents
In crates/vm/src/builtins/object.rs around lines 428 to 432, the FIXME comment
is misleading because this is the __str__ slot implementation and should clarify
that we call the object's str (tp_str) and, per CPython semantics for
object.__str__, fall back to object.__repr__ via zelf.repr(vm); update the
comment to explicitly state "This is the __str__ slot: use tp_str, and fall back
to object.__repr__ (implemented here by calling zelf.repr(vm))" so future
readers understand the intent.

Expand Down
20 changes: 5 additions & 15 deletions crates/vm/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,15 @@ pub trait PyClassImpl: PyClassDef {
"Initialize self. See help(type(self)) for accurate signature."
);
add_slot_wrapper!(repr, __repr__, Repr, "Return repr(self).");
add_slot_wrapper!(str, __str__, Str, "Return str(self).");
add_slot_wrapper!(iter, __iter__, Iter, "Implement iter(self).");
add_slot_wrapper!(iternext, __next__, IterNext, "Implement next(self).");

// __hash__ needs special handling: hash_not_implemented sets __hash__ = None
if let Some(hash_func) = class.slots.hash.load() {
if hash_func as usize == hash_not_implemented as usize {
class.set_attr(ctx.names.__hash__, ctx.none.clone().into());
} else {
let hash_name = identifier!(ctx, __hash__);
if !class.attributes.read().contains_key(hash_name) {
let wrapper = PySlotWrapper {
typ: class,
name: ctx.intern_str("__hash__"),
wrapped: SlotFunc::Hash(hash_func),
doc: Some("Return hash(self)."),
};
class.set_attr(hash_name, wrapper.into_ref(ctx).into());
}
}
if class.slots.hash.load().map_or(0, |h| h as usize) == hash_not_implemented as usize {
class.set_attr(ctx.names.__hash__, ctx.none.clone().into());
} else {
add_slot_wrapper!(hash, __hash__, Hash, "Return hash(self).");
}

class.extend_methods(class.slots.methods, ctx);
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub struct PyTypeSlots {
// More standard operations (here for binary compatibility)
pub hash: AtomicCell<Option<HashFunc>>,
pub call: AtomicCell<Option<GenericMethod>>,
// tp_str
pub str: AtomicCell<Option<StringifyFunc>>,
pub repr: AtomicCell<Option<StringifyFunc>>,
pub getattro: AtomicCell<Option<GetattroFunc>>,
pub setattro: AtomicCell<Option<SetattroFunc>>,
Expand Down
Loading