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-78724: Initialize struct.Struct in __new__#94532

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
mdickinson merged 8 commits intopython:mainfromkumaraditya303:struct-78724
Sep 25, 2022

Conversation

@kumaraditya303
Copy link
Contributor

@kumaraditya303kumaraditya303 commentedJul 3, 2022
edited
Loading

@kumaraditya303kumaraditya303 added needs backport to 3.10only security fixes extension-modulesC modules in the Modules dir needs backport to 3.11only security fixes labelsJul 3, 2022
@kumaraditya303kumaraditya303 added the 🔨 test-with-buildbotsTest PR w/ buildbots; report in status section labelJul 3, 2022
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by@kumaraditya303 for commita74cdbb 🤖

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

@bedevere-botbedevere-bot removed the 🔨 test-with-buildbotsTest PR w/ buildbots; report in status section labelJul 3, 2022
Copy link
Contributor

@ambvambv left a comment

Choose a reason for hiding this comment

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

LGTM. I'm wondering if moving initialization from__init__ to__new__ can cause some user-facing backwards compatibility, say, wrt subclassing or something. I asked for more eyes on this.

@mdickinson
Copy link
Member

Apologies for the silence; I'll get to this by the end of this weekend.

@mdickinson
Copy link
Member

LGTM too.

To be on the safe side, I'd recommendnot backporting this to 3.10, though it seems fine to backport to 3.11 if@pablogsal agrees. The bugs being fixed seem to be of the self-inflicted "well don't do that then" variety, so while it's definitely nice to have them fixed, it seems unlikely that they'll be blocking any real users ofStruct. And there's just enough possibility of unintended side-effects that it doesn't seem worth changing this in an already-released version of Python.

@mdickinson
Copy link
Member

mdickinson commentedJul 5, 2022
edited
Loading

LGTM. I'm wondering if moving initialization from__init__ to__new__ can cause some user-facing backwards compatibility, say, wrt subclassing or something.

Yes, it can. This is fine, on Python 3.10:

>>>importstruct>>>classBob(struct.Struct):...def__init__(self,format):...super().__init__(format)...>>>b=Bob("!f")>>>b.pack(2.3)b'@\x1333'

But it breaks on this branch:

>>>importstruct>>>classBob(struct.Struct):...def__init__(self,format):...super().__init__(format)...>>>b=Bob("!f")Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>File"<stdin>",line3,in__init__TypeError:object.__init__()takesexactlyoneargument (theinstancetoinitialize)

I'm finding it hard to imaginewhy anyone would be subclassingStruct like this, so it doesn't seem like a real issue if the behaviour changes in 3.11 or 3.12 (and in particular this certainly doesn't seem worth a deprecation period), but it does seem like sufficient reason to be careful in 3.10.

@pablogsal
Copy link
Member

To be on the safe side, I'd recommendnot backporting this to 3.10, though it seems fine to backport to 3.11 if@pablogsal agrees.

I think is ok to backport to 3.11 👍 Not sure if it makes sense to mention the subclassing incompatibility in the what's new, though.

@mdickinson
Copy link
Member

@kumaraditya303 Apologies, Kumar; I'm having second thoughts here. While common sense says that no-one will be subclassingstruct.Struct this way, experience says that someone somewhere will be, and it would be at least a bit irresponsible to change the behaviour.

Can you think of any ways that we could fix these bugs without the backward compatibility breakage?

Copy link
Member

@mdickinsonmdickinson left a comment

Choose a reason for hiding this comment

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

On balance, the backwards compatibility change seems like something we should avoid if possible.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@kumaraditya303
Copy link
ContributorAuthor

kumaraditya303 commentedJul 6, 2022
edited
Loading

Thanks for the reviews!

Can you think of any ways that we could fix these bugs without the backward compatibility breakage?

I have made it backwards compatible by adding__init__ and added a test for subclassing.

I have made the requested changes; please review again@mdickinson@ambv

@kumaraditya303
Copy link
ContributorAuthor

Thanks for the review, I'll make the changes by next weekend.

mdickinson reacted with thumbs up emoji

@kumaraditya303kumaraditya303 removed needs backport to 3.10only security fixes needs backport to 3.11only security fixes labelsSep 17, 2022
@kumaraditya303
Copy link
ContributorAuthor

cc@mdickinson I have made the changes, I removed the code duplication and it is now only for 3.12+.

Copy link
Member

@mdickinsonmdickinson left a comment
edited
Loading

Choose a reason for hiding this comment

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

LGTM. I'll do a buildbot run, to be on the safe side.

kumaraditya303 reacted with thumbs up emoji
@mdickinsonmdickinson added the 🔨 test-with-buildbotsTest PR w/ buildbots; report in status section labelSep 25, 2022
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by@mdickinson for commitd7ce7d8 🤖

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

@bedevere-botbedevere-bot removed the 🔨 test-with-buildbotsTest PR w/ buildbots; report in status section labelSep 25, 2022
@mdickinson
Copy link
Member

The buildbots look happy. There are still three checks pending as of writing, but enough checks have passed to satisfy me. I'll merge now, but continue to watch those remaining three bots.

@dopplershift
Copy link

dopplershift commentedOct 12, 2023
edited
Loading

So I just hit this while trying to upgrade to 3.12. While subclassing works, subclassesMUST now match the same signature for__init__:

fromstructimportStructclassFloatStruct(Struct):def__init__(self):super().__init__('f')f=FloatStruct()

gives

Traceback (most recent call last):  File"/Users/rmay/test.py", line7, in<module>    f= FloatStruct()^^^^^^^^^^^^^TypeError:Struct() missing required argument 'format' (pos 1)

My actual use case was passing additional arguments, but either way fails.

I'm not here to necessarily vouch for why our implementation uses that, but it was seriously confusing not understanding why__init__ arguments were being checked before getting to my own__init__ implementation.

mdickinson added a commit to mdickinson/cpython that referenced this pull requestNov 26, 2023
mdickinson added a commit that referenced this pull requestNov 26, 2023
…#112424)Revert commitc8c0afc (PR#94532),which moved `struct.Struct` initialisation from `Struct.__init__` to `Struct.__new__`.This caused issues with code in the wild that subclasses `struct.Struct`.
mdickinson added a commit to mdickinson/cpython that referenced this pull requestNov 26, 2023
…truct.Struct. (pythonGH-112424)Revert commitc8c0afc (PRpythonGH-94532),which moved `struct.Struct` initialisation from `Struct.__init__` to `Struct.__new__`.This caused issues with code in the wild that subclasses `struct.Struct`..(cherry picked from commit9fe6034)Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
mdickinson added a commit that referenced this pull requestNov 27, 2023
…Struct (GH-112424) (#112426)* [3.12]gh-112358: Fix Python 3.12 regression with subclassing struct.Struct. (GH-112424)Revert commitc8c0afc (PRGH-94532),which moved `struct.Struct` initialisation from `Struct.__init__` to `Struct.__new__`.This caused issues with code in the wild that subclasses `struct.Struct`..(cherry picked from commit9fe6034)Co-authored-by: Mark Dickinson <dickinsm@gmail.com>* Remove unrelated test
aisk pushed a commit to aisk/cpython that referenced this pull requestFeb 11, 2024
…truct. (python#112424)Revert commitc8c0afc (PRpython#94532),which moved `struct.Struct` initialisation from `Struct.__init__` to `Struct.__new__`.This caused issues with code in the wild that subclasses `struct.Struct`.
Glyphack pushed a commit to Glyphack/cpython that referenced this pull requestSep 2, 2024
…truct. (python#112424)Revert commitc8c0afc (PRpython#94532),which moved `struct.Struct` initialisation from `Struct.__init__` to `Struct.__new__`.This caused issues with code in the wild that subclasses `struct.Struct`.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@ambvambvAwaiting requested review from ambv

1 more reviewer

@mdickinsonmdickinsonmdickinson approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

@mdickinsonmdickinson

Labels

extension-modulesC modules in the Modules dir

Projects

None yet

Milestone

No milestone

6 participants

@kumaraditya303@bedevere-bot@mdickinson@pablogsal@dopplershift@ambv

[8]ページ先頭

©2009-2025 Movatter.jp