Skip to content

Commit 0551d52

Browse files
Add CREATE_TYPE helper
1 parent 288a764 commit 0551d52

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

Diff for: Modules/_pickle.c

+15-29
Original file line numberDiff line numberDiff line change
@@ -7851,43 +7851,29 @@ _pickle_exec(PyObject *m)
78517851
{
78527852
PickleState *st = _Pickle_GetState(m);
78537853

7854-
st->Pdata_Type = (PyTypeObject *)PyType_FromMetaclass(NULL, m, &pdata_spec,
7855-
NULL);
7856-
if (st->Pdata_Type == NULL) {
7857-
return -1;
7858-
}
7859-
7860-
st->PicklerMemoProxyType = (PyTypeObject *)PyType_FromMetaclass(
7861-
NULL, m, &memoproxy_spec, NULL);
7862-
if (st->PicklerMemoProxyType == NULL) {
7863-
return -1;
7864-
}
7865-
7866-
st->UnpicklerMemoProxyType = (PyTypeObject *)PyType_FromMetaclass(
7867-
NULL, m, &unpickler_memoproxy_spec, NULL);
7868-
if (st->UnpicklerMemoProxyType == NULL) {
7869-
return -1;
7870-
}
7854+
#define CREATE_TYPE(mod, type, spec) \
7855+
do { \
7856+
type = (PyTypeObject *)PyType_FromMetaclass(NULL, mod, spec, NULL); \
7857+
if (type == NULL) { \
7858+
return -1; \
7859+
} \
7860+
} while (0)
7861+
7862+
CREATE_TYPE(m, st->Pdata_Type, &pdata_spec);
7863+
CREATE_TYPE(m, st->PicklerMemoProxyType, &memoproxy_spec);
7864+
CREATE_TYPE(m, st->UnpicklerMemoProxyType, &unpickler_memoproxy_spec);
7865+
CREATE_TYPE(m, st->Pickler_Type, &pickler_type_spec);
7866+
CREATE_TYPE(m, st->Unpickler_Type, &unpickler_type_spec);
7867+
7868+
#undef CREATE_TYPE
78717869

78727870
/* Add types */
78737871
if (PyModule_AddType(m, &PyPickleBuffer_Type) < 0) {
78747872
return -1;
78757873
}
7876-
7877-
st->Pickler_Type = (PyTypeObject *)PyType_FromModuleAndSpec(m, &pickler_type_spec, NULL);
7878-
if (st->Pickler_Type == NULL) {
7879-
return -1;
7880-
}
7881-
78827874
if (PyModule_AddType(m, st->Pickler_Type) < 0) {
78837875
return -1;
78847876
}
7885-
7886-
st->Unpickler_Type = (PyTypeObject *)PyType_FromModuleAndSpec(m, &unpickler_type_spec, NULL);
7887-
if (st->Unpickler_Type == NULL) {
7888-
return -1;
7889-
}
7890-
78917877
if (PyModule_AddType(m, st->Unpickler_Type) < 0) {
78927878
return -1;
78937879
}

0 commit comments

Comments
 (0)