Skip to content

Commit a158036

Browse files
authored
GH-47823: [Python] Use PyWeakref_GetRef instead of PyWeakref_GET_OBJECT (Python 3.15) (#48027)
### Rationale for this change pyarrow builds fail on CPython 3.15 due to the 3.15's removal of PyWeakref_GetObject, used in extension_types.cc. This was deprecated in 3.13. A backport is available in the already-vendored [pythoncapi_compat](https://github.com/apache/arrow/blob/main/python/pyarrow/src/arrow/python/vendored/pythoncapi_compat.h). Fixes #47823. To be clear: this fixes only the build issue reported in the issue, not "3.15 support". ### What changes are included in this PR? Replaces the sole use of PyWeakref_GET_OBJECT with the backported version of PyWeakref_GetRef. This follows the recommendation from [Pending removal in Python 3.15](https://docs.python.org/3/deprecations/c-api-pending-removal-in-3.15.html) > [PyWeakref_GetObject()](https://docs.python.org/3/c-api/weakref.html#c.PyWeakref_GetObject) and [PyWeakref_GET_OBJECT()](https://docs.python.org/3/c-api/weakref.html#c.PyWeakref_GET_OBJECT): Use [PyWeakref_GetRef()](https://docs.python.org/3/c-api/weakref.html#c.PyWeakref_GetRef) instead. The [pythoncapi-compat project](https://github.com/python/pythoncapi-compat/) can be used to get [PyWeakref_GetRef()](https://docs.python.org/3/c-api/weakref.html#c.PyWeakref_GetRef) on Python 3.12 and older. ### Are these changes tested? Build and tested on a local Ubuntu 24.04 build. ### Are there any user-facing changes? No user-facing changes. * GitHub Issue: #47823 Authored-by: paultiq <[email protected]> Signed-off-by: AlenkaF <[email protected]>
1 parent 1f8cec3 commit a158036

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

python/pyarrow/src/arrow/python/extension_type.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "arrow/python/extension_type.h"
2323
#include "arrow/python/helpers.h"
2424
#include "arrow/python/pyarrow.h"
25+
#include "arrow/python/vendored/pythoncapi_compat.h"
2526
#include "arrow/util/checked_cast.h"
2627
#include "arrow/util/logging.h"
2728

@@ -164,15 +165,18 @@ PyObject* PyExtensionType::GetInstance() const {
164165
return nullptr;
165166
}
166167
ARROW_DCHECK(PyWeakref_CheckRef(type_instance_.obj()));
167-
PyObject* inst = PyWeakref_GET_OBJECT(type_instance_.obj());
168-
if (inst != Py_None) {
169-
// Cached instance still alive
170-
Py_INCREF(inst);
168+
PyObject* inst = NULL;
169+
int result = PyWeakref_GetRef(type_instance_.obj(), &inst);
170+
if (result == 1) {
171+
// Alive: inst is a new strong reference
171172
return inst;
172-
} else {
173-
// Must reconstruct from serialized form
173+
} else if (result == 0) {
174+
// Weakref is dead, must reconstruct from serialized form
174175
// XXX cache again?
175176
return DeserializeExtInstance(type_class_.obj(), storage_type_, serialized_);
177+
} else {
178+
// -1 = exception
179+
return nullptr;
176180
}
177181
}
178182

0 commit comments

Comments
 (0)