Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-105927: Fix test_weakref_capi() refleak #105966

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3395,57 +3395,73 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
Py_DECREF(obj);
return NULL;
}

// test PyWeakref_Check(), valid weakref object
assert(PyWeakref_Check(weakref));
assert(PyWeakref_CheckRefExact(weakref));
assert(PyWeakref_CheckRefExact(weakref));
assert(Py_REFCNT(obj) == refcnt);

// test PyWeakref_GetRef(), reference is alive
PyObject *ref1;
assert(PyWeakref_GetRef(weakref, &ref1) == 0);
assert(ref1 == obj);
PyObject *ref = Py_True; // marker to check that value was set
assert(PyWeakref_GetRef(weakref, &ref) == 0);
assert(ref == obj);
assert(Py_REFCNT(obj) == (refcnt + 1));
Py_DECREF(ref1);
Py_DECREF(ref);

// test PyWeakref_GetObject(), reference is alive
PyObject *ref2 = PyWeakref_GetObject(weakref);
assert(ref2 == obj);
ref = PyWeakref_GetObject(weakref); // borrowed ref
assert(ref == obj);

// test PyWeakref_GET_OBJECT(), reference is alive
PyObject *ref3 = PyWeakref_GET_OBJECT(weakref);
assert(ref3 == obj);
ref = PyWeakref_GET_OBJECT(weakref); // borrowed ref
assert(ref == obj);

// delete the referenced object
// delete the referenced object: clear the weakref
assert(Py_REFCNT(obj) == 1);
Py_DECREF(obj);

// test PyWeakref_GET_OBJECT(), reference is dead
assert(PyWeakref_GET_OBJECT(weakref) == Py_None);

// test PyWeakref_GetRef(), reference is dead
PyObject *ref4 = Py_True; // marker to check that value was set
assert(PyWeakref_GetRef(weakref, &ref4) == 0);
assert(ref4 == NULL);
ref = Py_True;
assert(PyWeakref_GetRef(weakref, &ref) == 0);
assert(ref == NULL);

// None is not a weak reference object
// test PyWeakref_Check(), not a weakref object
PyObject *invalid_weakref = Py_None;
assert(!PyWeakref_Check(invalid_weakref));
assert(!PyWeakref_CheckRefExact(invalid_weakref));
assert(!PyWeakref_CheckRefExact(invalid_weakref));

// test PyWeakref_GetRef(), invalid type
assert(!PyErr_Occurred());
PyObject *ref5 = Py_True; // marker to check that value was set
assert(PyWeakref_GetRef(invalid_weakref, &ref5) == -1);
ref = Py_True;
assert(PyWeakref_GetRef(invalid_weakref, &ref) == -1);
assert(PyErr_ExceptionMatches(PyExc_TypeError));
PyErr_Clear();
assert(ref5 == NULL);
assert(ref == NULL);

// test PyWeakref_GetObject(), invalid type
assert(PyWeakref_GetObject(invalid_weakref) == NULL);
assert(PyErr_ExceptionMatches(PyExc_SystemError));
PyErr_Clear();

// test PyWeakref_GetRef(NULL)
ref = Py_True; // marker to check that value was set
assert(PyWeakref_GetRef(NULL, &ref) == -1);
assert(PyErr_ExceptionMatches(PyExc_SystemError));
assert(ref == NULL);
PyErr_Clear();

// test PyWeakref_GetObject(NULL)
assert(PyWeakref_GetObject(NULL) == NULL);
assert(PyErr_ExceptionMatches(PyExc_SystemError));
PyErr_Clear();

Py_DECREF(weakref);

Py_RETURN_NONE;
}

Expand Down
Loading