@@ -776,6 +776,13 @@ def check_constant(self, func, expected):
776776 self .fail ("unable to find constant %r in %r"
777777 % (expected , func .__code__ .co_consts ))
778778
779+ @staticmethod
780+ def _frozen_dict_consts (* consts ):
781+ """Use AST to make frozendict constants since it has no literal syntax"""
782+ m = ast .Interactive ([ast .Expr (ast .Constant (c )) for c in consts ])
783+ ast .fix_missing_locations (m )
784+ return compile (m , "<test>" , "single" )
785+
779786 # Merging equal constants is not a strict requirement for the Python
780787 # semantics, it's a more an implementation detail.
781788 @support .cpython_only
@@ -820,6 +827,21 @@ def check_same_constant(const):
820827 self .check_constant (f1 , frozenset ({0 }))
821828 self .assertTrue (f1 (0 ))
822829
830+ # two identical frozendicts merge into one constant
831+ c = self ._frozen_dict_consts (frozendict ({0 : 1 }), frozendict ({0 : 1 }))
832+ self .assertEqual (c .co_consts , (frozendict ({0 : 1 }),))
833+
834+ # empty frozendicts also merge
835+ c = self ._frozen_dict_consts (frozendict (), frozendict ())
836+ self .assertEqual (c .co_consts , (frozendict (),))
837+
838+ # frozendicts containing a nested frozendict value merge
839+ c = self ._frozen_dict_consts (
840+ frozendict ({0 : frozendict ({1 : 2 })}),
841+ frozendict ({0 : frozendict ({1 : 2 })}),
842+ )
843+ self .assertEqual (c .co_consts , (frozendict ({0 : frozendict ({1 : 2 })}),))
844+
823845 # Merging equal co_linetable is not a strict requirement
824846 # for the Python semantics, it's a more an implementation detail.
825847 @support .cpython_only
@@ -1033,6 +1055,16 @@ def check_different_constants(const1, const2):
10331055 self .assertTrue (f1 (0 ))
10341056 self .assertTrue (f2 (0.0 ))
10351057
1058+ # frozendicts with type-distinct keys must not merge (0 vs 0.0)
1059+ c = self ._frozen_dict_consts (frozendict ({0 : 1 }), frozendict ({0.0 : 1 }))
1060+ self .assertEqual (c .co_consts , (frozendict ({0 : 1 }), frozendict ({0.0 : 1 })))
1061+ self .assertIsNot (c .co_consts [0 ], c .co_consts [1 ])
1062+
1063+ # frozendicts with type-distinct values must not merge (1 vs 1.0)
1064+ c = self ._frozen_dict_consts (frozendict ({0 : 1 }), frozendict ({0 : 1.0 }))
1065+ self .assertEqual (c .co_consts , (frozendict ({0 : 1 }), frozendict ({0 : 1.0 })))
1066+ self .assertIsNot (c .co_consts [0 ], c .co_consts [1 ])
1067+
10361068 def test_path_like_objects (self ):
10371069 # An implicit test for PyUnicode_FSDecoder().
10381070 compile ("42" , FakePath ("test_compile_pathlike" ), "single" )
0 commit comments