Bug description:
Since gh-150027, frozenset() of a set comprehension freezes the temporary set in place (make_frozenset() in Python/intrinsics.c), even when something else still holds it. A trace function that keeps the return value of a comprehension in a class annotation is enough:
import sys
kept = []
def tracer(frame, event, arg):
if event == 'return' and isinstance(arg, set):
kept.append(arg)
return tracer
class C:
x: frozenset({i for i in range(3)})
sys.settrace(tracer)
ann = C.__annotations__
sys.settrace(None)
print(type(kept[0]).__name__, kept[0] is ann['x'])
3.15 prints set False. A release build of main prints frozenset True: the set the tracer kept has become the returned frozenset. A debug build aborts:
python: Python/intrinsics.c:215: make_frozenset: Assertion `_PyObject_IsUniquelyReferenced(set)' failed.
Stepping into such an annotation with pdb's s command aborts a debug build the same way.
Expected: the kept object stays a set and frozenset() returns a new object, as in 3.15.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.16.0a0 (heads/fix-frozenset-shared-set:77c06751571, Sep 30 2026, 13:07:14) [GCC 15.2.0]
Linked PRs
Bug description:
Since gh-150027,
frozenset()of a set comprehension freezes the temporary set in place (make_frozenset()inPython/intrinsics.c), even when something else still holds it. A trace function that keeps the return value of a comprehension in a class annotation is enough:3.15 prints
set False. A release build of main printsfrozenset True: the set the tracer kept has become the returned frozenset. A debug build aborts:Stepping into such an annotation with pdb's
scommand aborts a debug build the same way.Expected: the kept object stays a
setandfrozenset()returns a new object, as in 3.15.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.16.0a0 (heads/fix-frozenset-shared-set:77c06751571, Sep 30 2026, 13:07:14) [GCC 15.2.0]
Linked PRs