@@ -5,7 +5,6 @@ use super::pytype::PyTypeRef;
55use crate :: common:: hash:: PyHash ;
66use crate :: common:: rc:: PyRc ;
77use crate :: dictdatatype;
8- use crate :: function:: OptionalArg :: { Missing , Present } ;
98use crate :: function:: { Args , OptionalArg } ;
109use crate :: slots:: { Comparable , Hashable , Iterable , PyComparisonOp , PyIter , Unhashable } ;
1110use crate :: vm:: { ReprGuard , VirtualMachine } ;
@@ -554,8 +553,7 @@ macro_rules! multi_args_frozenset {
554553
555554#[ pyimpl( flags( BASETYPE ) , with( Hashable , Comparable , Iterable ) ) ]
556555impl PyFrozenSet {
557- // used by ssl.rs windows
558- #[ allow( dead_code) ]
556+ // Also used by ssl.rs windows.
559557 pub ( crate ) fn from_iter (
560558 vm : & VirtualMachine ,
561559 it : impl IntoIterator < Item = PyObjectRef > ,
@@ -573,23 +571,26 @@ impl PyFrozenSet {
573571 iterable : OptionalArg < PyObjectRef > ,
574572 vm : & VirtualMachine ,
575573 ) -> PyResult < PyRef < Self > > {
576- let iterable = if let Present ( iterable) = iterable {
577- if cls. is ( & vm. ctx . types . frozenset_type ) {
578- match iterable. downcast_exact :: < PyFrozenSet > ( vm) {
579- Ok ( iter ) => return Ok ( iter ) ,
580- Err ( iterable) => Present ( PyIterable :: try_from_object ( vm , iterable) ? ) ,
574+ let elements = if let OptionalArg :: Present ( iterable) = iterable {
575+ let iterable = if cls. is ( & vm. ctx . types . frozenset_type ) {
576+ match iterable. downcast_exact :: < Self > ( vm) {
577+ Ok ( fs ) => return Ok ( fs ) ,
578+ Err ( iterable) => iterable,
581579 }
582580 } else {
583- Present ( PyIterable :: try_from_object ( vm, iterable) ?)
584- }
581+ iterable
582+ } ;
583+ vm. extract_elements ( & iterable) ?
585584 } else {
586- Missing
585+ vec ! [ ]
587586 } ;
588587
589- Self {
590- inner : PySetInner :: from_arg ( iterable, vm) ?,
588+ // Return empty fs if iterable passed is emtpy and only for exact fs types.
589+ if elements. is_empty ( ) && cls. is ( & vm. ctx . types . frozenset_type ) {
590+ Ok ( vm. ctx . empty_frozenset . clone ( ) )
591+ } else {
592+ Self :: from_iter ( vm, elements) . and_then ( |o| o. into_ref_with_type ( vm, cls) )
591593 }
592- . into_ref_with_type ( vm, cls)
593594 }
594595
595596 #[ pymethod( name = "__len__" ) ]
0 commit comments