Skip to content

Commit 22dfb5b

Browse files
williamwen42pytorchmergebot
authored andcommitted
[dynamo, 3.13] replace deprecated PyWeakref_GetObject (pytorch#140187)
Pull Request resolved: pytorch#140187 Approved by: https://github.com/jansel
1 parent 03cccaa commit 22dfb5b

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

torch/csrc/dynamo/guards.cpp

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,8 +3444,19 @@ class GlobalWeakRefGuardAccessor : public GuardAccessor {
34443444
return false;
34453445
}
34463446

3447-
PyObject* x = PyWeakref_GetObject(weakref); // borrowed ref
3448-
return _guard_manager->check_nopybind(x);
3447+
PyObject* x = nullptr;
3448+
if (PyWeakref_GetRef(weakref, &x) == -1) { // strong reference
3449+
// error when attempting to call ref
3450+
PyErr_Clear();
3451+
return false;
3452+
}
3453+
if (x == nullptr) {
3454+
// weakref is dead
3455+
x = Py_NewRef(Py_None);
3456+
}
3457+
bool result = _guard_manager->check_nopybind(x);
3458+
Py_DECREF(x);
3459+
return result;
34493460
}
34503461

34513462
GuardDebugInfo check_verbose_nopybind(
@@ -3465,8 +3476,20 @@ class GlobalWeakRefGuardAccessor : public GuardAccessor {
34653476
false, std::string("Not a weakref ") + get_source(), 0);
34663477
}
34673478

3468-
PyObject* x = PyWeakref_GetObject(weakref); // borrowed ref
3469-
return _guard_manager->check_verbose_nopybind(x);
3479+
PyObject* x = nullptr;
3480+
if (PyWeakref_GetRef(weakref, &x) == -1) { // strong reference
3481+
// error when attempting to call ref
3482+
PyErr_Clear();
3483+
return GuardDebugInfo(
3484+
false, std::string("Weakref_GetRef failed ") + get_source(), 0);
3485+
}
3486+
if (x == nullptr) {
3487+
// weakref is dead
3488+
x = Py_NewRef(Py_None);
3489+
}
3490+
auto result = _guard_manager->check_verbose_nopybind(x);
3491+
Py_DECREF(x);
3492+
return result;
34703493
}
34713494

34723495
std::string repr() const override {
@@ -3504,8 +3527,19 @@ class WeakRefCallGuardAccessor : public GuardAccessor {
35043527
return false;
35053528
}
35063529

3507-
PyObject* x = PyWeakref_GetObject(obj); // borrowed ref
3508-
return _guard_manager->check_nopybind(x);
3530+
PyObject* x = nullptr;
3531+
if (PyWeakref_GetRef(obj, &x) == -1) { // strong reference
3532+
// error when attempting to call ref
3533+
PyErr_Clear();
3534+
return false;
3535+
}
3536+
if (x == nullptr) {
3537+
// weakref is dead
3538+
x = Py_NewRef(Py_None);
3539+
}
3540+
bool result = _guard_manager->check_nopybind(x);
3541+
Py_DECREF(x);
3542+
return result;
35093543
}
35103544

35113545
GuardDebugInfo check_verbose_nopybind(
@@ -3515,8 +3549,20 @@ class WeakRefCallGuardAccessor : public GuardAccessor {
35153549
false, std::string("Not a weakref obj ") + get_source(), 0);
35163550
}
35173551

3518-
PyObject* x = PyWeakref_GetObject(obj); // borrowed ref
3519-
return _guard_manager->check_verbose_nopybind(x);
3552+
PyObject* x = nullptr;
3553+
if (PyWeakref_GetRef(obj, &x) == -1) { // strong reference
3554+
// error when attempting to call ref
3555+
PyErr_Clear();
3556+
return GuardDebugInfo(
3557+
false, std::string("Weakref_GetRef failed ") + get_source(), 0);
3558+
}
3559+
if (x == nullptr) {
3560+
// weakref is dead
3561+
x = Py_NewRef(Py_None);
3562+
}
3563+
auto result = _guard_manager->check_verbose_nopybind(x);
3564+
Py_DECREF(x);
3565+
return result;
35203566
}
35213567

35223568
std::string repr() const override {

0 commit comments

Comments
 (0)