@@ -27,7 +27,6 @@ use crate::{
2727 signal:: NSIG ,
2828 stdlib,
2929 types:: PyComparisonOp ,
30- utils:: Either ,
3130 IdProtocol , ItemProtocol , PyArithmeticValue , PyContext , PyLease , PyMethod , PyObject ,
3231 PyObjectRef , PyObjectWrap , PyRef , PyRefExact , PyResult , PyValue , TryFromObject , TypeProtocol ,
3332} ;
@@ -1786,69 +1785,6 @@ impl VirtualMachine {
17861785 . invoke ( ( ) , self )
17871786 }
17881787
1789- // Perform a comparison, raising TypeError when the requested comparison
1790- // operator is not supported.
1791- // see: CPython PyObject_RichCompare
1792- fn _cmp (
1793- & self ,
1794- v : & PyObjectRef ,
1795- w : & PyObjectRef ,
1796- op : PyComparisonOp ,
1797- ) -> PyResult < Either < PyObjectRef , bool > > {
1798- let swapped = op. swapped ( ) ;
1799- let call_cmp = |obj : & PyObjectRef , other, op| {
1800- let cmp = obj
1801- . class ( )
1802- . mro_find_map ( |cls| cls. slots . richcompare . load ( ) )
1803- . unwrap ( ) ;
1804- Ok ( match cmp ( obj, other, op, self ) ? {
1805- Either :: A ( obj) => PyArithmeticValue :: from_object ( self , obj) . map ( Either :: A ) ,
1806- Either :: B ( arithmetic) => arithmetic. map ( Either :: B ) ,
1807- } )
1808- } ;
1809-
1810- let mut checked_reverse_op = false ;
1811- let is_strict_subclass = {
1812- let v_class = v. class ( ) ;
1813- let w_class = w. class ( ) ;
1814- !v_class. is ( & w_class) && w_class. issubclass ( & v_class)
1815- } ;
1816- if is_strict_subclass {
1817- let res = self . with_recursion ( "in comparison" , || call_cmp ( w, v, swapped) ) ?;
1818- checked_reverse_op = true ;
1819- if let PyArithmeticValue :: Implemented ( x) = res {
1820- return Ok ( x) ;
1821- }
1822- }
1823- if let PyArithmeticValue :: Implemented ( x) =
1824- self . with_recursion ( "in comparison" , || call_cmp ( v, w, op) ) ?
1825- {
1826- return Ok ( x) ;
1827- }
1828- if !checked_reverse_op {
1829- let res = self . with_recursion ( "in comparison" , || call_cmp ( w, v, swapped) ) ?;
1830- if let PyArithmeticValue :: Implemented ( x) = res {
1831- return Ok ( x) ;
1832- }
1833- }
1834- match op {
1835- PyComparisonOp :: Eq => Ok ( Either :: B ( v. is ( & w) ) ) ,
1836- PyComparisonOp :: Ne => Ok ( Either :: B ( !v. is ( & w) ) ) ,
1837- _ => Err ( self . new_unsupported_binop_error ( v, w, op. operator_token ( ) ) ) ,
1838- }
1839- }
1840-
1841- pub fn bool_cmp ( & self , a : & PyObjectRef , b : & PyObjectRef , op : PyComparisonOp ) -> PyResult < bool > {
1842- match self . _cmp ( a, b, op) ? {
1843- Either :: A ( obj) => obj. try_to_bool ( self ) ,
1844- Either :: B ( b) => Ok ( b) ,
1845- }
1846- }
1847-
1848- pub fn obj_cmp ( & self , a : PyObjectRef , b : PyObjectRef , op : PyComparisonOp ) -> PyResult {
1849- self . _cmp ( & a, & b, op) . map ( |res| res. into_pyobject ( self ) )
1850- }
1851-
18521788 pub fn obj_len_opt ( & self , obj : & PyObjectRef ) -> Option < PyResult < usize > > {
18531789 self . get_special_method ( obj. clone ( ) , "__len__" )
18541790 . map ( Result :: ok)
@@ -2019,7 +1955,7 @@ impl VirtualMachine {
20191955 }
20201956
20211957 pub fn bool_eq ( & self , a : & PyObjectRef , b : & PyObjectRef ) -> PyResult < bool > {
2022- self . bool_cmp ( a , b, PyComparisonOp :: Eq )
1958+ a . rich_compare_bool ( b, PyComparisonOp :: Eq , self )
20231959 }
20241960
20251961 pub fn identical_or_equal ( & self , a : & PyObjectRef , b : & PyObjectRef ) -> PyResult < bool > {
@@ -2031,7 +1967,7 @@ impl VirtualMachine {
20311967 }
20321968
20331969 pub fn bool_seq_lt ( & self , a : & PyObjectRef , b : & PyObjectRef ) -> PyResult < Option < bool > > {
2034- let value = if self . bool_cmp ( a , b, PyComparisonOp :: Lt ) ? {
1970+ let value = if a . rich_compare_bool ( b, PyComparisonOp :: Lt , self ) ? {
20351971 Some ( true )
20361972 } else if !self . bool_eq ( a, b) ? {
20371973 Some ( false )
@@ -2042,7 +1978,7 @@ impl VirtualMachine {
20421978 }
20431979
20441980 pub fn bool_seq_gt ( & self , a : & PyObjectRef , b : & PyObjectRef ) -> PyResult < Option < bool > > {
2045- let value = if self . bool_cmp ( a , b, PyComparisonOp :: Gt ) ? {
1981+ let value = if a . rich_compare_bool ( b, PyComparisonOp :: Gt , self ) ? {
20461982 Some ( true )
20471983 } else if !self . bool_eq ( a, b) ? {
20481984 Some ( false )
0 commit comments