Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2e6d0a

Browse files
committed
Replaces some pybind11::handle uses by type deduction.
This change is done to prevent slicing `pybind11::object` values (or its subclasses) into `pybind11::handle`, which may result into handles to garbage collected objects.
1 parent 9dbe864 commit f2e6d0a

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/pyobject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ Object toObject(const ::py::object& obj)
333333
: ObjectThreadingModel_SingleThread);
334334

335335
const auto attrKeys = ::py::reinterpret_steal<::py::list>(PyObject_Dir(obj.ptr()));
336-
for (const ::py::handle& pyAttrKey : attrKeys)
336+
for (const auto& pyAttrKey : attrKeys)
337337
{
338338
QI_ASSERT_TRUE(pyAttrKey);
339339
QI_ASSERT_FALSE(pyAttrKey.is_none());
@@ -342,7 +342,7 @@ Object toObject(const ::py::object& obj)
342342
const auto attrKey = pyAttrKey.cast<std::string>();
343343
auto memberName = attrKey;
344344

345-
const ::py::object attr = obj.attr(pyAttrKey);
345+
const auto attr = obj.attr(pyAttrKey);
346346
if (attr.is_none())
347347
{
348348
qiLogVerbose() << "The object attribute '" << attrKey

src/pystrand.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ bool isUnboundMethod(const ::py::function& func, const ::py::object& object)
3535

3636
GILAcquire lock;
3737
const auto dir = ::py::reinterpret_steal<::py::list>(PyObject_Dir(object.ptr()));
38-
for (const ::py::handle attrName : dir)
38+
for (const auto& attrName : dir)
3939
{
40-
const ::py::handle attr = object.attr(attrName);
40+
const auto attr = object.attr(attrName);
4141
QI_ASSERT_TRUE(attr);
4242

4343
if (!PyMethod_Check(attr.ptr()))
4444
continue;
4545

46-
const ::py::handle unboundMethod = PyMethod_GET_FUNCTION(attr.ptr());
46+
const auto unboundMethod = ::py::reinterpret_borrow<::py::object>(PyMethod_GET_FUNCTION(attr.ptr()));
4747
if (func.is(unboundMethod))
4848
return true;
4949
}

0 commit comments

Comments
 (0)