Skip to content

frozenset({...}) freezes a set that is still referenced #158490

Description

@fedonman

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions