@@ -286,7 +286,7 @@ fn is_name_chars(value: &crate::common::wtf8::Wtf8) -> bool {
286286/// real `Hashable` impl, notably matching across the numeric tower (`hash(1) == hash(1.0)`),
287287/// since `1` and `1.0` must land in the same frozenset-constant bucket.
288288fn const_hash ( ctx : & Context , obj : & PyObject ) -> crate :: common:: hash:: PyHash {
289- use crate :: builtins:: { PyBool , PyBytes , PyComplex , PyFloat , PyInt , PyStr , PyTuple } ;
289+ use crate :: builtins:: { PyBool , PyBytes , PyComplex , PyFloat , PyInt , PySlice , PyStr , PyTuple } ;
290290 use crate :: common:: hash;
291291
292292 if let Some ( b) = obj. downcast_ref :: < PyBool > ( ) {
@@ -310,6 +310,15 @@ fn const_hash(ctx: &Context, obj: &PyObject) -> crate::common::hash::PyHash {
310310 crate :: vm:: init_hash_secret ( None ) . hash_bytes ( s. as_bytes ( ) )
311311 } else if let Some ( b) = obj. downcast_ref :: < PyBytes > ( ) {
312312 crate :: vm:: init_hash_secret ( None ) . hash_bytes ( b. as_bytes ( ) )
313+ } else if let Some ( slice) = obj. downcast_ref :: < PySlice > ( ) {
314+ match slice. hash_with ( ctx, |part| {
315+ Ok :: < _ , core:: convert:: Infallible > ( const_hash ( ctx, part) )
316+ } ) {
317+ Ok ( h) => h,
318+ Err ( never) => match never { } ,
319+ }
320+ } else if let Some ( code) = obj. downcast_ref :: < PyCode > ( ) {
321+ const_hash ( ctx, code. hash_attributes ( ctx) . as_object ( ) )
313322 } else if let Some ( t) = obj. downcast_ref :: < PyTuple > ( ) {
314323 let hashes = t
315324 . as_slice ( )
@@ -338,8 +347,8 @@ fn const_hash(ctx: &Context, obj: &PyObject) -> crate::common::hash::PyHash {
338347/// Structural equality for compile-time constant objects, matching Python equality - including
339348/// numeric-tower cross-type equality (`1 == 1.0 == True`) - without a `VirtualMachine`. Safe
340349/// because none of these types can have a custom `__eq__`.
341- fn const_eq ( a : & PyObject , b : & PyObject ) -> bool {
342- use crate :: builtins:: { PyBool , PyBytes , PyComplex , PyFloat , PyInt , PyStr , PyTuple } ;
350+ fn const_eq ( ctx : & Context , a : & PyObject , b : & PyObject ) -> bool {
351+ use crate :: builtins:: { PyBool , PyBytes , PyComplex , PyFloat , PyInt , PySlice , PyStr , PyTuple } ;
343352 use crate :: common:: float_ops;
344353
345354 if a. is ( b) {
@@ -391,19 +400,38 @@ fn const_eq(a: &PyObject, b: &PyObject) -> bool {
391400 if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyBytes > ( ) , b. downcast_ref :: < PyBytes > ( ) ) {
392401 return x. as_bytes ( ) == y. as_bytes ( ) ;
393402 }
403+ if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PySlice > ( ) , b. downcast_ref :: < PySlice > ( ) ) {
404+ return x
405+ . parts ( ctx)
406+ . into_iter ( )
407+ . zip ( y. parts ( ctx) )
408+ . all ( |( x, y) | const_eq ( ctx, x, y) ) ;
409+ }
410+ if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyCode > ( ) , b. downcast_ref :: < PyCode > ( ) ) {
411+ return x. eq_metadata ( y)
412+ && x. code . constants . len ( ) == y. code . constants . len ( )
413+ && x. code
414+ . constants
415+ . iter ( )
416+ . zip ( y. code . constants . iter ( ) )
417+ . all ( |( x, y) | const_eq ( ctx, & x. 0 , & y. 0 ) ) ;
418+ }
394419 if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyTuple > ( ) , b. downcast_ref :: < PyTuple > ( ) ) {
395420 return x. as_slice ( ) . len ( ) == y. as_slice ( ) . len ( )
396421 && x. as_slice ( )
397422 . iter ( )
398423 . zip ( y. as_slice ( ) )
399- . all ( |( e1, e2) | const_eq ( e1, e2) ) ;
424+ . all ( |( e1, e2) | const_eq ( ctx , e1, e2) ) ;
400425 }
401426 if let ( Some ( x) , Some ( y) ) = (
402427 a. downcast_ref :: < PyFrozenSet > ( ) ,
403428 b. downcast_ref :: < PyFrozenSet > ( ) ,
404429 ) {
405430 let ( xe, ye) = ( x. elements ( ) , y. elements ( ) ) ;
406- return xe. len ( ) == ye. len ( ) && xe. iter ( ) . all ( |e1| ye. iter ( ) . any ( |e2| const_eq ( e1, e2) ) ) ;
431+ return xe. len ( ) == ye. len ( )
432+ && xe
433+ . iter ( )
434+ . all ( |e1| ye. iter ( ) . any ( |e2| const_eq ( ctx, e1, e2) ) ) ;
407435 }
408436 // `None`/`Ellipsis` are canonical singletons already handled by the identity check above.
409437 false
@@ -457,9 +485,11 @@ impl ConstantBag for PyObjBag<'_> {
457485 ( obj, hash)
458486 } )
459487 . collect ( ) ;
460- PyFrozenSet :: from_constant_elements ( elements_with_hashes, const_eq)
461- . into_ref ( ctx)
462- . into ( )
488+ PyFrozenSet :: from_constant_elements ( elements_with_hashes, |a, b| {
489+ const_eq ( ctx, a, b)
490+ } )
491+ . into_ref ( ctx)
492+ . into ( )
463493 }
464494 BorrowedConstant :: None => ctx. none ( ) ,
465495 BorrowedConstant :: Ellipsis => ctx. ellipsis . clone ( ) . into ( ) ,
@@ -883,6 +913,44 @@ impl fmt::Debug for PyCode {
883913}
884914
885915impl PyCode {
916+ fn eq_metadata ( & self , other : & Self ) -> bool {
917+ let a = & self . code ;
918+ let b = & other. code ;
919+ a. obj_name == b. obj_name
920+ && a. arg_count == b. arg_count
921+ && a. posonlyarg_count == b. posonlyarg_count
922+ && a. kwonlyarg_count == b. kwonlyarg_count
923+ && a. flags == b. flags
924+ && a. first_line_number == b. first_line_number
925+ && a. instructions . original_bytes ( ) == b. instructions . original_bytes ( )
926+ && a. linetable == b. linetable
927+ && a. exceptiontable == b. exceptiontable
928+ && a. names == b. names
929+ && a. varnames == b. varnames
930+ && a. freevars == b. freevars
931+ && a. cellvars == b. cellvars
932+ }
933+
934+ fn hash_attributes ( & self , ctx : & Context ) -> PyTupleRef {
935+ let code = & self . code ;
936+ // Hash a tuple of key attributes, matching CPython's code_hash
937+ ctx. new_tuple ( vec ! [
938+ ctx. new_str( code. obj_name. as_str( ) ) . into( ) ,
939+ ctx. new_int( code. arg_count) . into( ) ,
940+ ctx. new_int( code. posonlyarg_count) . into( ) ,
941+ ctx. new_int( code. kwonlyarg_count) . into( ) ,
942+ ctx. new_int( code. varnames. len( ) ) . into( ) ,
943+ ctx. new_int( code. flags. bits( ) ) . into( ) ,
944+ ctx. new_int( code. first_line_number. map_or( 0 , |n| n. get( ) ) as i64 )
945+ . into( ) ,
946+ ctx. new_bytes( code. instructions. original_bytes( ) ) . into( ) ,
947+ {
948+ let consts: Vec <_> = code. constants. iter( ) . map( |c| c. 0 . clone( ) ) . collect( ) ;
949+ ctx. new_tuple( consts) . into( )
950+ } ,
951+ ] )
952+ }
953+
886954 /// Line number for a byte offset, or -1 when the linetable has no line.
887955 pub fn addr2line ( & self , lasti_bytes : i32 ) -> i32 {
888956 if lasti_bytes < 0 {
@@ -935,61 +1003,30 @@ impl Comparable for PyCode {
9351003 let other = class_or_notimplemented ! ( Self , other) ;
9361004 let a = & zelf. code ;
9371005 let b = & other. code ;
938- let eq = a. obj_name == b. obj_name
939- && a. arg_count == b. arg_count
940- && a. posonlyarg_count == b. posonlyarg_count
941- && a. kwonlyarg_count == b. kwonlyarg_count
942- && a. flags == b. flags
943- && a. first_line_number == b. first_line_number
944- && a. instructions . original_bytes ( ) == b. instructions . original_bytes ( )
945- && a. linetable == b. linetable
946- && a. exceptiontable == b. exceptiontable
947- && a. names == b. names
948- && a. varnames == b. varnames
949- && a. freevars == b. freevars
950- && a. cellvars == b. cellvars
951- && {
952- let a_consts: Vec < _ > = a. constants . iter ( ) . map ( |c| c. 0 . clone ( ) ) . collect ( ) ;
953- let b_consts: Vec < _ > = b. constants . iter ( ) . map ( |c| c. 0 . clone ( ) ) . collect ( ) ;
954- if a_consts. len ( ) != b_consts. len ( ) {
955- false
956- } else {
957- let mut eq = true ;
958- for ( ac, bc) in a_consts. iter ( ) . zip ( b_consts. iter ( ) ) {
959- if !vm. bool_eq ( ac, bc) ? {
960- eq = false ;
961- break ;
962- }
1006+ let eq = zelf. eq_metadata ( other) && {
1007+ let a_consts: Vec < _ > = a. constants . iter ( ) . map ( |c| c. 0 . clone ( ) ) . collect ( ) ;
1008+ let b_consts: Vec < _ > = b. constants . iter ( ) . map ( |c| c. 0 . clone ( ) ) . collect ( ) ;
1009+ if a_consts. len ( ) != b_consts. len ( ) {
1010+ false
1011+ } else {
1012+ let mut eq = true ;
1013+ for ( ac, bc) in a_consts. iter ( ) . zip ( b_consts. iter ( ) ) {
1014+ if !vm. bool_eq ( ac, bc) ? {
1015+ eq = false ;
1016+ break ;
9631017 }
964- eq
9651018 }
966- } ;
1019+ eq
1020+ }
1021+ } ;
9671022 Ok ( eq. into ( ) )
9681023 } )
9691024 }
9701025}
9711026
9721027impl Hashable for PyCode {
9731028 fn hash ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < crate :: common:: hash:: PyHash > {
974- let code = & zelf. code ;
975- // Hash a tuple of key attributes, matching CPython's code_hash
976- let tuple = vm. ctx . new_tuple ( vec ! [
977- vm. ctx. new_str( code. obj_name. as_str( ) ) . into( ) ,
978- vm. ctx. new_int( code. arg_count) . into( ) ,
979- vm. ctx. new_int( code. posonlyarg_count) . into( ) ,
980- vm. ctx. new_int( code. kwonlyarg_count) . into( ) ,
981- vm. ctx. new_int( code. varnames. len( ) ) . into( ) ,
982- vm. ctx. new_int( code. flags. bits( ) ) . into( ) ,
983- vm. ctx
984- . new_int( code. first_line_number. map_or( 0 , |n| n. get( ) ) as i64 )
985- . into( ) ,
986- vm. ctx. new_bytes( code. instructions. original_bytes( ) ) . into( ) ,
987- {
988- let consts: Vec <_> = code. constants. iter( ) . map( |c| c. 0 . clone( ) ) . collect( ) ;
989- vm. ctx. new_tuple( consts) . into( )
990- } ,
991- ] ) ;
992- tuple. as_object ( ) . hash ( vm)
1029+ zelf. hash_attributes ( & vm. ctx ) . as_object ( ) . hash ( vm)
9931030 }
9941031}
9951032
@@ -1901,6 +1938,117 @@ assert u_count == 3
19011938 } ) ;
19021939 }
19031940
1941+ #[ test]
1942+ fn context_only_frozenset_slice_and_code ( ) {
1943+ use super :: { PyObjBag , const_hash} ;
1944+ use crate :: {
1945+ AsObject , Context , PyPayload ,
1946+ builtins:: PyFrozenSet ,
1947+ bytecode:: { Constant , ConstantBag , ConstantData } ,
1948+ compiler,
1949+ } ;
1950+
1951+ let slice = |stop| ConstantData :: Slice {
1952+ elements : Box :: new ( [ ConstantData :: None , stop, ConstantData :: None ] ) ,
1953+ } ;
1954+ let int_slice = slice ( ConstantData :: Integer { value : 10 . into ( ) } ) ;
1955+ let code = compiler:: compile (
1956+ "pass" ,
1957+ compiler:: Mode :: Exec ,
1958+ "<constant-test>" ,
1959+ Default :: default ( ) ,
1960+ )
1961+ . unwrap ( ) ;
1962+ let mut different_constants = code. clone ( ) ;
1963+ different_constants. constants = core:: iter:: once ( ConstantData :: Tuple {
1964+ elements : vec ! [
1965+ int_slice. clone( ) ,
1966+ ConstantData :: Code {
1967+ code: Box :: new( code. clone( ) ) ,
1968+ } ,
1969+ ] ,
1970+ } )
1971+ . collect ( ) ;
1972+ // Names affect code equality but are omitted from its hash.
1973+ let mut different_names = code. clone ( ) ;
1974+ different_names. names = vec ! [ "unused" . into( ) ] . into_boxed_slice ( ) ;
1975+ let code_constant = |code| ConstantData :: Code {
1976+ code : Box :: new ( code) ,
1977+ } ;
1978+ let different_constants = code_constant ( different_constants) ;
1979+ let code = code_constant ( code) ;
1980+ let nested = ConstantData :: Tuple {
1981+ elements : vec ! [
1982+ int_slice. clone( ) ,
1983+ ConstantData :: Frozenset {
1984+ elements: vec![ code. clone( ) , code. clone( ) ] ,
1985+ } ,
1986+ ] ,
1987+ } ;
1988+ let cases = [
1989+ (
1990+ "slices" ,
1991+ vec ! [
1992+ int_slice. clone( ) ,
1993+ int_slice,
1994+ slice( ConstantData :: Float { value: 10.0 } ) ,
1995+ slice( ConstantData :: Integer { value: 11 . into( ) } ) ,
1996+ ] ,
1997+ 2 ,
1998+ ) ,
1999+ (
2000+ "code constants" ,
2001+ vec ! [
2002+ code. clone( ) ,
2003+ different_constants. clone( ) ,
2004+ different_constants,
2005+ ] ,
2006+ 2 ,
2007+ ) ,
2008+ (
2009+ "code hash collision" ,
2010+ vec ! [ code, code_constant( different_names) ] ,
2011+ 2 ,
2012+ ) ,
2013+ ( "nested" , vec ! [ nested. clone( ) , nested] , 1 ) ,
2014+ ] ;
2015+ let ctx = Context :: genesis ( ) ;
2016+ let make = |data : & ConstantData | PyObjBag ( ctx) . make_constant ( data. borrow_constant ( ) ) . 0 ;
2017+ let cases: Vec < _ > = cases
2018+ . into_iter ( )
2019+ . map ( |( label, elements, expected_len) | {
2020+ let keys: Vec < _ > = elements. iter ( ) . map ( make) . collect ( ) ;
2021+ let constant = make ( & ConstantData :: Frozenset { elements } ) ;
2022+ ( label, constant, keys, expected_len)
2023+ } )
2024+ . collect ( ) ;
2025+
2026+ Interpreter :: without_stdlib ( Default :: default ( ) ) . enter ( |vm| {
2027+ for ( label, constant, keys, expected_len) in cases {
2028+ let frozen = constant. downcast_ref :: < PyFrozenSet > ( ) . unwrap ( ) ;
2029+ assert_eq ! ( frozen. elements( ) . len( ) , expected_len, "{label}" ) ;
2030+ for key in & keys {
2031+ assert_eq ! ( const_hash( ctx, key) , key. hash( vm) . unwrap( ) , "{label}" ) ;
2032+ assert ! ( frozen. contains( key, vm) . unwrap( ) , "{label}" ) ;
2033+ }
2034+ if label == "code hash collision" {
2035+ assert_eq ! ( keys[ 0 ] . hash( vm) . unwrap( ) , keys[ 1 ] . hash( vm) . unwrap( ) ) ;
2036+ assert ! ( !vm. bool_eq( & keys[ 0 ] , & keys[ 1 ] ) . unwrap( ) ) ;
2037+ }
2038+ let runtime = PyFrozenSet :: from_iter ( vm, keys) . unwrap ( ) . into_ref ( ctx) ;
2039+ assert ! (
2040+ vm. bool_eq( & constant, runtime. as_object( ) ) . unwrap( ) ,
2041+ "{label}"
2042+ ) ;
2043+ assert_eq ! (
2044+ const_hash( ctx, & constant) ,
2045+ runtime. as_object( ) . hash( vm) . unwrap( ) ,
2046+ "{label}" ,
2047+ ) ;
2048+ }
2049+ } ) ;
2050+ }
2051+
19042052 #[ cfg( not( target_arch = "wasm32" ) ) ]
19052053 #[ test]
19062054 fn context_only_frozenset_hash_secret ( ) {
0 commit comments