diff --git a/python/pyarrow/src/arrow/python/extension_type.cc b/python/pyarrow/src/arrow/python/extension_type.cc index 8439ecf8583..8c3f3f1d8d0 100644 --- a/python/pyarrow/src/arrow/python/extension_type.cc +++ b/python/pyarrow/src/arrow/python/extension_type.cc @@ -22,6 +22,7 @@ #include "arrow/python/extension_type.h" #include "arrow/python/helpers.h" #include "arrow/python/pyarrow.h" +#include "arrow/python/vendored/pythoncapi_compat.h" #include "arrow/util/checked_cast.h" #include "arrow/util/logging.h" @@ -164,15 +165,18 @@ PyObject* PyExtensionType::GetInstance() const { return nullptr; } ARROW_DCHECK(PyWeakref_CheckRef(type_instance_.obj())); - PyObject* inst = PyWeakref_GET_OBJECT(type_instance_.obj()); - if (inst != Py_None) { - // Cached instance still alive - Py_INCREF(inst); + PyObject* inst = NULL; + int result = PyWeakref_GetRef(type_instance_.obj(), &inst); + if (result == 1) { + // Alive: inst is a new strong reference return inst; - } else { - // Must reconstruct from serialized form + } else if (result == 0) { + // Weakref is dead, must reconstruct from serialized form // XXX cache again? return DeserializeExtInstance(type_class_.obj(), storage_type_, serialized_); + } else { + // -1 = exception + return nullptr; } }