Skip to content

Commit

Permalink
[dynamo, 3.13] replace deprecated PyWeakref_GetObject (pytorch#140187)
Browse files Browse the repository at this point in the history
Pull Request resolved: pytorch#140187
Approved by: https://github.com/jansel
  • Loading branch information
williamwen42 authored and pytorchmergebot committed Nov 13, 2024
1 parent 03cccaa commit 22dfb5b
Showing 1 changed file with 54 additions and 8 deletions.
62 changes: 54 additions & 8 deletions torch/csrc/dynamo/guards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand Down

0 comments on commit 22dfb5b

Please sign in to comment.