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: Always return a dict in PyDict_Copy()#145517

Open
vstinner wants to merge 3 commits intopython:mainfrom
vstinner:dict_copy2
Open

gh-141510: Always return a dict in PyDict_Copy()#145517
vstinner wants to merge 3 commits intopython:mainfrom
vstinner:dict_copy2

Conversation

@vstinner
Copy link
Member

@vstinnervstinner commentedMar 4, 2026
edited by github-actionsbot
Loading

  • PyDict_Copy() now also returns a dict if the argument is a frozendict.
  • Remove _PyDict_CopyAsDict() function.
  • Fix frozendict.items() ^ frozendict.items(). Add non-regression test.

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

* PyDict_Copy() now also returns a dict if the argument is a  frozendict.* Remove _PyDict_CopyAsDict() function.* Fix frozendict.items() ^ frozendict.items(). Add non-regression  test.
@vstinner
Copy link
MemberAuthor

I propose changingPyDict_Copy() API to always return adict. Previously, it returned afrozendict if it was called on afrozendict. IMO it's less surprising and more convenient that PyDict_Copy() always return a copy (as adict).

Example intypeobject.c:

staticPyTypeObject*type_new_init(type_new_ctx*ctx){PyObject*dict=PyDict_Copy(ctx->orig_dict);if (dict==NULL) {        gotoerror;    }        ...set_tp_dict(type,dict);    ...}

The function acceptsdict orfrozendict but the type dictionary (dict variable) must be adict.

Example inModules/_elementtree.c:

staticPyObject*get_attrib_from_keywords(PyObject*kwds){    ...if (attrib) {        ...Py_SETREF(attrib,PyDict_Copy(attrib));    }else {attrib=PyDict_New();    }if (attrib!=NULL&&PyDict_Update(attrib,kwds)<0) {Py_DECREF(attrib);returnNULL;    }returnattrib;}

(With my pending change PRgh-145508), the function acceptsdict orfrozendict but the result must be adict (for example, we callPyDict_Update() on it).

cc@corona10@ZeroIntensity@eendebakpt

@vstinner
Copy link
MemberAuthor

Currently,PyDict_Copy(frozendict) returns afrozendict copy which is something used internally tomodify an immutablefrozendict! But I don't think that we should leak such implementation details to the public C API.

Existing C extensions callingPyDict_Copy() expect it to return adict. If it can return afrozendict on Python 3.15, it can trigger bugs.

Always returningdict avoids these issues.

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

Reviewers

@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.

1 participant

@vstinner

[8]ページ先頭

©2009-2026 Movatter.jp