@@ -281,6 +281,110 @@ fn is_name_chars(value: &crate::common::wtf8::Wtf8) -> bool {
281281 . all ( |& b| b. is_ascii_alphanumeric ( ) || b == b'_' )
282282}
283283
284+ /// Structural hash for an object built from a compile-time constant, without a `VirtualMachine`
285+ /// - safe because none of these types can have a custom `__hash__`. Must agree with each type's
286+ /// real `Hashable` impl, notably matching across the numeric tower (`hash(1) == hash(1.0)`),
287+ /// since `1` and `1.0` must land in the same frozenset-constant bucket.
288+ fn const_hash ( ctx : & Context , obj : & PyObject ) -> crate :: common:: hash:: PyHash {
289+ use crate :: common:: hash;
290+ use crate :: builtins:: { PyBool , PyBytes , PyComplex , PyFloat , PyInt , PyStr , PyTuple } ;
291+
292+ if let Some ( b) = obj. downcast_ref :: < PyBool > ( ) {
293+ hash:: hash_bigint ( b. 0 . as_bigint ( ) )
294+ } else if let Some ( int) = obj. downcast_ref :: < PyInt > ( ) {
295+ hash:: hash_bigint ( int. as_bigint ( ) )
296+ } else if let Some ( float) = obj. downcast_ref :: < PyFloat > ( ) {
297+ hash:: hash_float ( float. to_f64 ( ) ) . unwrap_or_else ( || hash:: hash_object_id ( obj. get_id ( ) ) )
298+ } else if let Some ( complex) = obj. downcast_ref :: < PyComplex > ( ) {
299+ let value = complex. to_complex64 ( ) ;
300+ let re_hash =
301+ hash:: hash_float ( value. re ) . unwrap_or_else ( || hash:: hash_object_id ( obj. get_id ( ) ) ) ;
302+ let im_hash =
303+ hash:: hash_float ( value. im ) . unwrap_or_else ( || hash:: hash_object_id ( obj. get_id ( ) ) ) ;
304+ let core:: num:: Wrapping ( ret) =
305+ core:: num:: Wrapping ( re_hash) + core:: num:: Wrapping ( im_hash) * core:: num:: Wrapping ( hash:: IMAG ) ;
306+ hash:: fix_sentinel ( ret)
307+ } else if let Some ( s) = obj. downcast_ref :: < PyStr > ( ) {
308+ // Matches `PyStr::hash` - strings hash their WTF-8 bytes, not a validated `&str`.
309+ ctx. hash_secret . hash_bytes ( s. as_bytes ( ) )
310+ } else if let Some ( b) = obj. downcast_ref :: < PyBytes > ( ) {
311+ ctx. hash_secret . hash_bytes ( b. as_bytes ( ) )
312+ } else if let Some ( t) = obj. downcast_ref :: < PyTuple > ( ) {
313+ let hashes = t
314+ . as_slice ( )
315+ . iter ( )
316+ . map ( |e| Ok :: < _ , core:: convert:: Infallible > ( const_hash ( ctx, e) ) ) ;
317+ match hash:: hash_tuple ( hashes) {
318+ Ok ( h) => h,
319+ Err ( never) => match never { } ,
320+ }
321+ } else if let Some ( fs) = obj. downcast_ref :: < PyFrozenSet > ( ) {
322+ let mut acc = hash:: FrozenSetHash :: new ( fs. elements ( ) . len ( ) ) ;
323+ for e in fs. elements ( ) {
324+ acc. add ( const_hash ( ctx, & e) ) ;
325+ }
326+ acc. finish ( )
327+ } else if obj. is ( ctx. none . as_object ( ) ) {
328+ // Matches `Hashable for PyNone`.
329+ 0xFCA86420_u64 as hash:: PyHash
330+ } else {
331+ // `Ellipsis` (no custom `__hash__`) and anything else - identity-based, same as the
332+ // default object hash a real VM would compute for it.
333+ hash:: hash_object_id ( obj. get_id ( ) )
334+ }
335+ }
336+
337+ /// Structural equality for compile-time constant objects, matching Python equality - including
338+ /// numeric-tower cross-type equality (`1 == 1.0 == True`) - without a `VirtualMachine`. Safe
339+ /// because none of these types can have a custom `__eq__`.
340+ fn const_eq ( a : & PyObject , b : & PyObject ) -> bool {
341+ use crate :: common:: float_ops;
342+ use crate :: builtins:: { PyBool , PyBytes , PyFloat , PyInt , PyStr , PyTuple } ;
343+
344+ if a. is ( b) {
345+ return true ;
346+ }
347+ let a_bigint = a
348+ . downcast_ref :: < PyBool > ( )
349+ . map ( |b| b. 0 . as_bigint ( ) )
350+ . or_else ( || a. downcast_ref :: < PyInt > ( ) . map ( |i| i. as_bigint ( ) ) ) ;
351+ let b_bigint = b
352+ . downcast_ref :: < PyBool > ( )
353+ . map ( |b| b. 0 . as_bigint ( ) )
354+ . or_else ( || b. downcast_ref :: < PyInt > ( ) . map ( |i| i. as_bigint ( ) ) ) ;
355+ if let ( Some ( x) , Some ( y) ) = ( a_bigint, b_bigint) {
356+ return x == y;
357+ }
358+ if let ( Some ( x) , Some ( y) ) = ( a_bigint, b. downcast_ref :: < PyFloat > ( ) ) {
359+ return float_ops:: eq_int ( y. to_f64 ( ) , x) ;
360+ }
361+ if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyFloat > ( ) , b_bigint) {
362+ return float_ops:: eq_int ( x. to_f64 ( ) , y) ;
363+ }
364+ if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyFloat > ( ) , b. downcast_ref :: < PyFloat > ( ) ) {
365+ return x. to_f64 ( ) == y. to_f64 ( ) ;
366+ }
367+ if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyStr > ( ) , b. downcast_ref :: < PyStr > ( ) ) {
368+ return x. as_bytes ( ) == y. as_bytes ( ) ;
369+ }
370+ if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyBytes > ( ) , b. downcast_ref :: < PyBytes > ( ) ) {
371+ return x. as_bytes ( ) == y. as_bytes ( ) ;
372+ }
373+ if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyTuple > ( ) , b. downcast_ref :: < PyTuple > ( ) ) {
374+ return x. as_slice ( ) . len ( ) == y. as_slice ( ) . len ( )
375+ && x. as_slice ( )
376+ . iter ( )
377+ . zip ( y. as_slice ( ) )
378+ . all ( |( e1, e2) | const_eq ( e1, e2) ) ;
379+ }
380+ if let ( Some ( x) , Some ( y) ) = ( a. downcast_ref :: < PyFrozenSet > ( ) , b. downcast_ref :: < PyFrozenSet > ( ) ) {
381+ let ( xe, ye) = ( x. elements ( ) , y. elements ( ) ) ;
382+ return xe. len ( ) == ye. len ( ) && xe. iter ( ) . all ( |e1| ye. iter ( ) . any ( |e2| const_eq ( e1, e2) ) ) ;
383+ }
384+ // `None`/`Ellipsis` are canonical singletons already handled by the identity check above.
385+ false
386+ }
387+
284388impl ConstantBag for PyObjBag < ' _ > {
285389 type Constant = Literal ;
286390
@@ -320,13 +424,18 @@ impl ConstantBag for PyObjBag<'_> {
320424 . into_ref ( ctx)
321425 . into ( )
322426 }
323- BorrowedConstant :: Frozenset { elements : _ } => {
324- // Creating a frozenset requires VirtualMachine for element hashing.
325- // PyObjBag only has Context, so we cannot construct PyFrozenSet here.
326- // Frozenset constants from .pyc are handled by PyMarshalBag which has VM access.
327- unimplemented ! (
328- "frozenset constant in PyObjBag::make_constant requires VirtualMachine"
329- )
427+ BorrowedConstant :: Frozenset { elements } => {
428+ let elements_with_hashes = elements
429+ . iter ( )
430+ . map ( |constant| {
431+ let obj = self . make_constant ( constant. borrow_constant ( ) ) . 0 ;
432+ let hash = const_hash ( ctx, & obj) ;
433+ ( obj, hash)
434+ } )
435+ . collect ( ) ;
436+ PyFrozenSet :: from_constant_elements ( elements_with_hashes, const_eq)
437+ . into_ref ( ctx)
438+ . into ( )
330439 }
331440 BorrowedConstant :: None => ctx. none ( ) ,
332441 BorrowedConstant :: Ellipsis => ctx. ellipsis . clone ( ) . into ( ) ,
@@ -1736,3 +1845,38 @@ impl<'a> LineTableReader<'a> {
17361845pub ( crate ) fn init ( ctx : & ' static Context ) {
17371846 PyCode :: extend_class ( ctx, ctx. types . code_type ) ;
17381847}
1848+
1849+ #[ cfg( test) ]
1850+ mod tests {
1851+ use crate :: Interpreter ;
1852+
1853+ /// Exercises `PyObjBag` specifically (`Context`, no `VirtualMachine`) via `ctx.new_code`,
1854+ /// not `vm.compile`/`PyVmBag` - a frozenset literal constant used to panic here.
1855+ #[ test]
1856+ fn context_only_frozenset_constant ( ) {
1857+ Interpreter :: without_stdlib ( Default :: default ( ) ) . enter ( |vm| {
1858+ let source = "\
1859+ x = frozenset({1, 2, 3})
1860+ assert 1 in x and 2 in x and 3 in x and 4 not in x
1861+ assert len(x) == 3
1862+
1863+ # numeric tower: 1 == 1.0 == True must collapse to a single element, matching real Python.
1864+ y = frozenset({1, 1.0, True})
1865+ assert len(y) == 1
1866+ assert 1 in y and 1.0 in y and True in y
1867+
1868+ z = frozenset({'a', 'b', 'a'})
1869+ assert len(z) == 2
1870+
1871+ w = frozenset({(1, 'a'), (1, 'a'), (2, 'b')})
1872+ assert len(w) == 2
1873+ " ;
1874+ let opts = vm. compile_opts ( ) ;
1875+ let code = crate :: compiler:: compile ( source, crate :: compiler:: Mode :: Exec , "<test>" , opts)
1876+ . expect ( "compile should succeed" ) ;
1877+ let code_ref = vm. ctx . new_code ( code) ;
1878+ let scope = vm. new_scope_with_builtins ( ) ;
1879+ vm. run_code_obj ( code_ref, scope) . unwrap ( ) ;
1880+ } ) ;
1881+ }
1882+ }
0 commit comments