GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add tp_str by youknowone · Pull Request #6495 · RustPython/RustPython

Add tp_str #6495

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.

Sign up for GitHub

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
youknowone merged 2 commits into RustPython:main from youknowone:str-slot
Dec 25, 2025

Uh oh!

There was an error while loading. Please reload this page.

Merged

Add tp_str #6495

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
20f12d3
clean up hash
youknowone Dec 24, 2025
ed736da
__str__ slot wrapper
youknowone Dec 24, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
__str__ slot wrapper
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@youknowone
youknowone committed Dec 25, 2025
commit ed736da99e6772ffc283411a6f0228996a48723d

There are no files selected for viewing

1 change: 0 additions & 1 deletion 1 Lib/test/test_inspect/test_inspect.py
Show comments
View file
Open in desktop
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 13 crates/vm/src/builtins/descriptor.rs
Show comments
View file
Open in desktop
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 4 crates/vm/src/builtins/object.rs
Show comments
View file
Open in desktop
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 thread
Comment on lines +428 to 432

@coderabbitai coderabbitai Bot Dec 25, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Expand Down
2 changes: 1 addition & 1 deletion 2 crates/vm/src/types/slot.rs
Show comments
View file
Open in desktop
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
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.