GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-141510: Add PyAnyDict_AsNewDict() function by vstinner · Pull Request #145531 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Mar 5, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Remove internal _PyDict_CopyAsDict() function.

  • Issue: PEP 814: Add built-in frozendict type #141510

📚 Documentation preview 📚: https://cpython-previews--145531.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
pythongh-141510: Add PyFrozenDict_AsDict() function
5ba97b6
* PyDict_Copy() no longer accepts frozendict.
* Remove _PyDict_CopyAsDict() function.
* Fix frozendict.items() ^ frozendict.items(). Add non-regression test.
@vstinner
vstinner requested review from ZeroIntensity, markshannon and methane as code owners March 5, 2026 11:25
@bedevere-app bedevere-app Bot mentioned this pull request Mar 5, 2026
PEP 814: Add built-in frozendict type #141510
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Mar 5, 2026
@vstinner vstinner mentioned this pull request Mar 5, 2026
gh-141510: Always return a dict in PyDict_Copy() #145517
Closed
@vstinner vstinner added the skip news label Mar 5, 2026
@vstinner

vstinner commented Mar 5, 2026

Copy link
Copy Markdown
Member Author

Example of code to copy a frozendict or a dict (from Objects/typeobject.c):

    PyObject *dict;
    if (PyFrozenDict_Check(ctx->orig_dict)) {
        dict = PyFrozenDict_AsDict(ctx->orig_dict);
    }
    else {
        dict = PyDict_Copy(ctx->orig_dict);
    }
    if (dict == NULL) {
        goto error;
    }
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

corona10
corona10 reviewed Mar 5, 2026
View reviewed changes

@corona10 corona10 left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not part of PEP, C API WG is fine with the naming?

cc @encukou

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
vstinner added 2 commits March 5, 2026 12:49
@vstinner
Fix typo in C API tests
741f013
@vstinner
Merge branch 'main' into frozendict_copy
6947aec
@encukou

encukou commented Mar 5, 2026

Copy link
Copy Markdown
Member

What's the motivation for this change?

Would something like PyAnyDict_AsNewDict be more useful?

C API WG is fine with the naming?

That's for a vote; I can't speak for the entire WG.
FWIW, PEP 814 wasn't consulted with the C API WG in the first place. Personally I didn't follow it very closely. Things like making PyDict_Copy accept frozendict (which is being reverted here) aren't specified in the PEP anyway.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Merge branch 'main' into frozendict_copy
9d672ba
@vstinner

vstinner commented Mar 5, 2026

Copy link
Copy Markdown
Member Author

This PR was big, so I merged unrelated changes as separated changes:

  • commit 2cd0ddf fix frozendict.items() ^ frozendict.items().
  • commit 0c29f83 modify PyDict_Copy() to no longer accept frozendict

@ZeroIntensity convinced me that supporting frozendict in PyDict_Copy() is a bad idea.

What's the motivation for this change?

When a function using PyDict_Copy() is modified to support frozendict, I have to write code like that:

    PyObject *dict;
    if (PyFrozenDict_Check(orig_dict)) {
        dict = PyDict_New();
        if (dict == NULL) {
            goto error;
        }
        if (PyDict_Merge(dict, orig_dict, 1) < 0) {
            Py_DECREF(dict);
            goto error;
        }
    }
    else {
        dict = PyDict_Copy(orig_dict);
        if (dict == NULL) {
            goto error;
        }
    }

I have to copy/paste this code. I would prefer to have a function doing that: convert a frozendict to a dict, copy a dict, or fail if the argument is not a dict or a frozendict. A dedicated function may be more efficient than PyDict_New()+PyDict_Merge().

Would something like PyAnyDict_AsNewDict be more useful?

Oh, I prefer this function over PyFrozenDict_AsDict().

@ZeroIntensity: What do you think of adding a new PyAnyDict_AsNewDict() function?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Mar 5, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

I think this will be useful. When 3.15 comes out, there will be plenty of APIs that assume an input dictionary is mutable, and thus can't be used with frozendict. Being able to get a dictionary out of a frozendict will help adoption in that sense.

Is there an east way to convert a dictionary into a frozendict? Another potential use-case would be if you're trying to implement a function like this in C:

def adjust_frozendict(data: frozendict) -> frozendict:
    mutable = dict(data)
    mutable["whatever"] = 123
    return frozendict(mutable)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Replace the function with PyAnyDict_AsNewDict()
568ca96
@vstinner vstinner changed the title gh-141510: Add PyFrozenDict_AsDict() function gh-141510: Add PyAnyDict_AsNewDict() function Mar 5, 2026
@vstinner

vstinner commented Mar 5, 2026

Copy link
Copy Markdown
Member Author

Ok, I renamed the function to PyAnyDict_AsNewDict(). It accepts dict and frozendict and creates a new dict.

Is there an easy way to convert a dictionary into a frozendict?

Yes, call PyFrozenDict_New(dict): https://docs.python.org/dev/c-api/dict.html#c.PyFrozenDict_New

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brijkapadia brijkapadia mentioned this pull request Mar 12, 2026
Implement frozendict for partialobject in functools module #145478
Closed
@vstinner

vstinner commented Mar 12, 2026

Copy link
Copy Markdown
Member Author

Currently, dict = PyObject_CallOneArg((PyObject*)&PyDict_Type, frozendict) can be used to convert a frozendict to a dict. The code is harder to read and harder to "discover", but it just works.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented May 8, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 8, 2026
@vstinner

vstinner commented May 13, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

I lost track of this function and Python 3.15 beta1 has been released without it. We are now past the feature freeze, so I don't think that it's worth it to add such function.

We can revisit this function later if there is a need for it. I close th PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner closed this May 13, 2026
@vstinner
vstinner deleted the frozendict_copy branch May 13, 2026 09:30
@encukou

This comment was marked as resolved.

Sign in to view
@vstinner

This comment was marked as resolved.

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

Reviewers

@corona10 corona10 corona10 left review comments
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@methane methane Awaiting requested review from methane methane is a code owner
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review skip news stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@vstinner @encukou @ZeroIntensity @corona10
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.