GeezerVison is showing github.com — __str__ slot wrapper · RustPython/RustPython@340df35
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: crates/vm/src/builtins/descriptor.rs +9-4Lines changed: 9 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -399,6 +399,7 @@ pub fn init(ctx: &Context) { | 399 | 399 | pub enum SlotFunc { | 400 | 400 | Init(InitFunc), | 401 | 401 | Hash(HashFunc), | | 402 | + Str(StringifyFunc), | 402 | 403 | Repr(StringifyFunc), | 403 | 404 | Iter(IterFunc), | 404 | 405 | IterNext(IterNextFunc), | @@ -409,6 +410,7 @@ impl std::fmt::Debug for SlotFunc { | 409 | 410 | match self { | 410 | 411 | SlotFunc::Init(_) => write!(f, "SlotFunc::Init(...)"), | 411 | 412 | SlotFunc::Hash(_) => write!(f, "SlotFunc::Hash(...)"), | | 413 | + SlotFunc::Str(_) => write!(f, "SlotFunc::Str(...)"), | 412 | 414 | SlotFunc::Repr(_) => write!(f, "SlotFunc::Repr(...)"), | 413 | 415 | SlotFunc::Iter(_) => write!(f, "SlotFunc::Iter(...)"), | 414 | 416 | SlotFunc::IterNext(_) => write!(f, "SlotFunc::IterNext(...)"), | @@ -433,11 +435,14 @@ impl SlotFunc { | 433 | 435 | let hash = func(&obj, vm)?; | 434 | 436 | Ok(vm.ctx.new_int(hash).into()) | 435 | 437 | } | 436 | | - SlotFunc::Repr(func) => { | | 438 | + SlotFunc::Repr(func) | SlotFunc::Str(func) => { | 437 | 439 | 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)"))); | 441 | 446 | } | 442 | 447 | let s = func(&obj, vm)?; | 443 | 448 | Ok(s.into()) | |
Collapse file Copy file name to clipboardExpand all lines: crates/vm/src/builtins/object.rs +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -425,8 +425,8 @@ impl PyBaseObject { | 425 | 425 | } | 426 | 426 |
| 427 | 427 | /// 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> { | 430 | 430 | // FIXME: try tp_repr first and fallback to object.__repr__ | 431 | 431 | zelf.repr(vm) | 432 | 432 | } | |
Collapse file Copy file name to clipboardExpand all lines: crates/vm/src/types/slot.rs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -140,7 +140,7 @@ pub struct PyTypeSlots { | 140 | 140 | // More standard operations (here for binary compatibility) | 141 | 141 | pub hash: AtomicCell<Option<HashFunc>>, | 142 | 142 | pub call: AtomicCell<Option<GenericMethod>>, | 143 | | - // tp_str | | 143 | + pub str: AtomicCell<Option<StringifyFunc>>, | 144 | 144 | pub repr: AtomicCell<Option<StringifyFunc>>, | 145 | 145 | pub getattro: AtomicCell<Option<GetattroFunc>>, | 146 | 146 | pub setattro: AtomicCell<Option<SetattroFunc>>, | |
|