Skip to content

bpo-46417: Use _PyType_CAST() in Modules directory #30767

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

Closed
wants to merge 1 commit into from
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
20 changes: 10 additions & 10 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1691,24 +1691,24 @@ static struct PyMethodDef csv_methods[] = {
static int
csv_exec(PyObject *module) {
const StyleDesc *style;
PyObject *temp;
_csvstate *module_state = get_csv_state(module);

temp = PyType_FromModuleAndSpec(module, &Dialect_Type_spec, NULL);
module_state->dialect_type = (PyTypeObject *)temp;
if (PyModule_AddObjectRef(module, "Dialect", temp) < 0) {
PyTypeObject *type;
type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &Dialect_Type_spec, NULL);
module_state->dialect_type = type;
if (PyModule_AddObjectRef(module, "Dialect", (PyObject*)type) < 0) {
return -1;
}

temp = PyType_FromModuleAndSpec(module, &Reader_Type_spec, NULL);
module_state->reader_type = (PyTypeObject *)temp;
if (PyModule_AddObjectRef(module, "Reader", temp) < 0) {
type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &Reader_Type_spec, NULL);
module_state->reader_type = type;
if (PyModule_AddObjectRef(module, "Reader", (PyObject*)type) < 0) {
return -1;
}

temp = PyType_FromModuleAndSpec(module, &Writer_Type_spec, NULL);
module_state->writer_type = (PyTypeObject *)temp;
if (PyModule_AddObjectRef(module, "Writer", temp) < 0) {
type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &Writer_Type_spec, NULL);
module_state->writer_type = type;
if (PyModule_AddObjectRef(module, "Writer", (PyObject*)type) < 0) {
return -1;
}

Expand Down
8 changes: 4 additions & 4 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -876,10 +876,10 @@ new_date_subclass_ex(int year, int month, int day, PyObject *cls)
{
PyObject *result;
// We have "fast path" constructors for two subclasses: date and datetime
if ((PyTypeObject *)cls == &PyDateTime_DateType) {
if (cls == (PyObject *)&PyDateTime_DateType) {
result = new_date_ex(year, month, day, (PyTypeObject *)cls);
}
else if ((PyTypeObject *)cls == &PyDateTime_DateTimeType) {
else if (cls == (PyObject *)&PyDateTime_DateTimeType) {
result = new_datetime_ex(year, month, day, 0, 0, 0, 0, Py_None,
(PyTypeObject *)cls);
}
Expand Down Expand Up @@ -942,7 +942,7 @@ new_datetime_subclass_fold_ex(int year, int month, int day, int hour, int minute
int second, int usecond, PyObject *tzinfo,
int fold, PyObject *cls) {
PyObject* dt;
if ((PyTypeObject*)cls == &PyDateTime_DateTimeType) {
if (cls == (PyObject *)&PyDateTime_DateTimeType) {
// Use the fast path constructor
dt = new_datetime(year, month, day, hour, minute, second, usecond,
tzinfo, fold);
Expand Down Expand Up @@ -4603,7 +4603,7 @@ time_fromisoformat(PyObject *cls, PyObject *tstr) {
}

PyObject *t;
if ( (PyTypeObject *)cls == &PyDateTime_TimeType ) {
if (cls == (PyObject *)&PyDateTime_TimeType ) {
t = new_time(hour, minute, second, microsecond, tzinfo, 0);
} else {
t = PyObject_CallFunction(cls, "iiiiO",
Expand Down
22 changes: 12 additions & 10 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -5923,7 +5923,7 @@ newobj_unpickling_error(const char * msg, int use_kwargs, PyObject *arg)
static int
load_newobj(UnpicklerObject *self, int use_kwargs)
{
PyObject *cls, *args, *kwargs = NULL;
PyObject *args, *kwargs = NULL;
PyObject *obj;

/* Stack is ... cls args [kwargs], and we want to call
Expand All @@ -5940,21 +5940,23 @@ load_newobj(UnpicklerObject *self, int use_kwargs)
Py_XDECREF(kwargs);
return -1;
}
PDATA_POP(self->stack, cls);
if (cls == NULL) {
PyObject *cls_obj;
PDATA_POP(self->stack, cls_obj);
if (cls_obj == NULL) {
Py_XDECREF(kwargs);
Py_DECREF(args);
return -1;
}

if (!PyType_Check(cls)) {
if (!PyType_Check(cls_obj)) {
newobj_unpickling_error("%s class argument must be a type, not %.200s",
use_kwargs, cls);
use_kwargs, cls_obj);
goto error;
}
if (((PyTypeObject *)cls)->tp_new == NULL) {
PyTypeObject *cls = (PyTypeObject *)cls_obj;
if (cls->tp_new == NULL) {
newobj_unpickling_error("%s class argument '%.200s' doesn't have __new__",
use_kwargs, cls);
use_kwargs, (PyObject*)cls);
goto error;
}
if (!PyTuple_Check(args)) {
Expand All @@ -5968,20 +5970,20 @@ load_newobj(UnpicklerObject *self, int use_kwargs)
goto error;
}

obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
obj = cls->tp_new(cls, args, kwargs);
if (obj == NULL) {
goto error;
}
Py_XDECREF(kwargs);
Py_DECREF(args);
Py_DECREF(cls);
Py_DECREF(cls_obj);
PDATA_PUSH(self->stack, obj, -1);
return 0;

error:
Py_XDECREF(kwargs);
Py_DECREF(args);
Py_DECREF(cls);
Py_DECREF(cls_obj);
return -1;
}

Expand Down
11 changes: 6 additions & 5 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
#define LOWER_MASK 0x7fffffffU /* least significant r bits */

typedef struct {
PyObject *Random_Type;
PyTypeObject *Random_Type;
PyObject *Long___abs__;
} _randomstate;

Expand Down Expand Up @@ -531,8 +531,9 @@ random_init(RandomObject *self, PyObject *args, PyObject *kwds)
PyObject *arg = NULL;
_randomstate *state = _randomstate_type(Py_TYPE(self));

if ((Py_IS_TYPE(self, (PyTypeObject *)state->Random_Type) ||
Py_TYPE(self)->tp_init == ((PyTypeObject*)state->Random_Type)->tp_init) &&
PyTypeObject *Random_Type = state->Random_Type;
if ((Py_IS_TYPE(self, Random_Type) ||
Py_TYPE(self)->tp_init == Random_Type->tp_init) &&
!_PyArg_NoKeywords("Random", kwds)) {
return -1;
}
Expand Down Expand Up @@ -586,12 +587,12 @@ _random_exec(PyObject *module)
{
_randomstate *state = get_random_state(module);

state->Random_Type = PyType_FromModuleAndSpec(
state->Random_Type = (PyTypeObject *)PyType_FromModuleAndSpec(
module, &Random_Type_spec, NULL);
if (state->Random_Type == NULL) {
return -1;
}
if (PyModule_AddType(module, (PyTypeObject *)state->Random_Type) < 0) {
if (PyModule_AddType(module, state->Random_Type) < 0) {
return -1;
}

Expand Down
5 changes: 3 additions & 2 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2013,11 +2013,12 @@ static PyType_Spec connection_spec = {
int
pysqlite_connection_setup_types(PyObject *module)
{
PyObject *type = PyType_FromModuleAndSpec(module, &connection_spec, NULL);
PyTypeObject *type;
type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
if (type == NULL) {
return -1;
}
pysqlite_state *state = pysqlite_get_state(module);
state->ConnectionType = (PyTypeObject *)type;
state->ConnectionType = type;
return 0;
}
5 changes: 3 additions & 2 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1033,11 +1033,12 @@ static PyType_Spec cursor_spec = {
int
pysqlite_cursor_setup_types(PyObject *module)
{
PyObject *type = PyType_FromModuleAndSpec(module, &cursor_spec, NULL);
PyTypeObject *type;
type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &cursor_spec, NULL);
if (type == NULL) {
return -1;
}
pysqlite_state *state = pysqlite_get_state(module);
state->CursorType = (PyTypeObject *)type;
state->CursorType = type;
return 0;
}
5 changes: 3 additions & 2 deletions Modules/_sqlite/prepare_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ static PyType_Spec type_spec = {
int
pysqlite_prepare_protocol_setup_types(PyObject *module)
{
PyObject *type = PyType_FromModuleAndSpec(module, &type_spec, NULL);
PyTypeObject *type;
type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &type_spec, NULL);
if (type == NULL) {
return -1;
}
pysqlite_state *state = pysqlite_get_state(module);
state->PrepareProtocolType = (PyTypeObject *)type;
state->PrepareProtocolType = type;
return 0;
}
5 changes: 3 additions & 2 deletions Modules/_sqlite/row.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,12 @@ static PyType_Spec row_spec = {
int
pysqlite_row_setup_types(PyObject *module)
{
PyObject *type = PyType_FromModuleAndSpec(module, &row_spec, NULL);
PyTypeObject *type;
type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &row_spec, NULL);
if (type == NULL) {
return -1;
}
pysqlite_state *state = pysqlite_get_state(module);
state->RowType = (PyTypeObject *)type;
state->RowType = type;
return 0;
}
5 changes: 3 additions & 2 deletions Modules/_sqlite/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,12 @@ static PyType_Spec stmt_spec = {
int
pysqlite_statement_setup_types(PyObject *module)
{
PyObject *type = PyType_FromModuleAndSpec(module, &stmt_spec, NULL);
PyTypeObject *type;
type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &stmt_spec, NULL);
if (type == NULL) {
return -1;
}
pysqlite_state *state = pysqlite_get_state(module);
state->StatementType = (PyTypeObject *)type;
state->StatementType = type;
return 0;
}
16 changes: 8 additions & 8 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,23 +1151,23 @@ test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored))
assert(strcmp(PyUnicode_AsUTF8(tp_name), "module") == 0);
Py_DECREF(tp_name);

PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec);
PyTypeObject *HeapTypeNameType = (PyTypeObject *)PyType_FromSpec(&HeapTypeNameType_Spec);
if (HeapTypeNameType == NULL) {
Py_RETURN_NONE;
}
tp_name = PyType_GetName((PyTypeObject *)HeapTypeNameType);
tp_name = PyType_GetName(HeapTypeNameType);
assert(strcmp(PyUnicode_AsUTF8(tp_name), "HeapTypeNameType") == 0);
Py_DECREF(tp_name);

PyObject *name = PyUnicode_FromString("test_name");
if (name == NULL) {
goto done;
}
if (PyObject_SetAttrString(HeapTypeNameType, "__name__", name) < 0) {
if (PyObject_SetAttrString((PyObject*)HeapTypeNameType, "__name__", name) < 0) {
Py_DECREF(name);
goto done;
}
tp_name = PyType_GetName((PyTypeObject *)HeapTypeNameType);
tp_name = PyType_GetName(HeapTypeNameType);
assert(strcmp(PyUnicode_AsUTF8(tp_name), "test_name") == 0);
Py_DECREF(name);
Py_DECREF(tp_name);
Expand Down Expand Up @@ -1309,24 +1309,24 @@ test_get_type_qualname(PyObject *self, PyObject *Py_UNUSED(ignored))
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "OrderedDict") == 0);
Py_DECREF(tp_qualname);

PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec);
PyTypeObject *HeapTypeNameType = (PyTypeObject *)PyType_FromSpec(&HeapTypeNameType_Spec);
if (HeapTypeNameType == NULL) {
Py_RETURN_NONE;
}
tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
tp_qualname = PyType_GetQualName(HeapTypeNameType);
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "HeapTypeNameType") == 0);
Py_DECREF(tp_qualname);

PyObject *spec_name = PyUnicode_FromString(HeapTypeNameType_Spec.name);
if (spec_name == NULL) {
goto done;
}
if (PyObject_SetAttrString(HeapTypeNameType,
if (PyObject_SetAttrString((PyObject*)HeapTypeNameType,
"__qualname__", spec_name) < 0) {
Py_DECREF(spec_name);
goto done;
}
tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
tp_qualname = PyType_GetQualName(HeapTypeNameType);
assert(strcmp(PyUnicode_AsUTF8(tp_qualname),
"_testcapi.HeapTypeNameType") == 0);
Py_DECREF(spec_name);
Expand Down
3 changes: 1 addition & 2 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ zoneinfo_no_cache(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
}

static PyObject *
zoneinfo_clear_cache(PyObject *cls, PyObject *args, PyObject *kwargs)
zoneinfo_clear_cache(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *only_keys = NULL;
static char *kwlist[] = {"only_keys", NULL};
Expand All @@ -407,7 +407,6 @@ zoneinfo_clear_cache(PyObject *cls, PyObject *args, PyObject *kwargs)
return NULL;
}

PyTypeObject *type = (PyTypeObject *)cls;
PyObject *weak_cache = get_weak_cache(type);

if (only_keys == NULL || only_keys == Py_None) {
Expand Down