Skip to content
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

gh-84436: Add codemod for _PyVarObject_HEAD_IMMORTAL_INIT #103308

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/includes/custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ typedef struct {
} CustomObject;

static PyTypeObject CustomType = {
PyVarObject_HEAD_INIT(NULL, 0)
_PyVarObject_HEAD_IMMORTAL_INIT(NULL, 0)
.tp_name = "custom.Custom",
.tp_doc = PyDoc_STR("Custom objects"),
.tp_basicsize = sizeof(CustomObject),
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/custom2.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static PyMethodDef Custom_methods[] = {
};

static PyTypeObject CustomType = {
PyVarObject_HEAD_INIT(NULL, 0)
_PyVarObject_HEAD_IMMORTAL_INIT(NULL, 0)
.tp_name = "custom2.Custom",
.tp_doc = PyDoc_STR("Custom objects"),
.tp_basicsize = sizeof(CustomObject),
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/custom3.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static PyMethodDef Custom_methods[] = {
};

static PyTypeObject CustomType = {
PyVarObject_HEAD_INIT(NULL, 0)
_PyVarObject_HEAD_IMMORTAL_INIT(NULL, 0)
.tp_name = "custom3.Custom",
.tp_doc = PyDoc_STR("Custom objects"),
.tp_basicsize = sizeof(CustomObject),
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/custom4.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ static PyMethodDef Custom_methods[] = {
};

static PyTypeObject CustomType = {
PyVarObject_HEAD_INIT(NULL, 0)
_PyVarObject_HEAD_IMMORTAL_INIT(NULL, 0)
.tp_name = "custom4.Custom",
.tp_doc = PyDoc_STR("Custom objects"),
.tp_basicsize = sizeof(CustomObject),
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/sublist.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ SubList_init(SubListObject *self, PyObject *args, PyObject *kwds)
}

static PyTypeObject SubListType = {
PyVarObject_HEAD_INIT(NULL, 0)
_PyVarObject_HEAD_IMMORTAL_INIT(NULL, 0)
.tp_name = "sublist.SubList",
.tp_doc = PyDoc_STR("SubList objects"),
.tp_basicsize = sizeof(SubListObject),
Expand Down
8 changes: 6 additions & 2 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ whose size is determined when the object is allocated.
{ _PyObject_EXTRA_INIT \
1, (type) },

#define PyVarObject_HEAD_INIT(type, size) \
{ PyObject_HEAD_INIT(type) (size) },
#define _PyObject_HEAD_IMMORTAL_INIT(type) \
{ _PyObject_EXTRA_INIT \
999999999, (type) },

#define _PyVarObject_HEAD_IMMORTAL_INIT(type, size) \
{ _PyObject_HEAD_IMMORTAL_INIT(type) (size) },

/* PyObject_VAR_HEAD defines the initial segment of all variable-size
* container objects. These end with a declaration of an array with 1
Expand Down
48 changes: 24 additions & 24 deletions Lib/test/clinic.test
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,7 @@ test_keywords(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(a), &_Py_ID(b), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -1997,7 +1997,7 @@ test_keywords_kwonly(PyObject *module, PyObject *const *args, Py_ssize_t nargs,
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(a), &_Py_ID(b), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2068,7 +2068,7 @@ test_keywords_opt(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyO
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(a), &_Py_ID(b), &_Py_ID(c), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2154,7 +2154,7 @@ test_keywords_opt_kwonly(PyObject *module, PyObject *const *args, Py_ssize_t nar
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(a), &_Py_ID(b), &_Py_ID(c), &_Py_ID(d), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2250,7 +2250,7 @@ test_keywords_kwonly_opt(PyObject *module, PyObject *const *args, Py_ssize_t nar
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(a), &_Py_ID(b), &_Py_ID(c), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2333,7 +2333,7 @@ test_posonly_keywords(PyObject *module, PyObject *const *args, Py_ssize_t nargs,
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2404,7 +2404,7 @@ test_posonly_kwonly(PyObject *module, PyObject *const *args, Py_ssize_t nargs, P
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(c), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2477,7 +2477,7 @@ test_posonly_keywords_kwonly(PyObject *module, PyObject *const *args, Py_ssize_t
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), &_Py_ID(c), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2553,7 +2553,7 @@ test_posonly_keywords_opt(PyObject *module, PyObject *const *args, Py_ssize_t na
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), &_Py_ID(c), &_Py_ID(d), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2640,7 +2640,7 @@ test_posonly_keywords_opt2(PyObject *module, PyObject *const *args, Py_ssize_t n
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), &_Py_ID(c), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2726,7 +2726,7 @@ test_posonly_opt_keywords_opt(PyObject *module, PyObject *const *args, Py_ssize_
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(c), &_Py_ID(d), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2820,7 +2820,7 @@ test_posonly_kwonly_opt(PyObject *module, PyObject *const *args, Py_ssize_t narg
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), &_Py_ID(c), &_Py_ID(d), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2908,7 +2908,7 @@ test_posonly_kwonly_opt2(PyObject *module, PyObject *const *args, Py_ssize_t nar
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), &_Py_ID(c), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -2995,7 +2995,7 @@ test_posonly_opt_kwonly_opt(PyObject *module, PyObject *const *args, Py_ssize_t
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(c), &_Py_ID(d), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -3091,7 +3091,7 @@ test_posonly_keywords_kwonly_opt(PyObject *module, PyObject *const *args, Py_ssi
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), &_Py_ID(c), &_Py_ID(d), &_Py_ID(e), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -3183,7 +3183,7 @@ test_posonly_keywords_kwonly_opt2(PyObject *module, PyObject *const *args, Py_ss
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), &_Py_ID(c), &_Py_ID(d), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -3275,7 +3275,7 @@ test_posonly_keywords_opt_kwonly_opt(PyObject *module, PyObject *const *args, Py
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), &_Py_ID(c), &_Py_ID(d), &_Py_ID(e), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -3379,7 +3379,7 @@ test_posonly_keywords_opt2_kwonly_opt(PyObject *module, PyObject *const *args, P
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), &_Py_ID(c), &_Py_ID(d), &_Py_ID(e), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -3490,7 +3490,7 @@ test_posonly_opt_keywords_opt_kwonly_opt(PyObject *module, PyObject *const *args
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(c), &_Py_ID(d), &_Py_ID(e), &_Py_ID(f), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -3599,7 +3599,7 @@ test_keyword_only_parameter(PyObject *module, PyObject *const *args, Py_ssize_t
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(co_lnotab), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -3830,7 +3830,7 @@ test_vararg(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(a), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -3902,7 +3902,7 @@ test_vararg_with_default(PyObject *module, PyObject *const *args, Py_ssize_t nar
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(a), &_Py_ID(b), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -3985,7 +3985,7 @@ test_vararg_with_only_defaults(PyObject *module, PyObject *const *args, Py_ssize
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(b), &_Py_ID(c), },
};
#undef NUM_KEYWORDS
Expand Down Expand Up @@ -4068,7 +4068,7 @@ test_paramname_module(PyObject *module, PyObject *const *args, Py_ssize_t nargs,
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_base = _PyVarObject_HEAD_IMMORTAL_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(module), },
};
#undef NUM_KEYWORDS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Replace all internal uses of PyVarObject_HEAD_INIT fo
_PyVarObject_HEAD_IMMORTAL_INIT to be able to initialize them as immortal
objects since they live thorughout the entire execution
Copy link
Member

Choose a reason for hiding this comment

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

Typo

Suggested change
objects since they live thorughout the entire execution
objects since they live throughout the entire execution

4 changes: 2 additions & 2 deletions Modules/_blake2/clinic/blake2b_impl.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Modules/_blake2/clinic/blake2s_impl.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ PyDoc_STRVAR(deque_doc,
A list-like sequence optimized for data accesses near its endpoints.");

static PyTypeObject deque_type = {
PyVarObject_HEAD_INIT(NULL, 0)
_PyVarObject_HEAD_IMMORTAL_INIT(NULL, 0)
"collections.deque", /* tp_name */
sizeof(dequeobject), /* tp_basicsize */
0, /* tp_itemsize */
Expand Down Expand Up @@ -1770,7 +1770,7 @@ static PyMethodDef dequeiter_methods[] = {
};

static PyTypeObject dequeiter_type = {
PyVarObject_HEAD_INIT(NULL, 0)
_PyVarObject_HEAD_IMMORTAL_INIT(NULL, 0)
"_collections._deque_iterator", /* tp_name */
sizeof(dequeiterobject), /* tp_basicsize */
0, /* tp_itemsize */
Expand Down Expand Up @@ -1890,7 +1890,7 @@ dequereviter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}

static PyTypeObject dequereviter_type = {
PyVarObject_HEAD_INIT(NULL, 0)
_PyVarObject_HEAD_IMMORTAL_INIT(NULL, 0)
"_collections._deque_reverse_iterator", /* tp_name */
sizeof(dequeiterobject), /* tp_basicsize */
0, /* tp_itemsize */
Expand Down Expand Up @@ -2204,7 +2204,7 @@ passed to the dict constructor, including keyword arguments.\n\
#define DEFERRED_ADDRESS(ADDR) 0

static PyTypeObject defdict_type = {
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
_PyVarObject_HEAD_IMMORTAL_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
"collections.defaultdict", /* tp_name */
sizeof(defdictobject), /* tp_basicsize */
0, /* tp_itemsize */
Expand Down Expand Up @@ -2488,7 +2488,7 @@ static PyMethodDef tuplegetter_methods[] = {
};

static PyTypeObject tuplegetter_type = {
PyVarObject_HEAD_INIT(NULL, 0)
_PyVarObject_HEAD_IMMORTAL_INIT(NULL, 0)
"_collections._tuplegetter", /* tp_name */
sizeof(_tuplegetterobject), /* tp_basicsize */
0, /* tp_itemsize */
Expand Down
Loading