Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-141510: Add PyAnyDict_AsNewDict() function#145531

Open
vstinner wants to merge 5 commits intopython:mainfrom
vstinner:frozendict_copy
Open

gh-141510: Add PyAnyDict_AsNewDict() function#145531
vstinner wants to merge 5 commits intopython:mainfrom
vstinner:frozendict_copy

Conversation

@vstinner
Copy link
Member

@vstinnervstinner commentedMar 5, 2026
edited
Loading

Remove internal _PyDict_CopyAsDict() function.


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

* PyDict_Copy() no longer accepts frozendict.* Remove _PyDict_CopyAsDict() function.* Fix frozendict.items() ^ frozendict.items(). Add non-regression test.
@vstinner
Copy link
MemberAuthor

Example of code to copy afrozendict or adict (fromObjects/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) {        gotoerror;    }

Copy link
Member

@corona10corona10 left a 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

@encukou
Copy link
Member

What's the motivation for this change?

Would something likePyAnyDict_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 makingPyDict_Copy acceptfrozendict (which is being reverted here) aren't specified in the PEP anyway.

@vstinner
Copy link
MemberAuthor

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

  • commit2cd0ddf fixfrozendict.items() ^ frozendict.items().
  • commit0c29f83 modifyPyDict_Copy() to no longer acceptfrozendict

@ZeroIntensity convinced me that supportingfrozendict inPyDict_Copy() is a bad idea.

What's the motivation for this change?

When a function usingPyDict_Copy() is modified to supportfrozendict, I have to write code like that:

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

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

Would something like PyAnyDict_AsNewDict be more useful?

Oh, I prefer this function overPyFrozenDict_AsDict().

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

@ZeroIntensity
Copy link
Member

ZeroIntensity commentedMar 5, 2026
edited
Loading

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 withfrozendict. Being able to get a dictionary out of afrozendict will help adoption in that sense.

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

defadjust_frozendict(data:frozendict)->frozendict:mutable=dict(data)mutable["whatever"]=123returnfrozendict(mutable)

@vstinnervstinner changed the titlegh-141510: Add PyFrozenDict_AsDict() functiongh-141510: Add PyAnyDict_AsNewDict() functionMar 5, 2026
@vstinner
Copy link
MemberAuthor

Ok, I renamed the function toPyAnyDict_AsNewDict(). It acceptsdict andfrozendict and creates a newdict.

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

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

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

Reviewers

@corona10corona10corona10 left review comments

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

@methanemethaneAwaiting requested review from methanemethane is a code owner

@ZeroIntensityZeroIntensityAwaiting requested review from ZeroIntensityZeroIntensity is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@vstinner@encukou@ZeroIntensity@corona10

[8]ページ先頭

©2009-2026 Movatter.jp