Skip to content

Commit 790f968

Browse files
committed
Use Py_GetType() and %T format
1 parent e8c499a commit 790f968

44 files changed

Lines changed: 550 additions & 348 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Modules/_asynciomodule.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ static PyTypeObject TaskType;
111111
static PyTypeObject PyRunningLoopHolder_Type;
112112

113113

114-
#define Future_CheckExact(obj) (Py_TYPE(obj) == &FutureType)
115-
#define Task_CheckExact(obj) (Py_TYPE(obj) == &TaskType)
114+
#define Future_CheckExact(obj) (Py_TYPE_IS(obj, &FutureType))
115+
#define Task_CheckExact(obj) (Py_TYPE_IS(obj, &TaskType))
116116

117117
#define Future_Check(obj) PyObject_TypeCheck(obj, &FutureType)
118118
#define Task_Check(obj) PyObject_TypeCheck(obj, &TaskType)
@@ -158,9 +158,12 @@ _is_coroutine(PyObject *coro)
158158
positive types. That shouldn't ever happen, unless
159159
someone stressing the system on purpose.
160160
*/
161-
if (PySet_Add(iscoroutine_typecache, (PyObject*) Py_TYPE(coro))) {
161+
PyTypeObject *type = Py_GetType(coro);
162+
if (PySet_Add(iscoroutine_typecache, (PyObject*)type)) {
163+
Py_DECREF(type);
162164
return -1;
163165
}
166+
Py_DECREF(type);
164167
}
165168

166169
return 1;
@@ -183,8 +186,9 @@ is_coroutine(PyObject *coro)
183186
This cache allows us to avoid the cost of even calling
184187
a pure-Python function in 99.9% cases.
185188
*/
186-
int has_it = PySet_Contains(
187-
iscoroutine_typecache, (PyObject*) Py_TYPE(coro));
189+
PyTypeObject *type = Py_GetType(coro);
190+
int has_it = PySet_Contains(iscoroutine_typecache, (PyObject*) type);
191+
Py_DECREF(type);
188192
if (has_it == 0) {
189193
/* type(coro) is not in iscoroutine_typecache */
190194
return _is_coroutine(coro);
@@ -255,7 +259,7 @@ get_running_loop(PyObject **loop)
255259
cached_running_holder_tsid = ts->id;
256260
}
257261

258-
assert(Py_TYPE(rl) == &PyRunningLoopHolder_Type);
262+
assert(Py_TYPE_IS(rl, &PyRunningLoopHolder_Type));
259263
PyObject *running_loop = ((PyRunningLoopHolder *)rl)->rl_loop;
260264

261265
if (running_loop == Py_None) {
@@ -577,7 +581,7 @@ future_set_exception(FutureObj *fut, PyObject *exc)
577581
PyErr_SetString(PyExc_TypeError, "invalid exception object");
578582
return NULL;
579583
}
580-
if ((PyObject*)Py_TYPE(exc_val) == PyExc_StopIteration) {
584+
if (Py_TYPE_IS(exc_val, (PyTypeObject*)PyExc_StopIteration)) {
581585
Py_DECREF(exc_val);
582586
PyErr_SetString(PyExc_TypeError,
583587
"StopIteration interacts badly with generators "
@@ -1308,8 +1312,10 @@ FutureObj_repr(FutureObj *fut)
13081312
}
13091313

13101314
PyObject *rstr = NULL;
1311-
PyObject *type_name = PyObject_GetAttrString((PyObject*)Py_TYPE(fut),
1315+
PyTypeObject *type = Py_GetType(fut);
1316+
PyObject *type_name = PyObject_GetAttrString((PyObject*)type,
13121317
"__name__");
1318+
Py_DECREF(type);
13131319
if (type_name != NULL) {
13141320
rstr = PyUnicode_FromFormat("<%S %U>", type_name, rinfo_s);
13151321
Py_DECREF(type_name);
@@ -1347,7 +1353,9 @@ FutureObj_finalize(FutureObj *fut)
13471353
goto finally;
13481354
}
13491355

1350-
type_name = PyObject_GetAttrString((PyObject*)Py_TYPE(fut), "__name__");
1356+
PyTypeObject *type = Py_GetType(fut);
1357+
type_name = PyObject_GetAttrString((PyObject*)type, "__name__");
1358+
Py_DECREF(type);
13511359
if (type_name == NULL) {
13521360
goto finally;
13531361
}
@@ -1476,7 +1484,9 @@ FutureObj_dealloc(PyObject *self)
14761484
}
14771485

14781486
(void)FutureObj_clear(fut);
1479-
Py_TYPE(fut)->tp_free(fut);
1487+
PyTypeObject *type = Py_GetType(fut);
1488+
type->tp_free(fut);
1489+
Py_DECREF(type);
14801490
}
14811491

14821492

@@ -1702,7 +1712,9 @@ TaskStepMethWrapper_dealloc(TaskStepMethWrapper *o)
17021712
{
17031713
PyObject_GC_UnTrack(o);
17041714
(void)TaskStepMethWrapper_clear(o);
1705-
Py_TYPE(o)->tp_free(o);
1715+
PyTypeObject *type = Py_GetType(o);
1716+
type->tp_free(o);
1717+
Py_DECREF(type);
17061718
}
17071719

17081720
static PyObject *
@@ -1816,7 +1828,9 @@ TaskWakeupMethWrapper_dealloc(TaskWakeupMethWrapper *o)
18161828
{
18171829
PyObject_GC_UnTrack(o);
18181830
(void)TaskWakeupMethWrapper_clear(o);
1819-
Py_TYPE(o)->tp_free(o);
1831+
PyTypeObject *type = Py_GetType(o);
1832+
type->tp_free(o);
1833+
Py_DECREF(type);
18201834
}
18211835

18221836
static PyTypeObject TaskWakeupMethWrapper_Type = {
@@ -2503,7 +2517,9 @@ TaskObj_dealloc(PyObject *self)
25032517
}
25042518

25052519
(void)TaskObj_clear(task);
2506-
Py_TYPE(task)->tp_free(task);
2520+
PyTypeObject *type = Py_GetType(task);
2521+
type->tp_free(task);
2522+
Py_DECREF(type);
25072523
}
25082524

25092525
static int

Modules/_blake2/blake2b_impl.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@ _blake2_blake2b_copy_impl(BLAKE2bObject *self)
249249
{
250250
BLAKE2bObject *cpy;
251251

252-
if ((cpy = new_BLAKE2bObject(Py_TYPE(self))) == NULL)
252+
PyTypeObject *type = Py_GetType(self);
253+
cpy = new_BLAKE2bObject(type);
254+
Py_DECREF(type);
255+
if (cpy == NULL)
253256
return NULL;
254257

255258
ENTER_HASHLIB(self);

Modules/_blake2/blake2s_impl.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,12 @@ _blake2_blake2s_copy_impl(BLAKE2sObject *self)
249249
{
250250
BLAKE2sObject *cpy;
251251

252-
if ((cpy = new_BLAKE2sObject(Py_TYPE(self))) == NULL)
252+
PyTypeObject *type = Py_GetType(self);
253+
if ((cpy = new_BLAKE2sObject(type)) == NULL) {
254+
Py_DECREF(type);
253255
return NULL;
256+
}
257+
Py_DECREF(type);
254258

255259
ENTER_HASHLIB(self);
256260
cpy->param = self->param;

Modules/_bz2module.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ _bz2_BZ2Compressor_flush_impl(BZ2Compressor *self)
267267
static PyObject *
268268
BZ2Compressor_getstate(BZ2Compressor *self, PyObject *noargs)
269269
{
270-
PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
271-
Py_TYPE(self)->tp_name);
270+
PyErr_Format(PyExc_TypeError, "cannot serialize %T object",
271+
self);
272272
return NULL;
273273
}
274274

@@ -341,7 +341,9 @@ BZ2Compressor_dealloc(BZ2Compressor *self)
341341
BZ2_bzCompressEnd(&self->bzs);
342342
if (self->lock != NULL)
343343
PyThread_free_lock(self->lock);
344-
Py_TYPE(self)->tp_free((PyObject *)self);
344+
PyTypeObject *type = Py_GetType(self);
345+
type->tp_free((PyObject *)self);
346+
Py_DECREF(type);
345347
}
346348

347349
static PyMethodDef BZ2Compressor_methods[] = {
@@ -615,8 +617,8 @@ _bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
615617
static PyObject *
616618
BZ2Decompressor_getstate(BZ2Decompressor *self, PyObject *noargs)
617619
{
618-
PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
619-
Py_TYPE(self)->tp_name);
620+
PyErr_Format(PyExc_TypeError, "cannot serialize %T object",
621+
self);
620622
return NULL;
621623
}
622624

@@ -674,7 +676,9 @@ BZ2Decompressor_dealloc(BZ2Decompressor *self)
674676
Py_CLEAR(self->unused_data);
675677
if (self->lock != NULL)
676678
PyThread_free_lock(self->lock);
677-
Py_TYPE(self)->tp_free((PyObject *)self);
679+
PyTypeObject *type = Py_GetType(self);
680+
type->tp_free((PyObject *)self);
681+
Py_DECREF(type);
678682
}
679683

680684
static PyMethodDef BZ2Decompressor_methods[] = {

Modules/_csv.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ static void
312312
Dialect_dealloc(DialectObj *self)
313313
{
314314
Py_XDECREF(self->lineterminator);
315-
Py_TYPE(self)->tp_free((PyObject *)self);
315+
PyTypeObject *type = Py_GetType(self);
316+
type->tp_free((PyObject *)self);
317+
Py_DECREF(type);
316318
}
317319

318320
static char *dialect_kws[] = {

0 commit comments

Comments
 (0)