From 22dfb5b6cf9a9a92ff1f6c411a285dc1b2dd9f75 Mon Sep 17 00:00:00 2001 From: William Wen Date: Tue, 12 Nov 2024 13:14:10 -0800 Subject: [PATCH] [dynamo, 3.13] replace deprecated PyWeakref_GetObject (#140187) Pull Request resolved: https://github.com/pytorch/pytorch/pull/140187 Approved by: https://github.com/jansel --- torch/csrc/dynamo/guards.cpp | 62 +++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/torch/csrc/dynamo/guards.cpp b/torch/csrc/dynamo/guards.cpp index c39ac420ced0b0..24aea7ff95508e 100644 --- a/torch/csrc/dynamo/guards.cpp +++ b/torch/csrc/dynamo/guards.cpp @@ -3444,8 +3444,19 @@ class GlobalWeakRefGuardAccessor : public GuardAccessor { return false; } - PyObject* x = PyWeakref_GetObject(weakref); // borrowed ref - return _guard_manager->check_nopybind(x); + PyObject* x = nullptr; + if (PyWeakref_GetRef(weakref, &x) == -1) { // strong reference + // error when attempting to call ref + PyErr_Clear(); + return false; + } + if (x == nullptr) { + // weakref is dead + x = Py_NewRef(Py_None); + } + bool result = _guard_manager->check_nopybind(x); + Py_DECREF(x); + return result; } GuardDebugInfo check_verbose_nopybind( @@ -3465,8 +3476,20 @@ class GlobalWeakRefGuardAccessor : public GuardAccessor { false, std::string("Not a weakref ") + get_source(), 0); } - PyObject* x = PyWeakref_GetObject(weakref); // borrowed ref - return _guard_manager->check_verbose_nopybind(x); + PyObject* x = nullptr; + if (PyWeakref_GetRef(weakref, &x) == -1) { // strong reference + // error when attempting to call ref + PyErr_Clear(); + return GuardDebugInfo( + false, std::string("Weakref_GetRef failed ") + get_source(), 0); + } + if (x == nullptr) { + // weakref is dead + x = Py_NewRef(Py_None); + } + auto result = _guard_manager->check_verbose_nopybind(x); + Py_DECREF(x); + return result; } std::string repr() const override { @@ -3504,8 +3527,19 @@ class WeakRefCallGuardAccessor : public GuardAccessor { return false; } - PyObject* x = PyWeakref_GetObject(obj); // borrowed ref - return _guard_manager->check_nopybind(x); + PyObject* x = nullptr; + if (PyWeakref_GetRef(obj, &x) == -1) { // strong reference + // error when attempting to call ref + PyErr_Clear(); + return false; + } + if (x == nullptr) { + // weakref is dead + x = Py_NewRef(Py_None); + } + bool result = _guard_manager->check_nopybind(x); + Py_DECREF(x); + return result; } GuardDebugInfo check_verbose_nopybind( @@ -3515,8 +3549,20 @@ class WeakRefCallGuardAccessor : public GuardAccessor { false, std::string("Not a weakref obj ") + get_source(), 0); } - PyObject* x = PyWeakref_GetObject(obj); // borrowed ref - return _guard_manager->check_verbose_nopybind(x); + PyObject* x = nullptr; + if (PyWeakref_GetRef(obj, &x) == -1) { // strong reference + // error when attempting to call ref + PyErr_Clear(); + return GuardDebugInfo( + false, std::string("Weakref_GetRef failed ") + get_source(), 0); + } + if (x == nullptr) { + // weakref is dead + x = Py_NewRef(Py_None); + } + auto result = _guard_manager->check_verbose_nopybind(x); + Py_DECREF(x); + return result; } std::string repr() const override {