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 — __str__ slot wrapper · RustPython/RustPython@340df35

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • crates/vm/src
    • builtins
      • descriptor.rs
      • object.rs
    • types
      • slot.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎crates/vm/src/builtins/descriptor.rs‎

Copy file name to clipboardExpand all lines: crates/vm/src/builtins/descriptor.rs
+9-4Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ pub fn init(ctx: &Context) {
399399
pub enum SlotFunc {
400400
Init(InitFunc),
401401
Hash(HashFunc),
402+
Str(StringifyFunc),
402403
Repr(StringifyFunc),
403404
Iter(IterFunc),
404405
IterNext(IterNextFunc),
@@ -409,6 +410,7 @@ impl std::fmt::Debug for SlotFunc {
409410
match self {
410411
SlotFunc::Init(_) => write!(f, "SlotFunc::Init(...)"),
411412
SlotFunc::Hash(_) => write!(f, "SlotFunc::Hash(...)"),
413+
SlotFunc::Str(_) => write!(f, "SlotFunc::Str(...)"),
412414
SlotFunc::Repr(_) => write!(f, "SlotFunc::Repr(...)"),
413415
SlotFunc::Iter(_) => write!(f, "SlotFunc::Iter(...)"),
414416
SlotFunc::IterNext(_) => write!(f, "SlotFunc::IterNext(...)"),
@@ -433,11 +435,14 @@ impl SlotFunc {
433435
let hash = func(&obj, vm)?;
434436
Ok(vm.ctx.new_int(hash).into())
435437
}
436-
SlotFunc::Repr(func) => {
438+
SlotFunc::Repr(func) | SlotFunc::Str(func) => {
437439
if !args.args.is_empty() || !args.kwargs.is_empty() {
438-
return Err(
439-
vm.new_type_error("__repr__() takes no arguments (1 given)".to_owned())
440-
);
440+
let name = match self {
441+
SlotFunc::Repr(_) => "__repr__",
442+
SlotFunc::Str(_) => "__str__",
443+
_ => unreachable!(),
444+
};
445+
return Err(vm.new_type_error(format!("{name}() takes no arguments (1 given)")));
441446
}
442447
let s = func(&obj, vm)?;
443448
Ok(s.into())
Collapse file

‎crates/vm/src/builtins/object.rs‎

Copy file name to clipboardExpand all lines: crates/vm/src/builtins/object.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ impl PyBaseObject {
425425
}
426426

427427
/// Return str(self).
428-
#[pymethod]
429-
fn __str__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyStrRef> {
428+
#[pyslot]
429+
fn slot_str(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> {
430430
// FIXME: try tp_repr first and fallback to object.__repr__
431431
zelf.repr(vm)
432432
}
Collapse file

‎crates/vm/src/types/slot.rs‎

Copy file name to clipboardExpand all lines: crates/vm/src/types/slot.rs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub struct PyTypeSlots {
140140
// More standard operations (here for binary compatibility)
141141
pub hash: AtomicCell<Option<HashFunc>>,
142142
pub call: AtomicCell<Option<GenericMethod>>,
143-
// tp_str
143+
pub str: AtomicCell<Option<StringifyFunc>>,
144144
pub repr: AtomicCell<Option<StringifyFunc>>,
145145
pub getattro: AtomicCell<Option<GetattroFunc>>,
146146
pub setattro: AtomicCell<Option<SetattroFunc>>,

0 commit comments

Comments
0 (0)
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.