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-105736: Sync pure python version of OrderedDict with the C version#108098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ambv merged 8 commits intopython:mainfromrhettinger:od_new
Aug 21, 2023

Conversation

rhettinger
Copy link
Contributor

@rhettingerrhettinger commentedAug 17, 2023
edited by bedevere-bot
Loading

This solves a problem noticed in the referenced issue. When__init__ is overridden, the C version is still usable but the pure Python version fails. The solution is to have the internal invariants sets up in__new__ rather than__init__.

The original copy/pickle issue still remains.

@rhettingerrhettinger added the 🔨 test-with-buildbotsTest PR w/ buildbots; report in status section labelAug 17, 2023
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by@rhettinger for commit4f04b7e 🤖

If you want to schedule another build, you need to add the🔨 test-with-buildbots label again.

@bedevere-botbedevere-bot removed the 🔨 test-with-buildbotsTest PR w/ buildbots; report in status section labelAug 17, 2023
@rhettingerrhettinger added the 🔨 test-with-buildbotsTest PR w/ buildbots; report in status section labelAug 17, 2023
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by@rhettinger for commitd3179bc 🤖

If you want to schedule another build, you need to add the🔨 test-with-buildbots label again.

@bedevere-botbedevere-bot removed the 🔨 test-with-buildbotsTest PR w/ buildbots; report in status section labelAug 17, 2023
@ambv
Copy link
Contributor

I can confirm the new tests fail without the fix 👍🏻

One thing I'm trying to find is why we needed theAttributeError check forself.__root before. Since this is an double-underscore attribute, intuition tells me the only case when this can happen is unpickling. But when we provideself.__class__ as an "empty object factory" in the result ofself.reduce, that will call__init__() first before setting the state, soself.__root won't exist at pickling and unpickling time either. And in fact, I can reproduce that it isn't ever present in those times, for protocols 3 - 5.

So I guess skipping thetry:except: is harmless. But I wonder if I'm missing something.

@rhettinger
Copy link
ContributorAuthor

rhettinger commentedAug 18, 2023
edited
Loading

@ambv

One thing I'm trying to find is why we needed the AttributeError check for self.__root before.

The__init__ method can be called more than once for an instance and it must add on to existing state rather than reset it from scratch. Here's an example with both regular dictionaries and OrderedDicts:

>>> d = dict(a=1, b=2)>>> d.__init__(b=3, c=4)>>> d{'a': 1, 'b': 3, 'c': 4}>>> d = OrderedDict(a=1, b=2)>>> d.__init__(b=3, c=4)>>> dOrderedDict({'a': 1, 'b': 3, 'c': 4})

In contrast, the__new__ method can only be called once because it creates the actual instance. Hence, thetry/except is no longer needed — the attribute cannot already exist.

ambv reacted with thumbs up emoji

@ambv
Copy link
Contributor

By whom can it be called multiple times? I mean, yes, theoretically you can explicitly grab__init__() from an existing instance and call it again but is this a pattern in actual use anywhere? I haven't seen this before.

(yes, I agree that__new__() can skip thetry:except:.)

@serhiy-storchakaserhiy-storchaka changed the titleGH-105736 Sync pure python version of OrderedDict with the C versiongh-105736: Sync pure python version of OrderedDict with the C versionAug 21, 2023
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. And the resulting code looks clearer.

Moving the code from__init__ to__new__ can break pickling, but OrderedDict uses full constructor instead of__new__ in pickling, so it is safe from this side.

ambv reacted with thumbs up emoji
@miss-islington
Copy link
Contributor

Thanks@rhettinger for the PR, and@ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-108200 is a backport of this pull request to the3.12 branch.

@bedevere-botbedevere-bot removed the needs backport to 3.12only security fixes labelAug 21, 2023
@bedevere-bot
Copy link

GH-108201 is a backport of this pull request to the3.11 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestAug 21, 2023
…ersion (pythonGH-108098)(cherry picked from commit20cc90c)Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestAug 21, 2023
…ersion (pythonGH-108098)(cherry picked from commit20cc90c)Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
@bedevere-botbedevere-bot removed the needs backport to 3.11only security fixes labelAug 21, 2023
@serhiy-storchaka
Copy link
Member

Side note. "gh-" in the title should be in lower case, and the issue number should be separated by a colon from the rest of the text. I think this is the reason why other Raymond's PRs were not automatically linked from the corresponding issue.

serhiy-storchaka pushed a commit that referenced this pull requestAug 21, 2023
…version (GH-108098) (GH-108201)(cherry picked from commit20cc90c)Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
Yhg1s pushed a commit that referenced this pull requestAug 21, 2023
…version (GH-108098) (#108200)gh-105736: Sync pure python version of OrderedDict with the C version (GH-108098)(cherry picked from commit20cc90c)Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

@ericsnowcurrentlyericsnowcurrentlyAwaiting requested review from ericsnowcurrently

Assignees
No one assigned
Labels
type-bugAn unexpected behavior, bug, or error
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@rhettinger@bedevere-bot@ambv@miss-islington@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp