@@ -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} ;
@@ -1817,77 +1816,6 @@ impl VirtualMachine {
18171816 . invoke ( ( ) , self )
18181817 }
18191818
1820- // Perform a comparison, raising TypeError when the requested comparison
1821- // operator is not supported.
1822- // see: CPython PyObject_RichCompare
1823- fn _cmp (
1824- & self ,
1825- v : & PyObjectRef ,
1826- w : & PyObjectRef ,
1827- op : PyComparisonOp ,
1828- ) -> PyResult < Either < PyObjectRef , bool > > {
1829- let swapped = op. swapped ( ) ;
1830- let call_cmp = |obj : & PyObjectRef , other, op| {
1831- let cmp = obj
1832- . class ( )
1833- . mro_find_map ( |cls| cls. slots . richcompare . load ( ) )
1834- . unwrap ( ) ;
1835- Ok ( match cmp ( obj, other, op, self ) ? {
1836- Either :: A ( obj) => PyArithmeticValue :: from_object ( self , obj) . map ( Either :: A ) ,
1837- Either :: B ( arithmetic) => arithmetic. map ( Either :: B ) ,
1838- } )
1839- } ;
1840-
1841- let mut checked_reverse_op = false ;
1842- let is_strict_subclass = {
1843- let v_class = v. class ( ) ;
1844- let w_class = w. class ( ) ;
1845- !v_class. is ( & w_class) && w_class. issubclass ( & v_class)
1846- } ;
1847- if is_strict_subclass {
1848- let res = self . with_recursion ( "in comparison" , || call_cmp ( w, v, swapped) ) ?;
1849- checked_reverse_op = true ;
1850- if let PyArithmeticValue :: Implemented ( x) = res {
1851- return Ok ( x) ;
1852- }
1853- }
1854- if let PyArithmeticValue :: Implemented ( x) =
1855- self . with_recursion ( "in comparison" , || call_cmp ( v, w, op) ) ?
1856- {
1857- return Ok ( x) ;
1858- }
1859- if !checked_reverse_op {
1860- let res = self . with_recursion ( "in comparison" , || call_cmp ( w, v, swapped) ) ?;
1861- if let PyArithmeticValue :: Implemented ( x) = res {
1862- return Ok ( x) ;
1863- }
1864- }
1865- match op {
1866- PyComparisonOp :: Eq => Ok ( Either :: B ( v. is ( & w) ) ) ,
1867- PyComparisonOp :: Ne => Ok ( Either :: B ( !v. is ( & w) ) ) ,
1868- _ => Err ( self . new_unsupported_binop_error ( v, w, op. operator_token ( ) ) ) ,
1869- }
1870- }
1871-
1872- pub fn bool_cmp ( & self , a : & PyObjectRef , b : & PyObjectRef , op : PyComparisonOp ) -> PyResult < bool > {
1873- match self . _cmp ( a, b, op) ? {
1874- Either :: A ( obj) => obj. try_to_bool ( self ) ,
1875- Either :: B ( b) => Ok ( b) ,
1876- }
1877- }
1878-
1879- pub fn obj_cmp ( & self , a : PyObjectRef , b : PyObjectRef , op : PyComparisonOp ) -> PyResult {
1880- self . _cmp ( & a, & b, op) . map ( |res| res. into_pyobject ( self ) )
1881- }
1882-
1883- pub fn _hash ( & self , obj : & PyObjectRef ) -> PyResult < rustpython_common:: hash:: PyHash > {
1884- let hash = obj
1885- . class ( )
1886- . mro_find_map ( |cls| cls. slots . hash . load ( ) )
1887- . unwrap ( ) ; // hash always exist
1888- hash ( obj, self )
1889- }
1890-
18911819 pub fn obj_len_opt ( & self , obj : & PyObjectRef ) -> Option < PyResult < usize > > {
18921820 self . get_special_method ( obj. clone ( ) , "__len__" )
18931821 . map ( Result :: ok)
@@ -2067,7 +1995,7 @@ impl VirtualMachine {
20671995 }
20681996
20691997 pub fn bool_eq ( & self , a : & PyObjectRef , b : & PyObjectRef ) -> PyResult < bool > {
2070- self . bool_cmp ( a , b, PyComparisonOp :: Eq )
1998+ a . rich_compare_bool ( b, PyComparisonOp :: Eq , self )
20711999 }
20722000
20732001 pub fn identical_or_equal ( & self , a : & PyObjectRef , b : & PyObjectRef ) -> PyResult < bool > {
@@ -2079,7 +2007,7 @@ impl VirtualMachine {
20792007 }
20802008
20812009 pub fn bool_seq_lt ( & self , a : & PyObjectRef , b : & PyObjectRef ) -> PyResult < Option < bool > > {
2082- let value = if self . bool_cmp ( a , b, PyComparisonOp :: Lt ) ? {
2010+ let value = if a . rich_compare_bool ( b, PyComparisonOp :: Lt , self ) ? {
20832011 Some ( true )
20842012 } else if !self . bool_eq ( a, b) ? {
20852013 Some ( false )
@@ -2090,7 +2018,7 @@ impl VirtualMachine {
20902018 }
20912019
20922020 pub fn bool_seq_gt ( & self , a : & PyObjectRef , b : & PyObjectRef ) -> PyResult < Option < bool > > {
2093- let value = if self . bool_cmp ( a , b, PyComparisonOp :: Gt ) ? {
2021+ let value = if a . rich_compare_bool ( b, PyComparisonOp :: Gt , self ) ? {
20942022 Some ( true )
20952023 } else if !self . bool_eq ( a, b) ? {
20962024 Some ( false )
0 commit comments