Skip to content

Commit f839e6c

Browse files
committed
more #[pyclass(Py, PyRef)]
1 parent 52aad1e commit f839e6c

4 files changed

Lines changed: 119 additions & 110 deletions

File tree

‎vm/src/builtins/complex.rs‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,9 @@ impl PyComplex {
231231

232232
#[pyclass(
233233
flags(BASETYPE),
234-
with(Comparable, Hashable, Constructor, AsNumber, Representable)
234+
with(PyRef, Comparable, Hashable, Constructor, AsNumber, Representable)
235235
)]
236236
impl PyComplex {
237-
#[pymethod(magic)]
238-
fn complex(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<PyComplex> {
239-
if zelf.is(vm.ctx.types.complex_type) {
240-
zelf
241-
} else {
242-
PyComplex::from(zelf.value).into_ref(&vm.ctx)
243-
}
244-
}
245-
246237
#[pygetset]
247238
fn real(&self) -> f64 {
248239
self.value.re
@@ -387,6 +378,18 @@ impl PyComplex {
387378
}
388379
}
389380

381+
#[pyclass]
382+
impl PyRef<PyComplex> {
383+
#[pymethod(magic)]
384+
fn complex(self, vm: &VirtualMachine) -> PyRef<PyComplex> {
385+
if self.is(vm.ctx.types.complex_type) {
386+
self
387+
} else {
388+
PyComplex::from(self.value).into_ref(&vm.ctx)
389+
}
390+
}
391+
}
392+
390393
impl Comparable for PyComplex {
391394
fn cmp(
392395
zelf: &Py<Self>,

‎vm/src/builtins/coroutine.rs‎

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl PyPayload for PyCoroutine {
2222
}
2323
}
2424

25-
#[pyclass(with(Unconstructible, IterNext, Representable))]
25+
#[pyclass(with(Py, Unconstructible, IterNext, Representable))]
2626
impl PyCoroutine {
2727
pub fn as_coro(&self) -> &Coro {
2828
&self.inner
@@ -44,33 +44,6 @@ impl PyCoroutine {
4444
self.inner.set_name(name)
4545
}
4646

47-
#[pymethod]
48-
fn send(zelf: &Py<Self>, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
49-
zelf.inner.send(zelf.as_object(), value, vm)
50-
}
51-
52-
#[pymethod]
53-
fn throw(
54-
zelf: &Py<Self>,
55-
exc_type: PyObjectRef,
56-
exc_val: OptionalArg,
57-
exc_tb: OptionalArg,
58-
vm: &VirtualMachine,
59-
) -> PyResult<PyIterReturn> {
60-
zelf.inner.throw(
61-
zelf.as_object(),
62-
exc_type,
63-
exc_val.unwrap_or_none(vm),
64-
exc_tb.unwrap_or_none(vm),
65-
vm,
66-
)
67-
}
68-
69-
#[pymethod]
70-
fn close(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<()> {
71-
zelf.inner.close(zelf.as_object(), vm)
72-
}
73-
7447
#[pymethod(name = "__await__")]
7548
fn r#await(zelf: PyRef<Self>) -> PyCoroutineWrapper {
7649
PyCoroutineWrapper { coro: zelf }
@@ -99,6 +72,37 @@ impl PyCoroutine {
9972
None
10073
}
10174
}
75+
76+
#[pyclass]
77+
impl Py<PyCoroutine> {
78+
#[pymethod]
79+
fn send(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
80+
self.inner.send(self.as_object(), value, vm)
81+
}
82+
83+
#[pymethod]
84+
fn throw(
85+
&self,
86+
exc_type: PyObjectRef,
87+
exc_val: OptionalArg,
88+
exc_tb: OptionalArg,
89+
vm: &VirtualMachine,
90+
) -> PyResult<PyIterReturn> {
91+
self.inner.throw(
92+
self.as_object(),
93+
exc_type,
94+
exc_val.unwrap_or_none(vm),
95+
exc_tb.unwrap_or_none(vm),
96+
vm,
97+
)
98+
}
99+
100+
#[pymethod]
101+
fn close(&self, vm: &VirtualMachine) -> PyResult<()> {
102+
self.inner.close(self.as_object(), vm)
103+
}
104+
}
105+
102106
impl Unconstructible for PyCoroutine {}
103107

104108
impl Representable for PyCoroutine {
@@ -111,7 +115,7 @@ impl Representable for PyCoroutine {
111115
impl SelfIter for PyCoroutine {}
112116
impl IterNext for PyCoroutine {
113117
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
114-
Self::send(zelf, vm.ctx.none(), vm)
118+
zelf.send(vm.ctx.none(), vm)
115119
}
116120
}
117121

@@ -132,7 +136,7 @@ impl PyPayload for PyCoroutineWrapper {
132136
impl PyCoroutineWrapper {
133137
#[pymethod]
134138
fn send(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
135-
PyCoroutine::send(&self.coro, val, vm)
139+
self.coro.send(val, vm)
136140
}
137141

138142
#[pymethod]
@@ -143,7 +147,7 @@ impl PyCoroutineWrapper {
143147
exc_tb: OptionalArg,
144148
vm: &VirtualMachine,
145149
) -> PyResult<PyIterReturn> {
146-
PyCoroutine::throw(&self.coro, exc_type, exc_val, exc_tb, vm)
150+
self.coro.throw(exc_type, exc_val, exc_tb, vm)
147151
}
148152
}
149153

‎vm/src/builtins/dict.rs‎

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -109,43 +109,6 @@ impl PyDict {
109109
Ok(())
110110
}
111111

112-
fn inner_cmp(
113-
zelf: &Py<Self>,
114-
other: &Py<PyDict>,
115-
op: PyComparisonOp,
116-
item: bool,
117-
vm: &VirtualMachine,
118-
) -> PyResult<PyComparisonValue> {
119-
if op == PyComparisonOp::Ne {
120-
return Self::inner_cmp(zelf, other, PyComparisonOp::Eq, item, vm)
121-
.map(|x| x.map(|eq| !eq));
122-
}
123-
if !op.eval_ord(zelf.len().cmp(&other.len())) {
124-
return Ok(Implemented(false));
125-
}
126-
let (superset, subset) = if zelf.len() < other.len() {
127-
(other, zelf)
128-
} else {
129-
(zelf, other)
130-
};
131-
for (k, v1) in subset {
132-
match superset.get_item_opt(&*k, vm)? {
133-
Some(v2) => {
134-
if v1.is(&v2) {
135-
continue;
136-
}
137-
if item && !vm.bool_eq(&v1, &v2)? {
138-
return Ok(Implemented(false));
139-
}
140-
}
141-
None => {
142-
return Ok(Implemented(false));
143-
}
144-
}
145-
}
146-
Ok(Implemented(true))
147-
}
148-
149112
pub fn is_empty(&self) -> bool {
150113
self.entries.len() == 0
151114
}
@@ -317,23 +280,6 @@ impl PyDict {
317280
Ok(())
318281
}
319282

320-
#[pymethod(magic)]
321-
fn ior(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
322-
zelf.merge_object(other, vm)?;
323-
Ok(zelf)
324-
}
325-
326-
#[pymethod(magic)]
327-
fn ror(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
328-
let dicted: Result<PyDictRef, _> = other.downcast();
329-
if let Ok(other) = dicted {
330-
let other_cp = other.copy();
331-
other_cp.merge_dict(zelf, vm)?;
332-
return Ok(other_cp.into_pyobject(vm));
333-
}
334-
Ok(vm.ctx.not_implemented())
335-
}
336-
337283
#[pymethod(magic)]
338284
fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
339285
let dicted: Result<PyDictRef, _> = other.downcast();
@@ -378,6 +324,43 @@ impl PyDict {
378324

379325
#[pyclass]
380326
impl Py<PyDict> {
327+
fn inner_cmp(
328+
&self,
329+
other: &Py<PyDict>,
330+
op: PyComparisonOp,
331+
item: bool,
332+
vm: &VirtualMachine,
333+
) -> PyResult<PyComparisonValue> {
334+
if op == PyComparisonOp::Ne {
335+
return Self::inner_cmp(self, other, PyComparisonOp::Eq, item, vm)
336+
.map(|x| x.map(|eq| !eq));
337+
}
338+
if !op.eval_ord(self.len().cmp(&other.len())) {
339+
return Ok(Implemented(false));
340+
}
341+
let (superset, subset) = if self.len() < other.len() {
342+
(other, self)
343+
} else {
344+
(self, other)
345+
};
346+
for (k, v1) in subset {
347+
match superset.get_item_opt(&*k, vm)? {
348+
Some(v2) => {
349+
if v1.is(&v2) {
350+
continue;
351+
}
352+
if item && !vm.bool_eq(&v1, &v2)? {
353+
return Ok(Implemented(false));
354+
}
355+
}
356+
None => {
357+
return Ok(Implemented(false));
358+
}
359+
}
360+
}
361+
Ok(Implemented(true))
362+
}
363+
381364
#[pymethod(magic)]
382365
#[cfg_attr(feature = "flame-it", flame("PyDictRef"))]
383366
fn getitem(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult {
@@ -406,6 +389,23 @@ impl PyRef<PyDict> {
406389
fn reversed(self) -> PyDictReverseKeyIterator {
407390
PyDictReverseKeyIterator::new(self)
408391
}
392+
393+
#[pymethod(magic)]
394+
fn ior(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
395+
self.merge_object(other, vm)?;
396+
Ok(self)
397+
}
398+
399+
#[pymethod(magic)]
400+
fn ror(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
401+
let dicted: Result<PyDictRef, _> = other.downcast();
402+
if let Ok(other) = dicted {
403+
let other_cp = other.copy();
404+
other_cp.merge_dict(self, vm)?;
405+
return Ok(other_cp.into_pyobject(vm));
406+
}
407+
Ok(vm.ctx.not_implemented())
408+
}
409409
}
410410

411411
impl DefaultConstructor for PyDict {}
@@ -466,7 +466,7 @@ impl AsNumber for PyDict {
466466
}),
467467
inplace_or: Some(|a, b, vm| {
468468
if let Some(a) = a.downcast_ref::<PyDict>() {
469-
PyDict::ior(a.to_owned(), b.to_pyobject(vm), vm).map(|d| d.into())
469+
a.to_owned().ior(b.to_pyobject(vm), vm).map(|d| d.into())
470470
} else {
471471
Ok(vm.ctx.not_implemented())
472472
}
@@ -486,7 +486,7 @@ impl Comparable for PyDict {
486486
) -> PyResult<PyComparisonValue> {
487487
op.eq_only(|| {
488488
let other = class_or_notimplemented!(Self, other);
489-
Self::inner_cmp(zelf, other, PyComparisonOp::Eq, true, vm)
489+
zelf.inner_cmp(other, PyComparisonOp::Eq, true, vm)
490490
})
491491
}
492492
}
@@ -1066,8 +1066,7 @@ trait ViewSetOps: DictView {
10661066
) -> PyResult<PyComparisonValue> {
10671067
match_class!(match other {
10681068
ref dictview @ Self => {
1069-
return PyDict::inner_cmp(
1070-
zelf.dict(),
1069+
return zelf.dict().inner_cmp(
10711070
dictview.dict(),
10721071
op,
10731072
!zelf.class().is(vm.ctx.types.dict_keys_type),

‎vm/src/exceptions.rs‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ impl PyBaseException {
441441
}
442442

443443
#[pyclass(
444-
with(Constructor, Initializer, Representable),
444+
with(PyRef, Constructor, Initializer, Representable),
445445
flags(BASETYPE, HAS_DICT)
446446
)]
447447
impl PyBaseException {
@@ -499,12 +499,6 @@ impl PyBaseException {
499499
self.suppress_context.store(suppress_context);
500500
}
501501

502-
#[pymethod]
503-
fn with_traceback(zelf: PyRef<Self>, tb: Option<PyTracebackRef>) -> PyResult<PyRef<Self>> {
504-
*zelf.traceback.write() = tb;
505-
Ok(zelf)
506-
}
507-
508502
#[pymethod(magic)]
509503
pub(super) fn str(&self, vm: &VirtualMachine) -> PyStrRef {
510504
let str_args = vm.exception_args_as_string(self.args(), true);
@@ -514,13 +508,22 @@ impl PyBaseException {
514508
Err(i) => PyStr::from(format!("({})", i.format(", "))).into_ref(&vm.ctx),
515509
}
516510
}
511+
}
512+
513+
#[pyclass]
514+
impl PyRef<PyBaseException> {
515+
#[pymethod]
516+
fn with_traceback(self, tb: Option<PyTracebackRef>) -> PyResult<Self> {
517+
*self.traceback.write() = tb;
518+
Ok(self)
519+
}
517520

518521
#[pymethod(magic)]
519-
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyTupleRef {
520-
if let Some(dict) = zelf.as_object().dict().filter(|x| !x.is_empty()) {
521-
vm.new_tuple((zelf.class().to_owned(), zelf.args(), dict))
522+
fn reduce(self, vm: &VirtualMachine) -> PyTupleRef {
523+
if let Some(dict) = self.as_object().dict().filter(|x| !x.is_empty()) {
524+
vm.new_tuple((self.class().to_owned(), self.args(), dict))
522525
} else {
523-
vm.new_tuple((zelf.class().to_owned(), zelf.args()))
526+
vm.new_tuple((self.class().to_owned(), self.args()))
524527
}
525528
}
526529
}

0 commit comments

Comments
 (0)