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: raise RuntimeError's when calling methods on non-ready Struct()'s#143643

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

Conversation

@skirpichev
Copy link
Member

@skirpichevskirpichev commentedJan 10, 2026
edited
Loading

Now modification of the Struct() while packing trigger a RuntimeError
…y Struct()'sCalling the ``Struct.__new__()`` dunder without required argument now isdeprecated.  Calling the ``Struct.__init__()`` dunder method oninitialized object now also is deprecated.
@skirpichev

This comment was marked as outdated.

@skirpichevskirpichev marked this pull request as ready for reviewJanuary 10, 2026 07:55
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.

This issue is relatively independed from the concurrent packing issue, so I suggest fir it separately. We should not add deprecation in maintained versions, so I suggest to only leave checks that Struct was initialized, and add the deprecation in a separate PR.

prepare_s() should be rewritten so it only setss_codes and other fields when it is successful.

{
PyObject *self;

if (PyTuple_GET_SIZE(args) != 1

Choose a reason for hiding this comment

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

I do not see how can this be useful.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

This is to trigger deprecation warning in a following scenario:

>>>import struct>>>classMyStruct(struct.Struct):...def__init__(self):...super().__init__('>h')...         >>> my_struct= MyStruct()<python-input-2>:1: DeprecationWarning: Struct().__new__() has one required argument

PS: deprecation part moved to#143659.

@serhiy-storchakaserhiy-storchaka added needs backport to 3.13bugs and security fixes needs backport to 3.14bugs and security fixes labelsJan 10, 2026
@skirpichevskirpichev marked this pull request as draftJanuary 10, 2026 10:14
@skirpichev
Copy link
MemberAuthor

prepare_s() should be rewritten so it only sets s_codes and other fields when it is successful.

I think it's already the case. We can move a bit s_size/s_len assignments, but as far as we interested only in s_codes - I'm not sure if it's worth. There should be no failures aftercodes = PyMem_Malloc(...).

@skirpichevskirpichev marked this pull request as ready for reviewJanuary 10, 2026 11:45
@serhiy-storchaka
Copy link
Member

Well, it does not matter. It would reduce the chance of race condition, but nobody should run__init__() andpack() concurrently, there are no reasons to do this, and we should not support this.

self.assertRaises(RuntimeError, meth, spam)
self.assertRaises(RuntimeError, S.iter_unpack, 1)
self.assertRaises(RuntimeError, S.pack, 1)
self.assertRaises(RuntimeError, S.pack_into, 1)

Choose a reason for hiding this comment

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

S.pack_into(buffer, offset, v1, v2, ...)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

BTW, I think that argument processing in the struct's functions/methods should be refactored and transformed to the AC. E.g. S.pack_info could check required arguments before validation of the soself struct.

Though, this belongs to a separate issue.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

AC cleanup goes to#143673

@skirpichev
Copy link
MemberAuthor

Well, it does not matter. It would reduce the chance of race condition, but nobody should run__init__() andpack() concurrently, there are no reasons to do this, and we should not support this.

I guess this belongs to the#143382 pr thread.

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

@serhiy-storchakaserhiy-storchakaenabled auto-merge (squash)January 11, 2026 15:47
@serhiy-storchakaserhiy-storchaka merged commit515ae40 intopython:mainJan 11, 2026
50 checks passed
@miss-islington-app
Copy link

Thanks@skirpichev for the PR, and@serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestJan 11, 2026
…y Struct()'s (pythonGH-143643)(cherry picked from commit515ae40)Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
@miss-islington-app
Copy link

Sorry,@skirpichev and@serhiy-storchaka, I could not cleanly backport this to3.13 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 515ae4078dffa0b74e5e5431462c2f4fe4563ffa 3.13

@bedevere-app
Copy link

GH-143695 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelJan 11, 2026
@skirpichevskirpichev deleted the deprecate-struct-init/78724 branchJanuary 11, 2026 18:04
@skirpichevskirpichev self-assigned thisJan 11, 2026
@bedevere-app
Copy link

GH-143714 is a backport of this pull request to the3.13 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.13bugs and security fixes labelJan 11, 2026
@skirpichevskirpichev removed their assignmentJan 11, 2026
serhiy-storchaka pushed a commit that referenced this pull requestJan 12, 2026
…dy Struct()'s (GH-143643) (GH-143695)(cherry picked from commit515ae40)Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
serhiy-storchaka pushed a commit that referenced this pull requestJan 12, 2026
reidenong pushed a commit to reidenong/cpython that referenced this pull requestJan 12, 2026
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

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@skirpichev@serhiy-storchaka

[8]ページ先頭

©2009-2026 Movatter.jp