Skip to content

Commit a3daca0

Browse files
committed
Clean up slots in operator.
1 parent 2309e59 commit a3daca0

File tree

1 file changed

+58
-64
lines changed

1 file changed

+58
-64
lines changed

vm/src/stdlib/operator.rs

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ mod _operator {
2020
},
2121
utils::Either,
2222
vm::ReprGuard,
23-
IdProtocol, ItemProtocol, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
24-
VirtualMachine,
23+
IdProtocol, ItemProtocol, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
24+
TypeProtocol, VirtualMachine,
2525
};
2626

2727
/// Same as a < b.
@@ -429,29 +429,8 @@ mod _operator {
429429
attrs: Vec<PyStrRef>,
430430
}
431431

432-
#[pyimpl(with(Callable))]
432+
#[pyimpl(with(Callable, Constructor))]
433433
impl PyAttrGetter {
434-
#[pyslot]
435-
fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
436-
let nattr = args.args.len();
437-
// Check we get no keyword and at least one positional.
438-
if !args.kwargs.is_empty() {
439-
return Err(vm.new_type_error("attrgetter() takes no keyword arguments".to_owned()));
440-
}
441-
if nattr == 0 {
442-
return Err(vm.new_type_error("attrgetter expected 1 argument, got 0.".to_owned()));
443-
}
444-
let mut attrs = Vec::with_capacity(nattr);
445-
for o in args.args {
446-
if let Ok(r) = o.try_into_value(vm) {
447-
attrs.push(r);
448-
} else {
449-
return Err(vm.new_type_error("attribute name must be a string".to_owned()));
450-
}
451-
}
452-
PyAttrGetter { attrs }.into_pyresult_with_type(vm, cls)
453-
}
454-
455434
#[pymethod(magic)]
456435
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<String> {
457436
let fmt = if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
@@ -495,13 +474,33 @@ mod _operator {
495474
}
496475
}
497476

477+
impl Constructor for PyAttrGetter {
478+
type Args = FuncArgs;
479+
480+
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
481+
let nattr = args.args.len();
482+
// Check we get no keyword and at least one positional.
483+
if !args.kwargs.is_empty() {
484+
return Err(vm.new_type_error("attrgetter() takes no keyword arguments".to_owned()));
485+
}
486+
if nattr == 0 {
487+
return Err(vm.new_type_error("attrgetter expected 1 argument, got 0.".to_owned()));
488+
}
489+
let mut attrs = Vec::with_capacity(nattr);
490+
for o in args.args {
491+
if let Ok(r) = o.try_into_value(vm) {
492+
attrs.push(r);
493+
} else {
494+
return Err(vm.new_type_error("attribute name must be a string".to_owned()));
495+
}
496+
}
497+
PyAttrGetter { attrs }.into_pyresult_with_type(vm, cls)
498+
}
499+
}
500+
498501
impl Callable for PyAttrGetter {
499502
type Args = PyObjectRef;
500-
fn call(
501-
zelf: &crate::PyObjectView<Self>,
502-
obj: Self::Args,
503-
vm: &VirtualMachine,
504-
) -> PyResult {
503+
fn call(zelf: &PyObjectView<Self>, obj: Self::Args, vm: &VirtualMachine) -> PyResult {
505504
// Handle case where we only have one attribute.
506505
if zelf.attrs.len() == 1 {
507506
return Self::get_single_attr(obj, zelf.attrs[0].as_str(), vm);
@@ -527,20 +526,8 @@ mod _operator {
527526
items: Vec<PyObjectRef>,
528527
}
529528

530-
#[pyimpl(with(Callable))]
529+
#[pyimpl(with(Callable, Constructor))]
531530
impl PyItemGetter {
532-
#[pyslot]
533-
fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
534-
// Check we get no keyword and at least one positional.
535-
if !args.kwargs.is_empty() {
536-
return Err(vm.new_type_error("itemgetter() takes no keyword arguments".to_owned()));
537-
}
538-
if args.args.is_empty() {
539-
return Err(vm.new_type_error("itemgetter expected 1 argument, got 0.".to_owned()));
540-
}
541-
PyItemGetter { items: args.args }.into_pyresult_with_type(vm, cls)
542-
}
543-
544531
#[pymethod(magic)]
545532
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<String> {
546533
let fmt = if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
@@ -561,14 +548,24 @@ mod _operator {
561548
vm.new_pyobj((zelf.clone_class(), items))
562549
}
563550
}
551+
impl Constructor for PyItemGetter {
552+
type Args = FuncArgs;
553+
554+
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
555+
// Check we get no keyword and at least one positional.
556+
if !args.kwargs.is_empty() {
557+
return Err(vm.new_type_error("itemgetter() takes no keyword arguments".to_owned()));
558+
}
559+
if args.args.is_empty() {
560+
return Err(vm.new_type_error("itemgetter expected 1 argument, got 0.".to_owned()));
561+
}
562+
PyItemGetter { items: args.args }.into_pyresult_with_type(vm, cls)
563+
}
564+
}
564565

565566
impl Callable for PyItemGetter {
566567
type Args = PyObjectRef;
567-
fn call(
568-
zelf: &crate::PyObjectView<Self>,
569-
obj: Self::Args,
570-
vm: &VirtualMachine,
571-
) -> PyResult {
568+
fn call(zelf: &PyObjectView<Self>, obj: Self::Args, vm: &VirtualMachine) -> PyResult {
572569
// Handle case where we only have one attribute.
573570
if zelf.items.len() == 1 {
574571
return obj.get_item(zelf.items[0].clone(), vm);
@@ -596,18 +593,6 @@ mod _operator {
596593
args: FuncArgs,
597594
}
598595

599-
impl Constructor for PyMethodCaller {
600-
type Args = (PyObjectRef, FuncArgs);
601-
602-
fn py_new(cls: PyTypeRef, (name, args): Self::Args, vm: &VirtualMachine) -> PyResult {
603-
if let Ok(name) = name.try_into_value(vm) {
604-
PyMethodCaller { name, args }.into_pyresult_with_type(vm, cls)
605-
} else {
606-
Err(vm.new_type_error("method name must be a string".to_owned()))
607-
}
608-
}
609-
}
610-
611596
#[pyimpl(with(Callable, Constructor))]
612597
impl PyMethodCaller {
613598
#[pymethod(magic)]
@@ -662,14 +647,23 @@ mod _operator {
662647
}
663648
}
664649

650+
impl Constructor for PyMethodCaller {
651+
type Args = (PyObjectRef, FuncArgs);
652+
653+
fn py_new(cls: PyTypeRef, (name, args): Self::Args, vm: &VirtualMachine) -> PyResult {
654+
if let Ok(name) = name.try_into_value(vm) {
655+
PyMethodCaller { name, args }.into_pyresult_with_type(vm, cls)
656+
} else {
657+
Err(vm.new_type_error("method name must be a string".to_owned()))
658+
}
659+
}
660+
}
661+
665662
impl Callable for PyMethodCaller {
666663
type Args = PyObjectRef;
664+
667665
#[inline]
668-
fn call(
669-
zelf: &crate::PyObjectView<Self>,
670-
obj: Self::Args,
671-
vm: &VirtualMachine,
672-
) -> PyResult {
666+
fn call(zelf: &PyObjectView<Self>, obj: Self::Args, vm: &VirtualMachine) -> PyResult {
673667
vm.call_method(&obj, zelf.name.as_str(), zelf.args.clone())
674668
}
675669
}

0 commit comments

Comments
 (0)