@@ -2546,24 +2546,59 @@ static PyTypeObject tuplegetter_type = {
25462546
25472547/* module level code ********************************************************/
25482548
2549- PyDoc_STRVAR (module_doc ,
2549+ PyDoc_STRVAR (collections_doc ,
25502550"High performance data structures.\n\
25512551- deque: ordered collection accessible from endpoints only\n\
25522552- defaultdict: dict subclass with a default value factory\n\
25532553" );
25542554
2555- static struct PyMethodDef module_functions [] = {
2555+ static struct PyMethodDef collections_methods [] = {
25562556 _COLLECTIONS__COUNT_ELEMENTS_METHODDEF
25572557 {NULL , NULL } /* sentinel */
25582558};
25592559
2560+ static int
2561+ collections_exec (PyObject * module ) {
2562+ PyTypeObject * typelist [] = {
2563+ & deque_type ,
2564+ & defdict_type ,
2565+ & PyODict_Type ,
2566+ & dequeiter_type ,
2567+ & dequereviter_type ,
2568+ & tuplegetter_type ,
2569+ NULL ,
2570+ };
2571+
2572+ defdict_type .tp_base = & PyDict_Type ;
2573+
2574+ for (int i = 0 ; typelist [i ] != NULL ; i ++ ) {
2575+ PyTypeObject * type = typelist [i ];
2576+ if (PyType_Ready (type ) < 0 ) {
2577+ return -1 ;
2578+ }
2579+ const char * name = _PyType_Name (type );
2580+ Py_INCREF (type );
2581+ if (PyModule_AddObject (module , name , (PyObject * )type ) < 0 ) {
2582+ Py_DECREF (type );
2583+ return -1 ;
2584+ }
2585+ }
2586+
2587+ return 0 ;
2588+ }
2589+
2590+ static struct PyModuleDef_Slot collections_slots [] = {
2591+ {Py_mod_exec , collections_exec },
2592+ {0 , NULL }
2593+ };
2594+
25602595static struct PyModuleDef _collectionsmodule = {
25612596 PyModuleDef_HEAD_INIT ,
25622597 "_collections" ,
2563- module_doc ,
2564- -1 ,
2565- module_functions ,
2566- NULL ,
2598+ collections_doc ,
2599+ 0 ,
2600+ collections_methods ,
2601+ collections_slots ,
25672602 NULL ,
25682603 NULL ,
25692604 NULL
@@ -2572,40 +2607,5 @@ static struct PyModuleDef _collectionsmodule = {
25722607PyMODINIT_FUNC
25732608PyInit__collections (void )
25742609{
2575- PyObject * m ;
2576-
2577- m = PyModule_Create (& _collectionsmodule );
2578- if (m == NULL )
2579- return NULL ;
2580-
2581- if (PyType_Ready (& deque_type ) < 0 )
2582- return NULL ;
2583- Py_INCREF (& deque_type );
2584- PyModule_AddObject (m , "deque" , (PyObject * )& deque_type );
2585-
2586- defdict_type .tp_base = & PyDict_Type ;
2587- if (PyType_Ready (& defdict_type ) < 0 )
2588- return NULL ;
2589- Py_INCREF (& defdict_type );
2590- PyModule_AddObject (m , "defaultdict" , (PyObject * )& defdict_type );
2591-
2592- Py_INCREF (& PyODict_Type );
2593- PyModule_AddObject (m , "OrderedDict" , (PyObject * )& PyODict_Type );
2594-
2595- if (PyType_Ready (& dequeiter_type ) < 0 )
2596- return NULL ;
2597- Py_INCREF (& dequeiter_type );
2598- PyModule_AddObject (m , "_deque_iterator" , (PyObject * )& dequeiter_type );
2599-
2600- if (PyType_Ready (& dequereviter_type ) < 0 )
2601- return NULL ;
2602- Py_INCREF (& dequereviter_type );
2603- PyModule_AddObject (m , "_deque_reverse_iterator" , (PyObject * )& dequereviter_type );
2604-
2605- if (PyType_Ready (& tuplegetter_type ) < 0 )
2606- return NULL ;
2607- Py_INCREF (& tuplegetter_type );
2608- PyModule_AddObject (m , "_tuplegetter" , (PyObject * )& tuplegetter_type );
2609-
2610- return m ;
2610+ return PyModuleDef_Init (& _collectionsmodule );
26112611}
0 commit comments