Skip to content

Commit 50f8786

Browse files
committed
MNT: explicitly borrow the PyObject pointer for wrappers
When calling the CPython C api, always explicitly borrow the PyObject pointer.
1 parent 9fe8746 commit 50f8786

3 files changed

Lines changed: 7 additions & 6 deletions

File tree

src/greenlet/greenlet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2392,7 +2392,7 @@ green_repr(BorrowedGreenlet self)
23922392
PyObject* result;
23932393
int never_started = !self->started() && !self->active();
23942394

2395-
const char* const tp_name = Py_TYPE(self)->tp_name;
2395+
const char* const tp_name = Py_TYPE(self.borrow())->tp_name;
23962396

23972397
if (_green_not_dead(self)) {
23982398
/* XXX: The otid= is almost useless because you can't correlate it to

src/greenlet/greenlet_refs.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ namespace greenlet {
990990
"throw() third argument must be a traceback object");
991991
}
992992

993-
if (PyExceptionClass_Check(type)) {
993+
if (PyExceptionClass_Check(type.borrow())) {
994994
// If we just had a type, we'll now have a type and
995995
// instance.
996996
// The type's refcount will have gone up by one
@@ -1000,7 +1000,7 @@ namespace greenlet {
10001000
PyErr_NormalizeException(&type, &instance, &traceback);
10011001

10021002
}
1003-
else if (PyExceptionInstance_Check(type)) {
1003+
else if (PyExceptionInstance_Check(type.borrow())) {
10041004
/* Raising an instance. The value should be a dummy. */
10051005
if (instance && !instance.is_None()) {
10061006
throw PyErrOccurred(

src/greenlet/greenlet_thread_state.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ class ThreadState {
397397
}
398398
else if (refs
399399
&& refs.size() == 1
400-
&& PyCFunction_Check(refs.at(0))
401-
&& Py_REFCNT(refs.at(0)) == 2) {
400+
&& PyCFunction_Check(refs.at(0).borrow())
401+
&& Py_REFCNT(refs.at(0).borrow()) == 2) {
402402
assert(refs.REFCNT() == 1);
403403
// Ok, we found a C method that refers to the
404404
// main greenlet, and its only referenced
@@ -419,7 +419,8 @@ class ThreadState {
419419
if (refs && refs.empty()) {
420420
// Nope, it can't be found so it won't
421421
// ever be GC'd. Drop it.
422-
Py_CLEAR(function_w);
422+
PyObject * tmp = function_w.borrow();
423+
Py_CLEAR(tmp);
423424
}
424425
}
425426
}

0 commit comments

Comments
 (0)