From 37fcbb65d4589fbb5a72153e9338cf8e6495f64f Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 25 Mar 2020 07:08:51 +0900 Subject: [PATCH] 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(). --- Modules/_asynciomodule.c | 15 ++-------- Modules/_blake2/blake2module.c | 10 ++----- Modules/_bz2module.c | 17 ++--------- Modules/_csv.c | 8 ++---- Modules/_cursesmodule.c | 5 ++-- Modules/_datetimemodule.c | 45 ++++++++++-------------------- Modules/_io/_iomodule.c | 36 +++++++++++------------- Modules/_operator.c | 22 +++++++-------- Modules/_pickle.c | 17 ++++------- Modules/_queuemodule.c | 6 ++-- Modules/cjkcodecs/multibytecodec.c | 12 +++----- Modules/overlapped.c | 6 ++-- 12 files changed, 66 insertions(+), 133 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index d394002c065..fa94a65f1ce 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3391,9 +3391,6 @@ PyInit__asyncio(void) if (module_init() < 0) { return NULL; } - if (PyType_Ready(&FutureType) < 0) { - return NULL; - } if (PyType_Ready(&FutureIterType) < 0) { return NULL; } @@ -3403,9 +3400,6 @@ PyInit__asyncio(void) if (PyType_Ready(&TaskWakeupMethWrapper_Type) < 0) { return NULL; } - if (PyType_Ready(&TaskType) < 0) { - return NULL; - } if (PyType_Ready(&PyRunningLoopHolder_Type) < 0) { return NULL; } @@ -3415,16 +3409,13 @@ PyInit__asyncio(void) return NULL; } - Py_INCREF(&FutureType); - if (PyModule_AddObject(m, "Future", (PyObject *)&FutureType) < 0) { - Py_DECREF(&FutureType); + /* FutureType and TaskType are made ready by PyModule_AddType() calls below. */ + if (PyModule_AddType(m, &FutureType) < 0) { Py_DECREF(m); return NULL; } - Py_INCREF(&TaskType); - if (PyModule_AddObject(m, "Task", (PyObject *)&TaskType) < 0) { - Py_DECREF(&TaskType); + if (PyModule_AddType(m, &TaskType) < 0) { Py_DECREF(m); return NULL; } diff --git a/Modules/_blake2/blake2module.c b/Modules/_blake2/blake2module.c index 9d280a9ccd5..ff142c9f3ed 100644 --- a/Modules/_blake2/blake2module.c +++ b/Modules/_blake2/blake2module.c @@ -63,13 +63,10 @@ PyInit__blake2(void) /* BLAKE2b */ Py_SET_TYPE(&PyBlake2_BLAKE2bType, &PyType_Type); - if (PyType_Ready(&PyBlake2_BLAKE2bType) < 0) { + if (PyModule_AddType(m, &PyBlake2_BLAKE2bType) < 0) { return NULL; } - Py_INCREF(&PyBlake2_BLAKE2bType); - PyModule_AddObject(m, "blake2b", (PyObject *)&PyBlake2_BLAKE2bType); - d = PyBlake2_BLAKE2bType.tp_dict; ADD_INT(d, "SALT_SIZE", BLAKE2B_SALTBYTES); ADD_INT(d, "PERSON_SIZE", BLAKE2B_PERSONALBYTES); @@ -83,13 +80,10 @@ PyInit__blake2(void) /* BLAKE2s */ Py_SET_TYPE(&PyBlake2_BLAKE2sType, &PyType_Type); - if (PyType_Ready(&PyBlake2_BLAKE2sType) < 0) { + if (PyModule_AddType(m, &PyBlake2_BLAKE2sType) < 0) { return NULL; } - Py_INCREF(&PyBlake2_BLAKE2sType); - PyModule_AddObject(m, "blake2s", (PyObject *)&PyBlake2_BLAKE2sType); - d = PyBlake2_BLAKE2sType.tp_dict; ADD_INT(d, "SALT_SIZE", BLAKE2S_SALTBYTES); ADD_INT(d, "PERSON_SIZE", BLAKE2S_PERSONALBYTES); diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c index fe588098987..008aef0555f 100644 --- a/Modules/_bz2module.c +++ b/Modules/_bz2module.c @@ -731,24 +731,11 @@ static PyTypeObject BZ2Decompressor_Type = { static int _bz2_exec(PyObject *module) { - if (PyType_Ready(&BZ2Compressor_Type) < 0) { - return -1; - } - if (PyType_Ready(&BZ2Decompressor_Type) < 0) { + if (PyModule_AddType(module, &BZ2Compressor_Type) < 0) { return -1; } - Py_INCREF(&BZ2Compressor_Type); - 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); + if (PyModule_AddType(module, &BZ2Decompressor_Type) < 0) { return -1; } diff --git a/Modules/_csv.c b/Modules/_csv.c index 968a3d0c7f8..9c497154ffb 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -1633,9 +1633,6 @@ PyInit__csv(void) PyObject *module; const StyleDesc *style; - if (PyType_Ready(&Dialect_Type) < 0) - return NULL; - if (PyType_Ready(&Reader_Type) < 0) return NULL; @@ -1671,10 +1668,9 @@ PyInit__csv(void) return NULL; } - /* Add the Dialect type */ - Py_INCREF(&Dialect_Type); - if (PyModule_AddObject(module, "Dialect", (PyObject *)&Dialect_Type)) + if (PyModule_AddType(module, &Dialect_Type)) { return NULL; + } /* Add the CSV exception object to the module. */ get_csv_state(module)->error_obj = PyErr_NewException("_csv.Error", NULL, NULL); diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index ee33107ed62..3d16af7f0a3 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -4740,7 +4740,8 @@ PyInit__curses(void) SetDictInt("KEY_MAX", KEY_MAX); } - Py_INCREF(&PyCursesWindow_Type); - PyModule_AddObject(m, "window", (PyObject *)&PyCursesWindow_Type); + if (PyModule_AddType(m, &PyCursesWindow_Type) < 0) { + return NULL; + } return m; } diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 4c985b37385..f7c1b69def3 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -6386,18 +6386,20 @@ PyInit__datetime(void) if (m == NULL) return NULL; - if (PyType_Ready(&PyDateTime_DateType) < 0) - return NULL; - if (PyType_Ready(&PyDateTime_DateTimeType) < 0) - return NULL; - if (PyType_Ready(&PyDateTime_DeltaType) < 0) - return NULL; - if (PyType_Ready(&PyDateTime_TimeType) < 0) - return NULL; - if (PyType_Ready(&PyDateTime_TZInfoType) < 0) - return NULL; - if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) - return NULL; + PyTypeObject *types[] = { + &PyDateTime_DateType, + &PyDateTime_DateTimeType, + &PyDateTime_TimeType, + &PyDateTime_DeltaType, + &PyDateTime_TZInfoType, + &PyDateTime_TimeZoneType + }; + + for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) { + if (PyModule_AddType(m, types[i]) < 0) { + return NULL; + } + } /* timedelta values */ d = PyDateTime_DeltaType.tp_dict; @@ -6515,25 +6517,6 @@ PyInit__datetime(void) PyModule_AddIntMacro(m, MINYEAR); 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); if (x == NULL) return NULL; diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c index 5ceae62f57a..e880992070d 100644 --- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -684,12 +684,8 @@ PyInit__io(void) state = get_io_state(m); state->initialized = 0; -#define ADD_TYPE(type, name) \ - if (PyType_Ready(type) < 0) \ - goto fail; \ - Py_INCREF(type); \ - if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \ - Py_DECREF(type); \ +#define ADD_TYPE(type) \ + if (PyModule_AddType(m, type) < 0) { \ goto fail; \ } @@ -717,54 +713,54 @@ PyInit__io(void) /* Concrete base types of the IO ABCs. (the ABCs themselves are declared through inheritance in io.py) */ - ADD_TYPE(&PyIOBase_Type, "_IOBase"); - ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase"); - ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase"); - ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase"); + ADD_TYPE(&PyIOBase_Type); + ADD_TYPE(&PyRawIOBase_Type); + ADD_TYPE(&PyBufferedIOBase_Type); + ADD_TYPE(&PyTextIOBase_Type); /* Implementation of concrete IO objects. */ /* FileIO */ PyFileIO_Type.tp_base = &PyRawIOBase_Type; - ADD_TYPE(&PyFileIO_Type, "FileIO"); + ADD_TYPE(&PyFileIO_Type); /* BytesIO */ PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type; - ADD_TYPE(&PyBytesIO_Type, "BytesIO"); + ADD_TYPE(&PyBytesIO_Type); if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0) goto fail; /* StringIO */ PyStringIO_Type.tp_base = &PyTextIOBase_Type; - ADD_TYPE(&PyStringIO_Type, "StringIO"); + ADD_TYPE(&PyStringIO_Type); #ifdef MS_WINDOWS /* WindowsConsoleIO */ PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type; - ADD_TYPE(&PyWindowsConsoleIO_Type, "_WindowsConsoleIO"); + ADD_TYPE(&PyWindowsConsoleIO_Type); #endif /* BufferedReader */ PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type; - ADD_TYPE(&PyBufferedReader_Type, "BufferedReader"); + ADD_TYPE(&PyBufferedReader_Type); /* BufferedWriter */ PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type; - ADD_TYPE(&PyBufferedWriter_Type, "BufferedWriter"); + ADD_TYPE(&PyBufferedWriter_Type); /* BufferedRWPair */ PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type; - ADD_TYPE(&PyBufferedRWPair_Type, "BufferedRWPair"); + ADD_TYPE(&PyBufferedRWPair_Type); /* BufferedRandom */ PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type; - ADD_TYPE(&PyBufferedRandom_Type, "BufferedRandom"); + ADD_TYPE(&PyBufferedRandom_Type); /* TextIOWrapper */ PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type; - ADD_TYPE(&PyTextIOWrapper_Type, "TextIOWrapper"); + ADD_TYPE(&PyTextIOWrapper_Type); /* IncrementalNewlineDecoder */ - ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder"); + ADD_TYPE(&PyIncrementalNewlineDecoder_Type); /* Interned strings */ #define ADD_INTERNED(name) \ diff --git a/Modules/_operator.c b/Modules/_operator.c index adee5fd7968..9c54aac0583 100644 --- a/Modules/_operator.c +++ b/Modules/_operator.c @@ -1771,19 +1771,17 @@ PyInit__operator(void) if (m == NULL) return NULL; - if (PyType_Ready(&itemgetter_type) < 0) - return NULL; - Py_INCREF(&itemgetter_type); - PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type); + PyTypeObject *types[] = { + &itemgetter_type, + &attrgetter_type, + &methodcaller_type + }; - if (PyType_Ready(&attrgetter_type) < 0) - return NULL; - Py_INCREF(&attrgetter_type); - PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type); + for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) { + if (PyModule_AddType(m, types[i]) < 0) { + return NULL; + } + } - if (PyType_Ready(&methodcaller_type) < 0) - return NULL; - Py_INCREF(&methodcaller_type); - PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type); return m; } diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 6b903da8150..c3385ad3b9e 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -7993,10 +7993,6 @@ PyInit__pickle(void) return m; } - if (PyType_Ready(&Unpickler_Type) < 0) - return NULL; - if (PyType_Ready(&Pickler_Type) < 0) - return NULL; if (PyType_Ready(&Pdata_Type) < 0) return NULL; if (PyType_Ready(&PicklerMemoProxyType) < 0) @@ -8010,16 +8006,15 @@ PyInit__pickle(void) return NULL; /* Add types */ - Py_INCREF(&Pickler_Type); - if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) + if (PyModule_AddType(m, &Pickler_Type) < 0) { return NULL; - Py_INCREF(&Unpickler_Type); - if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) + } + if (PyModule_AddType(m, &Unpickler_Type) < 0) { return NULL; - Py_INCREF(&PyPickleBuffer_Type); - if (PyModule_AddObject(m, "PickleBuffer", - (PyObject *)&PyPickleBuffer_Type) < 0) + } + if (PyModule_AddType(m, &PyPickleBuffer_Type) < 0) { return NULL; + } st = _Pickle_GetState(m); diff --git a/Modules/_queuemodule.c b/Modules/_queuemodule.c index e033da50a5e..5eef06252cd 100644 --- a/Modules/_queuemodule.c +++ b/Modules/_queuemodule.c @@ -390,11 +390,9 @@ PyInit__queue(void) if (PyModule_AddObject(m, "Empty", EmptyError) < 0) return NULL; - if (PyType_Ready(&PySimpleQueueType) < 0) - return NULL; - Py_INCREF(&PySimpleQueueType); - if (PyModule_AddObject(m, "SimpleQueue", (PyObject *)&PySimpleQueueType) < 0) + if (PyModule_AddType(m, &PySimpleQueueType) < 0) { return NULL; + } return m; } diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c index 3bc07b2d8d5..a09c75d4ffa 100644 --- a/Modules/cjkcodecs/multibytecodec.c +++ b/Modules/cjkcodecs/multibytecodec.c @@ -2059,14 +2059,12 @@ static struct PyModuleDef _multibytecodecmodule = { PyMODINIT_FUNC PyInit__multibytecodec(void) { - int i; PyObject *m; PyTypeObject *typelist[] = { &MultibyteIncrementalEncoder_Type, &MultibyteIncrementalDecoder_Type, &MultibyteStreamReader_Type, - &MultibyteStreamWriter_Type, - NULL + &MultibyteStreamWriter_Type }; if (PyType_Ready(&MultibyteCodec_Type) < 0) @@ -2076,12 +2074,10 @@ PyInit__multibytecodec(void) if (m == NULL) return NULL; - for (i = 0; typelist[i] != NULL; i++) { - if (PyType_Ready(typelist[i]) < 0) + for (size_t i = 0; i < Py_ARRAY_LENGTH(typelist); i++) { + if (PyModule_AddType(m, typelist[i]) < 0) { return NULL; - Py_INCREF(typelist[i]); - PyModule_AddObject(m, typelist[i]->tp_name, - (PyObject *)typelist[i]); + } } if (PyErr_Occurred()) { diff --git a/Modules/overlapped.c b/Modules/overlapped.c index 52ed0bc284b..e8029e72f90 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -1860,12 +1860,10 @@ PyInit__overlapped(void) if (initialize_function_pointers() < 0) return NULL; - if (PyType_Ready(&OverlappedType) < 0) - return NULL; - m = PyModule_Create(&overlapped_module); - if (PyModule_AddObject(m, "Overlapped", (PyObject *)&OverlappedType) < 0) + if (PyModule_AddType(m, &OverlappedType) < 0) { return NULL; + } d = PyModule_GetDict(m);