@@ -65,7 +65,11 @@ getdents_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
65
65
return NULL ;
66
66
}
67
67
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 );
69
73
70
74
if (!state )
71
75
return NULL ;
@@ -86,8 +90,14 @@ getdents_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
86
90
static void
87
91
getdents_dealloc (struct getdents_state * state )
88
92
{
93
+ PyTypeObject * tp = Py_TYPE (state );
94
+ freefunc tp_free = PyType_GetSlot (tp , Py_tp_free );
95
+
96
+ assert (tp_free != NULL );
97
+
89
98
free (state -> buff );
90
- Py_TYPE (state )-> tp_free (state );
99
+ tp_free (state );
100
+ Py_DECREF (tp );
91
101
}
92
102
93
103
static PyObject *
@@ -117,67 +127,43 @@ getdents_next(struct getdents_state *s)
117
127
return result ;
118
128
}
119
129
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 ,
159
144
};
160
145
161
146
static struct PyModuleDef getdents_module = {
162
147
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 ,
166
151
};
167
152
168
153
PyMODINIT_FUNC
169
154
PyInit__getdents (void )
170
155
{
171
- if (PyType_Ready (& getdents_type ) < 0 )
172
- return NULL ;
173
-
174
156
PyObject * module = PyModule_Create (& getdents_module );
175
157
176
158
if (!module )
177
159
return NULL ;
178
160
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 );
181
167
PyModule_AddIntMacro (module , DT_BLK );
182
168
PyModule_AddIntMacro (module , DT_CHR );
183
169
PyModule_AddIntMacro (module , DT_DIR );
0 commit comments