-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-143715: Deprecate incomplete initialization of struct.Struct() #145580
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
serhiy-storchaka
merged 38 commits into
python:main
from
serhiy-storchaka:deprecate-struct-init
Mar 12, 2026
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
fff147f
gh-78724: deprecate incomplete initialization of struct.Struct()
skirpichev d2a0345
Merge branch 'master' into deprecate-struct-init-2/78724
skirpichev 589fbc7
address review: test_Struct_reinitialization()
skirpichev 5e91e87
catch new warning in test_operations_on_half_initialized_Struct()
skirpichev babb274
add init_called flag
skirpichev 0e4e84b
Merge branch 'master' into deprecate-struct-init-2/78724
skirpichev 3f36635
a hack to support new idiom for subclassing
skirpichev e576475
Merge branch 'master' into deprecate-struct-init-2/78724
skirpichev 628aadd
+ filter out Struct signature test
skirpichev 979cc18
Update Modules/_struct.c
skirpichev f7492ec
Merge branch 'master' into deprecate-struct-init-2/78724
skirpichev db8f5f2
address review: reformat s_new()
skirpichev dc8cbef
address review: actual___init___impl -> s_init
skirpichev 6206ae1
Merge branch 'master' into deprecate-struct-init-2/78724
skirpichev 12143d1
Update Doc/deprecations/pending-removal-in-3.20.rst
skirpichev d4ca6b8
Merge branch 'master' into deprecate-struct-init-2/78724
skirpichev 6b9b4fb
+ rename news
skirpichev 79961a2
Apply suggestions from code review
skirpichev 58550c0
address review: add test
skirpichev c815882
address review: fix for format
skirpichev f1388ee
address review: reformat if blocks
skirpichev 685a6c4
address review: s_new
skirpichev 76f8723
Merge branch 'master' into deprecate-struct-init-2/78724
skirpichev 538d301
address review: add fake signature for Struct
skirpichev 09d6ebd
+test new idiom
skirpichev 7f0a133
Some cleanup; also add reference to issue and reverted PR
skirpichev 26cde89
Merge branch 'master' into deprecate-struct-init-2/78724
skirpichev 7697820
issue DeprecationWarning for implicit tp_new calls in subclasses
skirpichev 05eb292
Fix more corner cases.
serhiy-storchaka d9c0488
Update credicts.
serhiy-storchaka 3de045b
Fix format for the __sizeof__ test.
serhiy-storchaka ebcf358
Fix test_Struct_reinitialization on big-endian and add tests for inva…
serhiy-storchaka e003dab
Add assertions for format.
serhiy-storchaka d453990
Factorize some tests.
serhiy-storchaka 66dbc04
Merge branch 'main' into deprecate-struct-init
serhiy-storchaka 6d60b16
Update Doc/deprecations/pending-removal-in-3.20.rst
serhiy-storchaka d7da0f8
Merge branch 'main' into deprecate-struct-init
serhiy-storchaka ca960a8
Emit FutureWarning instead of DeprecationWarning for re-initialization.
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add init_called flag
- Loading branch information
commit babb274bfe004bdaed46c2437a5e1ab25aa1842a
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,7 @@ typedef struct { | |
| formatcode *s_codes; | ||
| PyObject *s_format; | ||
| PyObject *weakreflist; /* List of weak references */ | ||
| bool init_called; | ||
| } PyStructObject; | ||
|
|
||
| #define PyStructObject_CAST(op) ((PyStructObject *)(op)) | ||
|
|
@@ -1757,30 +1758,61 @@ prepare_s(PyStructObject *self) | |
| return -1; | ||
| } | ||
|
|
||
| static int | ||
| actual___init___impl(PyStructObject *self, PyObject *format) | ||
| { | ||
| if (PyUnicode_Check(format)) { | ||
| format = PyUnicode_AsASCIIString(format); | ||
| if (format == NULL) | ||
| return -1; | ||
| } | ||
| else { | ||
| Py_INCREF(format); | ||
| } | ||
| if (!PyBytes_Check(format)) { | ||
| Py_DECREF(format); | ||
| PyErr_Format(PyExc_TypeError, | ||
| "Struct() argument 1 must be a str or bytes object, " | ||
| "not %.200s", | ||
| _PyType_Name(Py_TYPE(format))); | ||
| return -1; | ||
| } | ||
| Py_SETREF(self->s_format, format); | ||
| if (prepare_s(self)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of restoring s_format to its previous value if |
||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| static PyObject * | ||
| s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||
| { | ||
| PyObject *self; | ||
| PyStructObject *self; | ||
|
|
||
| if (PyTuple_GET_SIZE(args) != 1 | ||
| if ((PyTuple_GET_SIZE(args) != 1 || kwds) | ||
| && PyErr_WarnEx(PyExc_DeprecationWarning, | ||
| "Struct().__new__() has one required argument", 1)) | ||
| "Struct.__new__() has one positional argument", 1)) | ||
| { | ||
| return NULL; | ||
| } | ||
| assert(type != NULL); | ||
| allocfunc alloc_func = PyType_GetSlot(type, Py_tp_alloc); | ||
| assert(alloc_func != NULL); | ||
|
|
||
| self = alloc_func(type, 0); | ||
| self = (PyStructObject *)alloc_func(type, 0); | ||
| if (self != NULL) { | ||
| PyStructObject *s = (PyStructObject*)self; | ||
| s->s_format = Py_NewRef(Py_None); | ||
| s->s_codes = NULL; | ||
| s->s_size = -1; | ||
| s->s_len = -1; | ||
| self->s_format = Py_NewRef(Py_None); | ||
| self->s_codes = NULL; | ||
| self->s_size = -1; | ||
| self->s_len = -1; | ||
| self->init_called = false; | ||
| if (PyTuple_GET_SIZE(args) > 0) { | ||
| if (actual___init___impl(self, PyTuple_GET_ITEM(args, 0))) { | ||
| Py_DECREF(self); | ||
| return NULL; | ||
| } | ||
| } | ||
| } | ||
| return self; | ||
| return (PyObject *)self; | ||
| } | ||
|
|
||
| /*[clinic input] | ||
|
|
@@ -1800,37 +1832,21 @@ static int | |
| Struct___init___impl(PyStructObject *self, PyObject *format) | ||
| /*[clinic end generated code: output=b8e80862444e92d0 input=192a4575a3dde802]*/ | ||
| { | ||
| int ret = 0; | ||
|
|
||
| if (self->s_codes | ||
| if (!self->init_called) { | ||
| if (!self->s_codes && actual___init___impl(self, format)) { | ||
| return -1; | ||
| } | ||
| self->init_called = true; | ||
| return 0; | ||
| } | ||
| if ((self->s_codes && self->init_called) | ||
| && PyErr_WarnEx(PyExc_DeprecationWarning, | ||
| ("Explicit call of __init__() on " | ||
| "initialized Struct() is deprecated"), 1)) | ||
| { | ||
| return -1; | ||
| } | ||
| if (PyUnicode_Check(format)) { | ||
| format = PyUnicode_AsASCIIString(format); | ||
| if (format == NULL) | ||
| return -1; | ||
| } | ||
| else { | ||
| Py_INCREF(format); | ||
| } | ||
|
|
||
| if (!PyBytes_Check(format)) { | ||
| Py_DECREF(format); | ||
| PyErr_Format(PyExc_TypeError, | ||
| "Struct() argument 1 must be a str or bytes object, " | ||
| "not %.200s", | ||
| _PyType_Name(Py_TYPE(format))); | ||
| return -1; | ||
| } | ||
|
|
||
| Py_SETREF(self->s_format, format); | ||
|
|
||
| ret = prepare_s(self); | ||
| return ret; | ||
| return actual___init___impl(self, format); | ||
| } | ||
|
|
||
| static int | ||
|
|
@@ -2473,9 +2489,7 @@ static PyType_Slot PyStructType_slots[] = { | |
| {Py_tp_members, s_members}, | ||
| {Py_tp_getset, s_getsetlist}, | ||
| {Py_tp_init, Struct___init__}, | ||
| {Py_tp_alloc, PyType_GenericAlloc}, | ||
| {Py_tp_new, s_new}, | ||
| {Py_tp_free, PyObject_GC_Del}, | ||
| {0, 0}, | ||
| }; | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
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.
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.
It should be
'2n3P?0P', with padding.