Skip to content

Commit 1fc90df

Browse files
committed
Address review suggestions
1 parent b5fab45 commit 1fc90df

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

‎Objects/typeobject.c‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -560,22 +560,22 @@ set_tp_bases(PyTypeObject *self, PyObject *bases)
560560
self->tp_bases = bases;
561561
}
562562

563-
// Replace tp_bases and tp_base of a type with the world stopped. Returns 1
564-
// on success and 0 if tp_bases is no longer 'expected'. In the latter case
563+
// Replace tp_bases and tp_base of a type with the world stopped. Returns true
564+
// on success and false if tp_bases is no longer 'expected'. In the latter case
565565
// the caller still owns 'bases' and 'base'.
566-
static int
566+
static bool
567567
try_set_tp_bases(PyTypeObject *self, PyObject *expected,
568568
PyObject *bases, PyTypeObject *base)
569569
{
570570
ASSERT_TYPE_LOCK_HELD();
571571
assert(PyTuple_Check(bases));
572572
assert(!(self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN));
573-
int res = 0;
573+
bool res = false;
574574
types_stop_world();
575575
if (self->tp_bases == expected) {
576576
self->tp_bases = bases;
577577
self->tp_base = base;
578-
res = 1;
578+
res = true;
579579
}
580580
types_start_world();
581581
ASSERT_TYPE_LOCK_HELD();
@@ -628,23 +628,23 @@ set_tp_mro(PyTypeObject *self, PyObject *mro)
628628
self->tp_mro = mro;
629629
}
630630

631-
// Replace tp_mro of a type with the world stopped. Returns 1 on success and
632-
// 0 if tp_mro is no longer 'expected'. In the latter case the caller still
631+
// Replace tp_mro of a type with the world stopped. Returns true on success and
632+
// false if tp_mro is no longer 'expected'. In the latter case the caller still
633633
// owns 'mro'.
634-
static int
634+
static bool
635635
try_set_tp_mro(PyTypeObject *self, PyObject *expected, PyObject *mro)
636636
{
637637
ASSERT_TYPE_LOCK_HELD();
638638
assert(!(self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN));
639639
assert(mro == NULL || PyTuple_CheckExact(mro));
640-
int res = 0;
640+
bool res = false;
641641
types_stop_world();
642642
if (self->tp_mro == expected) {
643643
if (mro != NULL) {
644644
PyUnstable_Object_EnableDeferredRefcount(mro);
645645
}
646646
self->tp_mro = mro;
647-
res = 1;
647+
res = true;
648648
}
649649
types_start_world();
650650
ASSERT_TYPE_LOCK_HELD();
@@ -1768,7 +1768,7 @@ mro_hierarchy_for_complete_type(PyTypeObject *type, PyObject *temp)
17681768

17691769
if (res < 0) {
17701770
// Roll back, unless tp_mro has already been replaced again.
1771-
if (try_set_tp_mro(type, new_mro, old_mro) == 1) {
1771+
if (try_set_tp_mro(type, new_mro, old_mro)) {
17721772
Py_DECREF(new_mro);
17731773
}
17741774
else {
@@ -2014,7 +2014,7 @@ type_set_bases_unlocked(PyTypeObject *type, PyObject *new_bases)
20142014
"", 2, 3, &cls, &new_mro, &old_mro);
20152015
/* Do not rollback if cls has a newer version of MRO. */
20162016
old_mro = Py_XNewRef(old_mro);
2017-
if (try_set_tp_mro(cls, new_mro, old_mro) == 1) {
2017+
if (try_set_tp_mro(cls, new_mro, old_mro)) {
20182018
Py_DECREF(new_mro);
20192019
}
20202020
else {
@@ -2025,8 +2025,7 @@ type_set_bases_unlocked(PyTypeObject *type, PyObject *new_bases)
20252025

20262026
bail:
20272027
/* Restore the old bases, unless tp_bases has been replaced again. */
2028-
if (try_set_tp_bases(type, new_bases, old_bases, old_base) == 1)
2029-
{
2028+
if (try_set_tp_bases(type, new_bases, old_bases, old_base)) {
20302029
Py_DECREF(new_bases);
20312030
Py_DECREF(best_base);
20322031
}
@@ -2051,13 +2050,10 @@ type_set_bases(PyObject *tp, PyObject *new_bases, void *Py_UNUSED(closure))
20512050
// tp_bases was replaced again while TYPE_LOCK was released or due to
20522051
// re-entrancy from the mro() call.
20532052
Py_ssize_t n = PyTuple_GET_SIZE(new_bases);
2054-
PyObject *bases = PyTuple_New(n);
2053+
PyObject *bases = PyTuple_FromArray(((PyTupleObject *)new_bases)->ob_item, n);
20552054
if (bases == NULL) {
20562055
return -1;
20572056
}
2058-
for (Py_ssize_t i = 0; i < n; i++) {
2059-
PyTuple_SET_ITEM(bases, i, Py_NewRef(PyTuple_GET_ITEM(new_bases, i)));
2060-
}
20612057
BEGIN_TYPE_LOCK();
20622058
res = type_set_bases_unlocked(type, bases);
20632059
END_TYPE_LOCK();

0 commit comments

Comments
 (0)