Skip to content

Commit 3f36635

Browse files
committed
a hack to support new idiom for subclassing
This make format argument in the __init__() - optional. If it's missing, the object must be already initialized in __new__().
1 parent 0e4e84b commit 3f36635

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Modules/_struct.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,7 +1818,7 @@ s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
18181818
/*[clinic input]
18191819
Struct.__init__
18201820
1821-
format: object
1821+
format: object = NULL
18221822
18231823
Create a compiled struct object.
18241824
@@ -1830,8 +1830,13 @@ See help(struct) for more on format strings.
18301830

18311831
static int
18321832
Struct___init___impl(PyStructObject *self, PyObject *format)
1833-
/*[clinic end generated code: output=b8e80862444e92d0 input=192a4575a3dde802]*/
1833+
/*[clinic end generated code: output=b8e80862444e92d0 input=14845875ad162992]*/
18341834
{
1835+
if (!format && !self->s_codes) {
1836+
PyErr_SetString(PyExc_TypeError,
1837+
"Struct() missing required argument 'format' (pos 1)");
1838+
return -1;
1839+
}
18351840
if (!self->init_called) {
18361841
if (!self->s_codes && actual___init___impl(self, format)) {
18371842
return -1;

Modules/clinic/_struct.c.h

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)