@@ -340,7 +340,6 @@ fn const_hash(ctx: &Context, obj: &PyObject) -> crate::common::hash::PyHash {
340340fn const_eq ( a : & PyObject , b : & PyObject ) -> bool {
341341 use crate :: builtins:: { PyBool , PyBytes , PyComplex , PyFloat , PyInt , PyStr , PyTuple } ;
342342 use crate :: common:: float_ops;
343- use num_traits:: ToPrimitive ;
344343
345344 if a. is ( b) {
346345 return true ;
@@ -365,26 +364,25 @@ fn const_eq(a: &PyObject, b: &PyObject) -> bool {
365364 if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyFloat > ( ) , b. downcast_ref :: < PyFloat > ( ) ) {
366365 return x. to_f64 ( ) == y. to_f64 ( ) ;
367366 }
368- // Complex vs. complex/int/float/bool - matches `Comparable for PyComplex`, which converts
369- // the other side to `f64` too (imprecise for huge ints, same as real Python).
367+ // Complex vs. complex/int/float/bool.
370368 let a_complex = a. downcast_ref :: < PyComplex > ( ) . map ( |c| c. to_complex64 ( ) ) ;
371369 let b_complex = b. downcast_ref :: < PyComplex > ( ) . map ( |c| c. to_complex64 ( ) ) ;
372370 if let ( Some ( x) , Some ( y) ) = ( a_complex, b_complex) {
373371 return x == y;
374372 }
375373 if let Some ( c) = a_complex. or ( b_complex) {
376374 let other = if a_complex. is_some ( ) { b } else { a } ;
377- let other_f64 = other
378- . downcast_ref :: < PyFloat > ( )
379- . map ( |f| f . to_f64 ( ) )
380- . or_else ( || {
381- other
382- . downcast_ref :: < PyBool > ( )
383- . map ( |v| v . 0 . as_bigint ( ) )
384- . or_else ( || other . downcast_ref :: < PyInt > ( ) . map ( |i| i . as_bigint ( ) ) )
385- . and_then ( |i| i . to_f64 ( ) )
386- } ) ;
387- return other_f64 . is_some_and ( |f| c . im == 0.0 && c . re == f ) ;
375+ if c . im != 0.0 {
376+ return false ;
377+ }
378+ if let Some ( f ) = other . downcast_ref :: < PyFloat > ( ) {
379+ return c . re == f . to_f64 ( ) ;
380+ }
381+ let other_int = other
382+ . downcast_ref :: < PyBool > ( )
383+ . map ( |v| v . 0 . as_bigint ( ) )
384+ . or_else ( || other . downcast_ref :: < PyInt > ( ) . map ( |i| i . as_bigint ( ) ) ) ;
385+ return other_int . is_some_and ( |i| float_ops :: eq_int ( c . re , i ) ) ;
388386 }
389387 if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyStr > ( ) , b. downcast_ref :: < PyStr > ( ) ) {
390388 return x. as_bytes ( ) == y. as_bytes ( ) ;
@@ -1900,6 +1898,15 @@ assert len(w) == 2
19001898v = frozenset({1, 1j, (1+0j), 1.0})
19011899assert len(v) == 2
19021900assert 1 in v and 1j in v and (1+0j) in v and 1.0 in v
1901+
1902+ # huge int vs. a same-magnitude complex must compare exactly, not via lossy f64 rounding:
1903+ # 2**114 + (2**61 - 1) and (2**114+0j) round to the same f64, but aren't equal. A `for` loop
1904+ # (unlike a plain assignment) iterates the folded frozenset constant directly instead of
1905+ # rebuilding it through set.update()'s real-VM equality, so this exercises `const_eq` itself.
1906+ u_count = 0
1907+ for _e in {20769187434139312819964994530574335, (20769187434139310514121985316880384+0j), 42}:
1908+ u_count += 1
1909+ assert u_count == 3
19031910" ;
19041911 let opts = vm. compile_opts ( ) ;
19051912 let code =
0 commit comments