@@ -20,8 +20,8 @@ mod _operator {
20
20
} ,
21
21
utils:: Either ,
22
22
vm:: ReprGuard ,
23
- IdProtocol , ItemProtocol , PyObjectRef , PyRef , PyResult , PyValue , TypeProtocol ,
24
- VirtualMachine ,
23
+ IdProtocol , ItemProtocol , PyObjectRef , PyObjectView , PyRef , PyResult , PyValue ,
24
+ TypeProtocol , VirtualMachine ,
25
25
} ;
26
26
27
27
/// Same as a < b.
@@ -429,29 +429,8 @@ mod _operator {
429
429
attrs : Vec < PyStrRef > ,
430
430
}
431
431
432
- #[ pyimpl( with( Callable ) ) ]
432
+ #[ pyimpl( with( Callable , Constructor ) ) ]
433
433
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
-
455
434
#[ pymethod( magic) ]
456
435
fn repr ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyResult < String > {
457
436
let fmt = if let Some ( _guard) = ReprGuard :: enter ( vm, zelf. as_object ( ) ) {
@@ -495,13 +474,33 @@ mod _operator {
495
474
}
496
475
}
497
476
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
+
498
501
impl Callable for PyAttrGetter {
499
502
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 {
505
504
// Handle case where we only have one attribute.
506
505
if zelf. attrs . len ( ) == 1 {
507
506
return Self :: get_single_attr ( obj, zelf. attrs [ 0 ] . as_str ( ) , vm) ;
@@ -527,20 +526,8 @@ mod _operator {
527
526
items : Vec < PyObjectRef > ,
528
527
}
529
528
530
- #[ pyimpl( with( Callable ) ) ]
529
+ #[ pyimpl( with( Callable , Constructor ) ) ]
531
530
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
-
544
531
#[ pymethod( magic) ]
545
532
fn repr ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyResult < String > {
546
533
let fmt = if let Some ( _guard) = ReprGuard :: enter ( vm, zelf. as_object ( ) ) {
@@ -561,14 +548,24 @@ mod _operator {
561
548
vm. new_pyobj ( ( zelf. clone_class ( ) , items) )
562
549
}
563
550
}
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
+ }
564
565
565
566
impl Callable for PyItemGetter {
566
567
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 {
572
569
// Handle case where we only have one attribute.
573
570
if zelf. items . len ( ) == 1 {
574
571
return obj. get_item ( zelf. items [ 0 ] . clone ( ) , vm) ;
@@ -596,18 +593,6 @@ mod _operator {
596
593
args : FuncArgs ,
597
594
}
598
595
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
-
611
596
#[ pyimpl( with( Callable , Constructor ) ) ]
612
597
impl PyMethodCaller {
613
598
#[ pymethod( magic) ]
@@ -662,14 +647,23 @@ mod _operator {
662
647
}
663
648
}
664
649
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
+
665
662
impl Callable for PyMethodCaller {
666
663
type Args = PyObjectRef ;
664
+
667
665
#[ 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 {
673
667
vm. call_method ( & obj, zelf. name . as_str ( ) , zelf. args . clone ( ) )
674
668
}
675
669
}
0 commit comments