@@ -65,7 +65,11 @@ getdents_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
6565 return NULL ;
6666 }
6767
68- struct getdents_state * state = (void * ) type -> tp_alloc (type , 0 );
68+ allocfunc tp_alloc = PyType_GetSlot (type , Py_tp_alloc );
69+
70+ assert (tp_alloc != NULL );
71+
72+ struct getdents_state * state = (void * ) tp_alloc (type , 0 );
6973
7074 if (!state )
7175 return NULL ;
@@ -86,8 +90,14 @@ getdents_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
8690static void
8791getdents_dealloc (struct getdents_state * state )
8892{
93+ PyTypeObject * tp = Py_TYPE (state );
94+ freefunc tp_free = PyType_GetSlot (tp , Py_tp_free );
95+
96+ assert (tp_free != NULL );
97+
8998 free (state -> buff );
90- Py_TYPE (state )-> tp_free (state );
99+ tp_free (state );
100+ Py_DECREF (tp );
91101}
92102
93103static PyObject *
@@ -117,67 +127,43 @@ getdents_next(struct getdents_state *s)
117127 return result ;
118128}
119129
120- PyTypeObject getdents_type = {
121- PyVarObject_HEAD_INIT (NULL , 0 )
122- "getdents_raw" , /* tp_name */
123- sizeof (struct getdents_state ), /* tp_basicsize */
124- 0 , /* tp_itemsize */
125- (destructor ) getdents_dealloc , /* tp_dealloc */
126- 0 , /* tp_print */
127- 0 , /* tp_getattr */
128- 0 , /* tp_setattr */
129- 0 , /* tp_reserved */
130- 0 , /* tp_repr */
131- 0 , /* tp_as_number */
132- 0 , /* tp_as_sequence */
133- 0 , /* tp_as_mapping */
134- 0 , /* tp_hash */
135- 0 , /* tp_call */
136- 0 , /* tp_str */
137- 0 , /* tp_getattro */
138- 0 , /* tp_setattro */
139- 0 , /* tp_as_buffer */
140- Py_TPFLAGS_DEFAULT , /* tp_flags */
141- 0 , /* tp_doc */
142- 0 , /* tp_traverse */
143- 0 , /* tp_clear */
144- 0 , /* tp_richcompare */
145- 0 , /* tp_weaklistoffset */
146- PyObject_SelfIter , /* tp_iter */
147- (iternextfunc ) getdents_next , /* tp_iternext */
148- 0 , /* tp_methods */
149- 0 , /* tp_members */
150- 0 , /* tp_getset */
151- 0 , /* tp_base */
152- 0 , /* tp_dict */
153- 0 , /* tp_descr_get */
154- 0 , /* tp_descr_set */
155- 0 , /* tp_dictoffset */
156- 0 , /* tp_init */
157- PyType_GenericAlloc , /* tp_alloc */
158- getdents_new , /* tp_new */
130+ static PyType_Slot getdents_type_slots [] = {
131+ {Py_tp_alloc , PyType_GenericAlloc },
132+ {Py_tp_dealloc , getdents_dealloc },
133+ {Py_tp_iter , PyObject_SelfIter },
134+ {Py_tp_iternext , getdents_next },
135+ {Py_tp_new , getdents_new },
136+ {0 , 0 },
137+ };
138+
139+ static PyType_Spec getdents_type_spec = {
140+ .name = "getdents.getdents_raw" ,
141+ .basicsize = sizeof (struct getdents_state ),
142+ .flags = Py_TPFLAGS_DEFAULT ,
143+ .slots = getdents_type_slots ,
159144};
160145
161146static struct PyModuleDef getdents_module = {
162147 PyModuleDef_HEAD_INIT ,
163- "getdents" , /* m_name */
164- "" , /* m_doc */
165- -1 , /* m_size */
148+ . m_name = "getdents" ,
149+ . m_doc = "" ,
150+ . m_size = -1 ,
166151};
167152
168153PyMODINIT_FUNC
169154PyInit__getdents (void )
170155{
171- if (PyType_Ready (& getdents_type ) < 0 )
172- return NULL ;
173-
174156 PyObject * module = PyModule_Create (& getdents_module );
175157
176158 if (!module )
177159 return NULL ;
178160
179- Py_INCREF (& getdents_type );
180- PyModule_AddObject (module , "getdents_raw" , (PyObject * ) & getdents_type );
161+ PyObject * getdents_raw = PyType_FromSpec (& getdents_type_spec );
162+
163+ if (!getdents_raw )
164+ return NULL ;
165+
166+ PyModule_AddObject (module , "getdents_raw" , getdents_raw );
181167 PyModule_AddIntMacro (module , DT_BLK );
182168 PyModule_AddIntMacro (module , DT_CHR );
183169 PyModule_AddIntMacro (module , DT_DIR );
0 commit comments