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-78724: Initialize struct.Struct in __new__ by kumaraditya303 · Pull Request #94532 · python/cpython

Conversation

@kumaraditya303

@kumaraditya303 kumaraditya303 commented Jul 3, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Closes #75960
Closes #78724

  • Issue: _struct.Struct: calling functions without calling __init__ results in SystemError #78724

Sorry, something went wrong.

Uh oh!

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

All reactions
kumaraditya303 added 3 commits July 3, 2022 14:29
@kumaraditya303
initialize Struct in __new__
4583625
@kumaraditya303
fix ref leaks
a026273
@kumaraditya303
use PyType_GetSlot
f23671c
@bedevere-bot bedevere-bot added the awaiting review label Jul 3, 2022
@blurb-it
📜🤖 Added by blurb_it.
d44201f
@kumaraditya303 kumaraditya303 added needs backport to 3.10 extension-modules C modules in the Modules dir needs backport to 3.11 only security fixes labels Jul 3, 2022
@kumaraditya303
fix typo
a74cdbb
@kumaraditya303 kumaraditya303 added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 3, 2022
@bedevere-bot

bedevere-bot commented Jul 3, 2022

Copy link
Copy Markdown

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

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

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 3, 2022
@kumaraditya303
kumaraditya303 requested a review from mdickinson July 3, 2022 16:52
ambv
ambv approved these changes Jul 5, 2022
View reviewed changes

@ambv ambv left a comment

Copy link
Copy Markdown
Contributor

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.

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.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels Jul 5, 2022
@ambv ambv assigned mdickinson Jul 5, 2022
@mdickinson

mdickinson commented Jul 5, 2022

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

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

@mdickinson

mdickinson commented Jul 5, 2022

Copy link
Copy Markdown
Member

LGTM too.

To be on the safe side, I'd recommend not 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 of Struct. 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.

All reactions

Sorry, something went wrong.

Uh oh!

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

mdickinson
mdickinson approved these changes Jul 5, 2022
View reviewed changes
@mdickinson

mdickinson commented Jul 5, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

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:

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

But it breaks on this branch:

>>> import struct
>>> class Bob(struct.Struct):
...     def __init__(self, format):
...         super().__init__(format)
... 
>>> b = Bob("!f")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __init__
TypeError: object.__init__() takes exactly one argument (the instance to initialize)

I'm finding it hard to imagine why anyone would be subclassing Struct 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.

All reactions

Sorry, something went wrong.

Uh oh!

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

@pablogsal

pablogsal commented Jul 5, 2022

Copy link
Copy Markdown
Member

To be on the safe side, I'd recommend not 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.

All reactions

Sorry, something went wrong.

Uh oh!

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

@mdickinson

mdickinson commented Jul 5, 2022

Copy link
Copy Markdown
Member

@kumaraditya303 Apologies, Kumar; I'm having second thoughts here. While common sense says that no-one will be subclassing struct.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?

All reactions

Sorry, something went wrong.

Uh oh!

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

mdickinson
mdickinson suggested changes Jul 5, 2022
View reviewed changes

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

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

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting changes and removed awaiting merge labels Jul 5, 2022
@bedevere-bot

bedevere-bot commented Jul 5, 2022

Copy link
Copy Markdown

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 phrase I 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.

All reactions

Sorry, something went wrong.

Uh oh!

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

@kumaraditya303
backwards compatibility
1306df3
@kumaraditya303

kumaraditya303 commented Jul 6, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot removed the awaiting changes label Jul 6, 2022
@kumaraditya303
remove __init__
d7ce7d8
@kumaraditya303
kumaraditya303 requested a review from mdickinson September 17, 2022 12:10
@kumaraditya303

kumaraditya303 commented Sep 25, 2022

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

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

mdickinson
mdickinson approved these changes Sep 25, 2022
View reviewed changes

@mdickinson mdickinson left a comment •
edited
Loading

Uh oh!

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

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.

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

Sorry, something went wrong.

Uh oh!

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

kumaraditya303 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting change review labels Sep 25, 2022
@mdickinson mdickinson added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 25, 2022
@bedevere-bot

bedevere-bot commented Sep 25, 2022

Copy link
Copy Markdown

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

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

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 25, 2022
@mdickinson

mdickinson commented Sep 25, 2022

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

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

@mdickinson
mdickinson merged commit c8c0afc into python:main Sep 25, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Sep 25, 2022
@kumaraditya303 kumaraditya303 mentioned this pull request Sep 25, 2022
bpo-31779: Prevent assertion failures and a crash when using an uninitialized struct.Struct object #3984
Closed
@kumaraditya303
kumaraditya303 deleted the struct-78724 branch September 25, 2022 16:50
@kumaraditya303 kumaraditya303 mentioned this pull request Nov 25, 2022
bpo-34543: Fix SystemErrors and segfaults with uninitialized Structs #14777
Closed
@dopplershift

dopplershift commented Oct 12, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

So I just hit this while trying to upgrade to 3.12. While subclassing works, subclasses MUST now match the same signature for __init__:

from struct import Struct

class FloatStruct(Struct):
    def __init__(self):
        super().__init__('f')

f = FloatStruct()

gives

Traceback (most recent call last):
  File "/Users/rmay/test.py", line 7, 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.

All reactions

Sorry, something went wrong.

Uh oh!

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

@dopplershift dopplershift mentioned this pull request Oct 12, 2023
Support Python 3.12 Unidata/MetPy#3223
Merged
1 task done
@AlexWaygood AlexWaygood mentioned this pull request Nov 24, 2023
struct.Struct inheritance with Python 3.12.0 #112358
Closed
mdickinson added a commit to mdickinson/cpython that referenced this pull request Nov 26, 2023
@mdickinson
Revert "pythonGH-78724: Initialize struct.Struct in __new__ (pythonGH…
7190138
…-94532)"

This reverts commit c8c0afc.
@mdickinson mdickinson mentioned this pull request Nov 26, 2023
gh-112358: Fix Python 3.12 regression with subclassing struct.Struct. #112424
Merged
mdickinson added a commit that referenced this pull request Nov 26, 2023
@mdickinson
gh-112358: Fix Python 3.12 regression with subclassing struct.Struct. (…
9fe6034
…#112424)

Revert commit c8c0afc (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 mdickinson mentioned this pull request Nov 26, 2023
[3.12] gh-112358: Fix Python 3.12 regression with subclassing struct.Struct (GH-112424) #112426
Merged
mdickinson added a commit to mdickinson/cpython that referenced this pull request Nov 26, 2023
@mdickinson
[3.12] pythongh-112358: Fix Python 3.12 regression with subclassing s…
72d90d0
…truct.Struct. (pythonGH-112424)

Revert commit c8c0afc (PR pythonGH-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 commit 9fe6034)

Co-authored-by: Mark Dickinson <[email protected]>
mdickinson added a commit that referenced this pull request Nov 27, 2023
@mdickinson
[3.12] gh-112358: Fix Python 3.12 regression with subclassing struct.…
42df736
…Struct (GH-112424) (#112426)

* [3.12] gh-112358: Fix Python 3.12 regression with subclassing struct.Struct. (GH-112424)

Revert commit c8c0afc (PR GH-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 commit 9fe6034)

Co-authored-by: Mark Dickinson <[email protected]>

* Remove unrelated test
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
@mdickinson @aisk
pythongh-112358: Fix Python 3.12 regression with subclassing struct.S…
6d24e80
…truct. (python#112424)

Revert commit c8c0afc (PR python#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 request Sep 2, 2024
@mdickinson @Glyphack
pythongh-112358: Fix Python 3.12 regression with subclassing struct.S…
6aabe84
…truct. (python#112424)

Revert commit c8c0afc (PR python#94532),
which moved `struct.Struct` initialisation from `Struct.__init__` to `Struct.__new__`.
This caused issues with code in the wild that subclasses `struct.Struct`.
@gvanrossum gvanrossum mentioned this pull request Dec 12, 2025
Disallow multiple initializations of asyncio tasks and futures #142615
Closed
This was referenced Jan 10, 2026
gh-143715: deprecate incomplete initialization of struct.Struct() #143659
Closed
Make struct.Struct() really immutable #143715
Closed
skirpichev pushed a commit to skirpichev/cpython that referenced this pull request Feb 28, 2026
@kumaraditya303 @skirpichev
pythonGH-78724: Initialize struct.Struct in __new__ (pythonGH-94532)
be9d0d8
Closes python#75960
Closes python#78724
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ambv ambv Awaiting requested review from ambv
1 more reviewer
@mdickinson mdickinson mdickinson approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

@mdickinson mdickinson

Labels

extension-modules C modules in the Modules dir

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

_struct.Struct: calling functions without calling __init__ results in SystemError assertion failures and a crash when using an uninitialized struct.Struct object

Uh oh!

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

6 participants

@kumaraditya303 @bedevere-bot @mdickinson @pablogsal @dopplershift @ambv
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.