Skip to content

Commit f21ad9b

Browse files
authored
Merge pull request RustPython#4689 from haxelion/feature/tp_repr
Implemented repr slot
2 parents 21615cb + 8851a24 commit f21ad9b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+892
-568
lines changed

vm/src/builtins/asyncgenerator.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
frame::FrameRef,
77
function::OptionalArg,
88
protocol::PyIterReturn,
9-
types::{Constructor, IterNext, IterNextIterable, Unconstructible},
9+
types::{Constructor, IterNext, IterNextIterable, Representable, Unconstructible},
1010
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
1111
};
1212

@@ -26,7 +26,7 @@ impl PyPayload for PyAsyncGen {
2626
}
2727
}
2828

29-
#[pyclass(with(Constructor))]
29+
#[pyclass(with(Constructor, Representable))]
3030
impl PyAsyncGen {
3131
pub fn as_coro(&self) -> &Coro {
3232
&self.inner
@@ -49,11 +49,6 @@ impl PyAsyncGen {
4949
self.inner.set_name(name)
5050
}
5151

52-
#[pymethod(magic)]
53-
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> String {
54-
zelf.inner.repr(zelf.as_object(), zelf.get_id(), vm)
55-
}
56-
5752
#[pymethod(magic)]
5853
fn aiter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
5954
zelf
@@ -129,6 +124,14 @@ impl PyAsyncGen {
129124
PyGenericAlias::new(cls, args, vm)
130125
}
131126
}
127+
128+
impl Representable for PyAsyncGen {
129+
#[inline]
130+
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
131+
Ok(zelf.inner.repr(zelf.as_object(), zelf.get_id(), vm))
132+
}
133+
}
134+
132135
impl Unconstructible for PyAsyncGen {}
133136

134137
#[pyclass(module = false, name = "async_generator_wrapped_value")]
@@ -264,7 +267,7 @@ impl PyAsyncGenASend {
264267

265268
impl IterNextIterable for PyAsyncGenASend {}
266269
impl IterNext for PyAsyncGenASend {
267-
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
270+
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
268271
PyIterReturn::from_pyresult(zelf.send(vm.ctx.none(), vm), vm)
269272
}
270273
}
@@ -410,7 +413,7 @@ impl PyAsyncGenAThrow {
410413

411414
impl IterNextIterable for PyAsyncGenAThrow {}
412415
impl IterNext for PyAsyncGenAThrow {
413-
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
416+
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
414417
PyIterReturn::from_pyresult(zelf.send(vm.ctx.none(), vm), vm)
415418
}
416419
}

vm/src/builtins/bool.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
function::OptionalArg,
66
identifier,
77
protocol::PyNumberMethods,
8-
types::{AsNumber, Constructor},
8+
types::{AsNumber, Constructor, Representable},
99
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject,
1010
VirtualMachine,
1111
};
@@ -107,18 +107,8 @@ impl Constructor for PyBool {
107107
}
108108
}
109109

110-
#[pyclass(with(Constructor, AsNumber))]
110+
#[pyclass(with(Constructor, AsNumber, Representable))]
111111
impl PyBool {
112-
#[pymethod(magic)]
113-
fn repr(zelf: bool, vm: &VirtualMachine) -> PyStrRef {
114-
if zelf {
115-
vm.ctx.names.True
116-
} else {
117-
vm.ctx.names.False
118-
}
119-
.to_owned()
120-
}
121-
122112
#[pymethod(magic)]
123113
fn format(obj: PyObjectRef, format_spec: PyStrRef, vm: &VirtualMachine) -> PyResult<PyStrRef> {
124114
if format_spec.is_empty() {
@@ -189,6 +179,23 @@ impl AsNumber for PyBool {
189179
}
190180
}
191181

182+
impl Representable for PyBool {
183+
#[inline]
184+
fn slot_repr(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> {
185+
let name = if get_value(zelf.as_object()) {
186+
vm.ctx.names.True
187+
} else {
188+
vm.ctx.names.False
189+
};
190+
Ok(name.to_owned())
191+
}
192+
193+
#[cold]
194+
fn repr_str(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
195+
unreachable!("use slot_repr instead")
196+
}
197+
}
198+
192199
pub(crate) fn init(context: &Context) {
193200
PyBool::extend_class(context, context.types.bool_type);
194201
}

vm/src/builtins/builtin_func.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
builtins::PyBoundMethod,
44
class::PyClassImpl,
55
function::{FuncArgs, IntoPyNativeFunc, PyNativeFunc},
6-
types::{Callable, Constructor, GetDescriptor, Unconstructible},
6+
types::{Callable, Constructor, GetDescriptor, Representable, Unconstructible},
77
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
88
};
99
use std::fmt;
@@ -109,7 +109,7 @@ impl PyBuiltinFunction {
109109
impl Callable for PyBuiltinFunction {
110110
type Args = FuncArgs;
111111
#[inline]
112-
fn call(zelf: &crate::Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
112+
fn call(zelf: &Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
113113
(zelf.value.func)(vm, args)
114114
}
115115
}
@@ -145,10 +145,6 @@ impl PyBuiltinFunction {
145145
fn reduce_ex(&self, _ver: PyObjectRef) -> PyStrRef {
146146
self.name()
147147
}
148-
#[pymethod(magic)]
149-
fn repr(&self) -> String {
150-
format!("<built-in function {}>", self.value.name)
151-
}
152148
#[pygetset(magic)]
153149
fn text_signature(&self) -> Option<String> {
154150
self.value.doc.as_ref().and_then(|doc| {
@@ -157,6 +153,14 @@ impl PyBuiltinFunction {
157153
})
158154
}
159155
}
156+
157+
impl Representable for PyBuiltinFunction {
158+
#[inline]
159+
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
160+
Ok(format!("<built-in function {}>", zelf.value.name))
161+
}
162+
}
163+
160164
impl Unconstructible for PyBuiltinFunction {}
161165

162166
// `PyBuiltinMethod` is similar to both `PyMethodDescrObject` in
@@ -206,7 +210,7 @@ impl GetDescriptor for PyBuiltinMethod {
206210
impl Callable for PyBuiltinMethod {
207211
type Args = FuncArgs;
208212
#[inline]
209-
fn call(zelf: &crate::Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
213+
fn call(zelf: &Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
210214
(zelf.value.func)(vm, args)
211215
}
212216
}
@@ -225,7 +229,10 @@ impl PyBuiltinMethod {
225229
}
226230
}
227231

228-
#[pyclass(with(GetDescriptor, Callable, Constructor), flags(METHOD_DESCR))]
232+
#[pyclass(
233+
with(GetDescriptor, Callable, Constructor, Representable),
234+
flags(METHOD_DESCR)
235+
)]
229236
impl PyBuiltinMethod {
230237
#[pygetset(magic)]
231238
fn name(&self) -> PyStrRef {
@@ -247,14 +254,6 @@ impl PyBuiltinMethod {
247254
})
248255
}
249256
#[pymethod(magic)]
250-
fn repr(&self) -> String {
251-
format!(
252-
"<method '{}' of '{}' objects>",
253-
&self.value.name,
254-
self.class.name()
255-
)
256-
}
257-
#[pymethod(magic)]
258257
fn reduce(
259258
&self,
260259
vm: &VirtualMachine,
@@ -264,6 +263,18 @@ impl PyBuiltinMethod {
264263
(builtins_getattr, (classname, self.value.name.clone()))
265264
}
266265
}
266+
267+
impl Representable for PyBuiltinMethod {
268+
#[inline]
269+
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
270+
Ok(format!(
271+
"<method '{}' of '{}' objects>",
272+
&zelf.value.name,
273+
zelf.class.name()
274+
))
275+
}
276+
}
277+
267278
impl Unconstructible for PyBuiltinMethod {}
268279

269280
pub fn init(context: &Context) {

vm/src/builtins/bytearray.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::{
3131
sliceable::{SequenceIndex, SliceableSequenceMutOp, SliceableSequenceOp},
3232
types::{
3333
AsBuffer, AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Initializer,
34-
IterNext, IterNextIterable, Iterable, PyComparisonOp, Unconstructible,
34+
IterNext, IterNextIterable, Iterable, PyComparisonOp, Representable, Unconstructible,
3535
},
3636
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
3737
VirtualMachine,
@@ -107,7 +107,8 @@ pub(crate) fn init(context: &Context) {
107107
AsMapping,
108108
AsSequence,
109109
AsNumber,
110-
Iterable
110+
Iterable,
111+
Representable
111112
)
112113
)]
113114
impl PyByteArray {
@@ -126,13 +127,6 @@ impl PyByteArray {
126127
self.inner.write()
127128
}
128129

129-
#[pymethod(magic)]
130-
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<String> {
131-
let class = zelf.class();
132-
let class_name = class.name();
133-
zelf.inner().repr(Some(&class_name), vm)
134-
}
135-
136130
#[pymethod(magic)]
137131
fn alloc(&self) -> usize {
138132
self.inner().capacity()
@@ -300,7 +294,7 @@ impl PyByteArray {
300294
}
301295
}
302296

303-
fn irepeat(zelf: &crate::Py<Self>, n: isize, vm: &VirtualMachine) -> PyResult<()> {
297+
fn irepeat(zelf: &Py<Self>, n: isize, vm: &VirtualMachine) -> PyResult<()> {
304298
if n == 1 {
305299
return Ok(());
306300
}
@@ -739,7 +733,7 @@ impl Initializer for PyByteArray {
739733

740734
impl Comparable for PyByteArray {
741735
fn cmp(
742-
zelf: &crate::Py<Self>,
736+
zelf: &Py<Self>,
743737
other: &PyObject,
744738
op: PyComparisonOp,
745739
vm: &VirtualMachine,
@@ -886,6 +880,15 @@ impl Iterable for PyByteArray {
886880
}
887881
}
888882

883+
impl Representable for PyByteArray {
884+
#[inline]
885+
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
886+
let class = zelf.class();
887+
let class_name = class.name();
888+
zelf.inner().repr(Some(&class_name), vm)
889+
}
890+
}
891+
889892
// fn set_value(obj: &PyObject, value: Vec<u8>) {
890893
// obj.borrow_mut().kind = PyObjectPayload::Bytes { value };
891894
// }
@@ -922,11 +925,12 @@ impl PyByteArrayIterator {
922925
.set_state(state, |obj, pos| pos.min(obj.len()), vm)
923926
}
924927
}
928+
925929
impl Unconstructible for PyByteArrayIterator {}
926930

927931
impl IterNextIterable for PyByteArrayIterator {}
928932
impl IterNext for PyByteArrayIterator {
929-
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
933+
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
930934
zelf.internal.lock().next(|bytearray, pos| {
931935
let buf = bytearray.borrow_buf();
932936
Ok(PyIterReturn::from_result(

vm/src/builtins/bytes.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
sliceable::{SequenceIndex, SliceableSequenceOp},
2222
types::{
2323
AsBuffer, AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Hashable,
24-
IterNext, IterNextIterable, Iterable, PyComparisonOp, Unconstructible,
24+
IterNext, IterNextIterable, Iterable, PyComparisonOp, Representable, Unconstructible,
2525
},
2626
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
2727
TryFromBorrowedObject, TryFromObject, VirtualMachine,
@@ -125,15 +125,11 @@ impl PyBytes {
125125
AsBuffer,
126126
Iterable,
127127
Constructor,
128-
AsNumber
128+
AsNumber,
129+
Representable,
129130
)
130131
)]
131132
impl PyBytes {
132-
#[pymethod(magic)]
133-
pub(crate) fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
134-
self.inner.repr(None, vm)
135-
}
136-
137133
#[pymethod(magic)]
138134
#[inline]
139135
pub fn len(&self) -> usize {
@@ -650,14 +646,14 @@ impl AsNumber for PyBytes {
650646

651647
impl Hashable for PyBytes {
652648
#[inline]
653-
fn hash(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
649+
fn hash(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
654650
Ok(zelf.inner.hash(vm))
655651
}
656652
}
657653

658654
impl Comparable for PyBytes {
659655
fn cmp(
660-
zelf: &crate::Py<Self>,
656+
zelf: &Py<Self>,
661657
other: &PyObject,
662658
op: PyComparisonOp,
663659
vm: &VirtualMachine,
@@ -689,6 +685,13 @@ impl Iterable for PyBytes {
689685
}
690686
}
691687

688+
impl Representable for PyBytes {
689+
#[inline]
690+
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
691+
zelf.inner.repr(None, vm)
692+
}
693+
}
694+
692695
#[pyclass(module = false, name = "bytes_iterator")]
693696
#[derive(Debug)]
694697
pub struct PyBytesIterator {
@@ -726,7 +729,7 @@ impl Unconstructible for PyBytesIterator {}
726729

727730
impl IterNextIterable for PyBytesIterator {}
728731
impl IterNext for PyBytesIterator {
729-
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
732+
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
730733
zelf.internal.lock().next(|bytes, pos| {
731734
Ok(PyIterReturn::from_result(
732735
bytes

0 commit comments

Comments
 (0)