bpo-40024: Update C extension modules to use PyModule_AddType() (GH-19119)

Update _asyncio, _bz2, _csv, _curses, _datetime,
_io, _operator, _pickle, _queue, blake2,
multibytecodec and overlapped C extension modules
to use PyModule_AddType().
This commit is contained in:
Dong-hee Na 2020-03-25 07:08:51 +09:00 committed by GitHub
parent 15e5024d04
commit 37fcbb65d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 66 additions and 133 deletions

View File

@ -3391,9 +3391,6 @@ PyInit__asyncio(void)
if (module_init() < 0) { if (module_init() < 0) {
return NULL; return NULL;
} }
if (PyType_Ready(&FutureType) < 0) {
return NULL;
}
if (PyType_Ready(&FutureIterType) < 0) { if (PyType_Ready(&FutureIterType) < 0) {
return NULL; return NULL;
} }
@ -3403,9 +3400,6 @@ PyInit__asyncio(void)
if (PyType_Ready(&TaskWakeupMethWrapper_Type) < 0) { if (PyType_Ready(&TaskWakeupMethWrapper_Type) < 0) {
return NULL; return NULL;
} }
if (PyType_Ready(&TaskType) < 0) {
return NULL;
}
if (PyType_Ready(&PyRunningLoopHolder_Type) < 0) { if (PyType_Ready(&PyRunningLoopHolder_Type) < 0) {
return NULL; return NULL;
} }
@ -3415,16 +3409,13 @@ PyInit__asyncio(void)
return NULL; return NULL;
} }
Py_INCREF(&FutureType); /* FutureType and TaskType are made ready by PyModule_AddType() calls below. */
if (PyModule_AddObject(m, "Future", (PyObject *)&FutureType) < 0) { if (PyModule_AddType(m, &FutureType) < 0) {
Py_DECREF(&FutureType);
Py_DECREF(m); Py_DECREF(m);
return NULL; return NULL;
} }
Py_INCREF(&TaskType); if (PyModule_AddType(m, &TaskType) < 0) {
if (PyModule_AddObject(m, "Task", (PyObject *)&TaskType) < 0) {
Py_DECREF(&TaskType);
Py_DECREF(m); Py_DECREF(m);
return NULL; return NULL;
} }

View File

@ -63,13 +63,10 @@ PyInit__blake2(void)
/* BLAKE2b */ /* BLAKE2b */
Py_SET_TYPE(&PyBlake2_BLAKE2bType, &PyType_Type); Py_SET_TYPE(&PyBlake2_BLAKE2bType, &PyType_Type);
if (PyType_Ready(&PyBlake2_BLAKE2bType) < 0) { if (PyModule_AddType(m, &PyBlake2_BLAKE2bType) < 0) {
return NULL; return NULL;
} }
Py_INCREF(&PyBlake2_BLAKE2bType);
PyModule_AddObject(m, "blake2b", (PyObject *)&PyBlake2_BLAKE2bType);
d = PyBlake2_BLAKE2bType.tp_dict; d = PyBlake2_BLAKE2bType.tp_dict;
ADD_INT(d, "SALT_SIZE", BLAKE2B_SALTBYTES); ADD_INT(d, "SALT_SIZE", BLAKE2B_SALTBYTES);
ADD_INT(d, "PERSON_SIZE", BLAKE2B_PERSONALBYTES); ADD_INT(d, "PERSON_SIZE", BLAKE2B_PERSONALBYTES);
@ -83,13 +80,10 @@ PyInit__blake2(void)
/* BLAKE2s */ /* BLAKE2s */
Py_SET_TYPE(&PyBlake2_BLAKE2sType, &PyType_Type); Py_SET_TYPE(&PyBlake2_BLAKE2sType, &PyType_Type);
if (PyType_Ready(&PyBlake2_BLAKE2sType) < 0) { if (PyModule_AddType(m, &PyBlake2_BLAKE2sType) < 0) {
return NULL; return NULL;
} }
Py_INCREF(&PyBlake2_BLAKE2sType);
PyModule_AddObject(m, "blake2s", (PyObject *)&PyBlake2_BLAKE2sType);
d = PyBlake2_BLAKE2sType.tp_dict; d = PyBlake2_BLAKE2sType.tp_dict;
ADD_INT(d, "SALT_SIZE", BLAKE2S_SALTBYTES); ADD_INT(d, "SALT_SIZE", BLAKE2S_SALTBYTES);
ADD_INT(d, "PERSON_SIZE", BLAKE2S_PERSONALBYTES); ADD_INT(d, "PERSON_SIZE", BLAKE2S_PERSONALBYTES);

View File

@ -731,24 +731,11 @@ static PyTypeObject BZ2Decompressor_Type = {
static int static int
_bz2_exec(PyObject *module) _bz2_exec(PyObject *module)
{ {
if (PyType_Ready(&BZ2Compressor_Type) < 0) { if (PyModule_AddType(module, &BZ2Compressor_Type) < 0) {
return -1;
}
if (PyType_Ready(&BZ2Decompressor_Type) < 0) {
return -1; return -1;
} }
Py_INCREF(&BZ2Compressor_Type); if (PyModule_AddType(module, &BZ2Decompressor_Type) < 0) {
if (PyModule_AddObject(module, "BZ2Compressor",
(PyObject *)&BZ2Compressor_Type) < 0) {
Py_DECREF(&BZ2Compressor_Type);
return -1;
}
Py_INCREF(&BZ2Decompressor_Type);
if (PyModule_AddObject(module, "BZ2Decompressor",
(PyObject *)&BZ2Decompressor_Type) < 0) {
Py_INCREF(&BZ2Decompressor_Type);
return -1; return -1;
} }

View File

@ -1633,9 +1633,6 @@ PyInit__csv(void)
PyObject *module; PyObject *module;
const StyleDesc *style; const StyleDesc *style;
if (PyType_Ready(&Dialect_Type) < 0)
return NULL;
if (PyType_Ready(&Reader_Type) < 0) if (PyType_Ready(&Reader_Type) < 0)
return NULL; return NULL;
@ -1671,10 +1668,9 @@ PyInit__csv(void)
return NULL; return NULL;
} }
/* Add the Dialect type */ if (PyModule_AddType(module, &Dialect_Type)) {
Py_INCREF(&Dialect_Type);
if (PyModule_AddObject(module, "Dialect", (PyObject *)&Dialect_Type))
return NULL; return NULL;
}
/* Add the CSV exception object to the module. */ /* Add the CSV exception object to the module. */
get_csv_state(module)->error_obj = PyErr_NewException("_csv.Error", NULL, NULL); get_csv_state(module)->error_obj = PyErr_NewException("_csv.Error", NULL, NULL);

View File

@ -4740,7 +4740,8 @@ PyInit__curses(void)
SetDictInt("KEY_MAX", KEY_MAX); SetDictInt("KEY_MAX", KEY_MAX);
} }
Py_INCREF(&PyCursesWindow_Type); if (PyModule_AddType(m, &PyCursesWindow_Type) < 0) {
PyModule_AddObject(m, "window", (PyObject *)&PyCursesWindow_Type); return NULL;
}
return m; return m;
} }

View File

@ -6386,18 +6386,20 @@ PyInit__datetime(void)
if (m == NULL) if (m == NULL)
return NULL; return NULL;
if (PyType_Ready(&PyDateTime_DateType) < 0) PyTypeObject *types[] = {
return NULL; &PyDateTime_DateType,
if (PyType_Ready(&PyDateTime_DateTimeType) < 0) &PyDateTime_DateTimeType,
return NULL; &PyDateTime_TimeType,
if (PyType_Ready(&PyDateTime_DeltaType) < 0) &PyDateTime_DeltaType,
return NULL; &PyDateTime_TZInfoType,
if (PyType_Ready(&PyDateTime_TimeType) < 0) &PyDateTime_TimeZoneType
return NULL; };
if (PyType_Ready(&PyDateTime_TZInfoType) < 0)
return NULL; for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) if (PyModule_AddType(m, types[i]) < 0) {
return NULL; return NULL;
}
}
/* timedelta values */ /* timedelta values */
d = PyDateTime_DeltaType.tp_dict; d = PyDateTime_DeltaType.tp_dict;
@ -6515,25 +6517,6 @@ PyInit__datetime(void)
PyModule_AddIntMacro(m, MINYEAR); PyModule_AddIntMacro(m, MINYEAR);
PyModule_AddIntMacro(m, MAXYEAR); PyModule_AddIntMacro(m, MAXYEAR);
Py_INCREF(&PyDateTime_DateType);
PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType);
Py_INCREF(&PyDateTime_DateTimeType);
PyModule_AddObject(m, "datetime",
(PyObject *)&PyDateTime_DateTimeType);
Py_INCREF(&PyDateTime_TimeType);
PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType);
Py_INCREF(&PyDateTime_DeltaType);
PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType);
Py_INCREF(&PyDateTime_TZInfoType);
PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType);
Py_INCREF(&PyDateTime_TimeZoneType);
PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType);
x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL);
if (x == NULL) if (x == NULL)
return NULL; return NULL;

View File

@ -684,12 +684,8 @@ PyInit__io(void)
state = get_io_state(m); state = get_io_state(m);
state->initialized = 0; state->initialized = 0;
#define ADD_TYPE(type, name) \ #define ADD_TYPE(type) \
if (PyType_Ready(type) < 0) \ if (PyModule_AddType(m, type) < 0) { \
goto fail; \
Py_INCREF(type); \
if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \
Py_DECREF(type); \
goto fail; \ goto fail; \
} }
@ -717,54 +713,54 @@ PyInit__io(void)
/* Concrete base types of the IO ABCs. /* Concrete base types of the IO ABCs.
(the ABCs themselves are declared through inheritance in io.py) (the ABCs themselves are declared through inheritance in io.py)
*/ */
ADD_TYPE(&PyIOBase_Type, "_IOBase"); ADD_TYPE(&PyIOBase_Type);
ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase"); ADD_TYPE(&PyRawIOBase_Type);
ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase"); ADD_TYPE(&PyBufferedIOBase_Type);
ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase"); ADD_TYPE(&PyTextIOBase_Type);
/* Implementation of concrete IO objects. */ /* Implementation of concrete IO objects. */
/* FileIO */ /* FileIO */
PyFileIO_Type.tp_base = &PyRawIOBase_Type; PyFileIO_Type.tp_base = &PyRawIOBase_Type;
ADD_TYPE(&PyFileIO_Type, "FileIO"); ADD_TYPE(&PyFileIO_Type);
/* BytesIO */ /* BytesIO */
PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type; PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type;
ADD_TYPE(&PyBytesIO_Type, "BytesIO"); ADD_TYPE(&PyBytesIO_Type);
if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0) if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0)
goto fail; goto fail;
/* StringIO */ /* StringIO */
PyStringIO_Type.tp_base = &PyTextIOBase_Type; PyStringIO_Type.tp_base = &PyTextIOBase_Type;
ADD_TYPE(&PyStringIO_Type, "StringIO"); ADD_TYPE(&PyStringIO_Type);
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
/* WindowsConsoleIO */ /* WindowsConsoleIO */
PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type; PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type;
ADD_TYPE(&PyWindowsConsoleIO_Type, "_WindowsConsoleIO"); ADD_TYPE(&PyWindowsConsoleIO_Type);
#endif #endif
/* BufferedReader */ /* BufferedReader */
PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type; PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type;
ADD_TYPE(&PyBufferedReader_Type, "BufferedReader"); ADD_TYPE(&PyBufferedReader_Type);
/* BufferedWriter */ /* BufferedWriter */
PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type; PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type;
ADD_TYPE(&PyBufferedWriter_Type, "BufferedWriter"); ADD_TYPE(&PyBufferedWriter_Type);
/* BufferedRWPair */ /* BufferedRWPair */
PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type; PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type;
ADD_TYPE(&PyBufferedRWPair_Type, "BufferedRWPair"); ADD_TYPE(&PyBufferedRWPair_Type);
/* BufferedRandom */ /* BufferedRandom */
PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type; PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type;
ADD_TYPE(&PyBufferedRandom_Type, "BufferedRandom"); ADD_TYPE(&PyBufferedRandom_Type);
/* TextIOWrapper */ /* TextIOWrapper */
PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type; PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type;
ADD_TYPE(&PyTextIOWrapper_Type, "TextIOWrapper"); ADD_TYPE(&PyTextIOWrapper_Type);
/* IncrementalNewlineDecoder */ /* IncrementalNewlineDecoder */
ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder"); ADD_TYPE(&PyIncrementalNewlineDecoder_Type);
/* Interned strings */ /* Interned strings */
#define ADD_INTERNED(name) \ #define ADD_INTERNED(name) \

View File

@ -1771,19 +1771,17 @@ PyInit__operator(void)
if (m == NULL) if (m == NULL)
return NULL; return NULL;
if (PyType_Ready(&itemgetter_type) < 0) PyTypeObject *types[] = {
return NULL; &itemgetter_type,
Py_INCREF(&itemgetter_type); &attrgetter_type,
PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type); &methodcaller_type
};
if (PyType_Ready(&attrgetter_type) < 0) for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
return NULL; if (PyModule_AddType(m, types[i]) < 0) {
Py_INCREF(&attrgetter_type); return NULL;
PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type); }
}
if (PyType_Ready(&methodcaller_type) < 0)
return NULL;
Py_INCREF(&methodcaller_type);
PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
return m; return m;
} }

View File

@ -7993,10 +7993,6 @@ PyInit__pickle(void)
return m; return m;
} }
if (PyType_Ready(&Unpickler_Type) < 0)
return NULL;
if (PyType_Ready(&Pickler_Type) < 0)
return NULL;
if (PyType_Ready(&Pdata_Type) < 0) if (PyType_Ready(&Pdata_Type) < 0)
return NULL; return NULL;
if (PyType_Ready(&PicklerMemoProxyType) < 0) if (PyType_Ready(&PicklerMemoProxyType) < 0)
@ -8010,16 +8006,15 @@ PyInit__pickle(void)
return NULL; return NULL;
/* Add types */ /* Add types */
Py_INCREF(&Pickler_Type); if (PyModule_AddType(m, &Pickler_Type) < 0) {
if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
return NULL; return NULL;
Py_INCREF(&Unpickler_Type); }
if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) if (PyModule_AddType(m, &Unpickler_Type) < 0) {
return NULL; return NULL;
Py_INCREF(&PyPickleBuffer_Type); }
if (PyModule_AddObject(m, "PickleBuffer", if (PyModule_AddType(m, &PyPickleBuffer_Type) < 0) {
(PyObject *)&PyPickleBuffer_Type) < 0)
return NULL; return NULL;
}
st = _Pickle_GetState(m); st = _Pickle_GetState(m);

View File

@ -390,11 +390,9 @@ PyInit__queue(void)
if (PyModule_AddObject(m, "Empty", EmptyError) < 0) if (PyModule_AddObject(m, "Empty", EmptyError) < 0)
return NULL; return NULL;
if (PyType_Ready(&PySimpleQueueType) < 0) if (PyModule_AddType(m, &PySimpleQueueType) < 0) {
return NULL;
Py_INCREF(&PySimpleQueueType);
if (PyModule_AddObject(m, "SimpleQueue", (PyObject *)&PySimpleQueueType) < 0)
return NULL; return NULL;
}
return m; return m;
} }

View File

@ -2059,14 +2059,12 @@ static struct PyModuleDef _multibytecodecmodule = {
PyMODINIT_FUNC PyMODINIT_FUNC
PyInit__multibytecodec(void) PyInit__multibytecodec(void)
{ {
int i;
PyObject *m; PyObject *m;
PyTypeObject *typelist[] = { PyTypeObject *typelist[] = {
&MultibyteIncrementalEncoder_Type, &MultibyteIncrementalEncoder_Type,
&MultibyteIncrementalDecoder_Type, &MultibyteIncrementalDecoder_Type,
&MultibyteStreamReader_Type, &MultibyteStreamReader_Type,
&MultibyteStreamWriter_Type, &MultibyteStreamWriter_Type
NULL
}; };
if (PyType_Ready(&MultibyteCodec_Type) < 0) if (PyType_Ready(&MultibyteCodec_Type) < 0)
@ -2076,12 +2074,10 @@ PyInit__multibytecodec(void)
if (m == NULL) if (m == NULL)
return NULL; return NULL;
for (i = 0; typelist[i] != NULL; i++) { for (size_t i = 0; i < Py_ARRAY_LENGTH(typelist); i++) {
if (PyType_Ready(typelist[i]) < 0) if (PyModule_AddType(m, typelist[i]) < 0) {
return NULL; return NULL;
Py_INCREF(typelist[i]); }
PyModule_AddObject(m, typelist[i]->tp_name,
(PyObject *)typelist[i]);
} }
if (PyErr_Occurred()) { if (PyErr_Occurred()) {

View File

@ -1860,12 +1860,10 @@ PyInit__overlapped(void)
if (initialize_function_pointers() < 0) if (initialize_function_pointers() < 0)
return NULL; return NULL;
if (PyType_Ready(&OverlappedType) < 0)
return NULL;
m = PyModule_Create(&overlapped_module); m = PyModule_Create(&overlapped_module);
if (PyModule_AddObject(m, "Overlapped", (PyObject *)&OverlappedType) < 0) if (PyModule_AddType(m, &OverlappedType) < 0) {
return NULL; return NULL;
}
d = PyModule_GetDict(m); d = PyModule_GetDict(m);