Skip to content

Commit 4589377

Browse files
committed
Enhance tests
1 parent ee5d23a commit 4589377

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

Lib/test/test_weakref.py

-4
Original file line numberDiff line numberDiff line change
@@ -967,10 +967,6 @@ def __del__(self): pass
967967
del x
968968
support.gc_collect()
969969

970-
@support.cpython_only
971-
def test_capi(self):
972-
import _testcapi
973-
_testcapi.check_weakref_capi(C)
974970

975971
class SubclassableWeakrefTestCase(TestBase):
976972

Modules/_testcapimodule.c

+26-15
Original file line numberDiff line numberDiff line change
@@ -3373,17 +3373,23 @@ check_pyimport_addmodule(PyObject *self, PyObject *args)
33733373

33743374

33753375
static PyObject *
3376-
check_weakref_capi(PyObject *self, PyObject *factory)
3376+
test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
33773377
{
3378-
// obj = factory()
3379-
PyObject *obj = PyObject_CallNoArgs(factory);
3378+
// Create a new heap type, create an instance of this type, and delete the
3379+
// type. This object supports weak references.
3380+
PyObject *new_type = PyObject_CallFunction((PyObject*)&PyType_Type,
3381+
"s(){}", "TypeName");
3382+
if (new_type == NULL) {
3383+
return NULL;
3384+
}
3385+
PyObject *obj = PyObject_CallNoArgs(new_type);
3386+
Py_DECREF(new_type);
33803387
if (obj == NULL) {
33813388
return NULL;
33823389
}
33833390
Py_ssize_t refcnt = Py_REFCNT(obj);
3384-
assert(refcnt == 1);
33853391

3386-
// test PyWeakref_NewRef()
3392+
// test PyWeakref_NewRef(), reference is alive
33873393
PyObject *weakref = PyWeakref_NewRef(obj, NULL);
33883394
if (weakref == NULL) {
33893395
Py_DECREF(obj);
@@ -3394,28 +3400,30 @@ check_weakref_capi(PyObject *self, PyObject *factory)
33943400
assert(PyWeakref_CheckRefExact(weakref));
33953401
assert(Py_REFCNT(obj) == refcnt);
33963402

3397-
// test PyWeakref_GetRef()
3403+
// test PyWeakref_GetRef(), reference is alive
33983404
PyObject *ref1;
33993405
assert(PyWeakref_GetRef(weakref, &ref1) == 0);
34003406
assert(ref1 == obj);
34013407
assert(Py_REFCNT(obj) == (refcnt + 1));
34023408
Py_DECREF(ref1);
34033409

3404-
// test PyWeakref_GetObject()
3410+
// test PyWeakref_GetObject(), reference is alive
34053411
PyObject *ref2 = PyWeakref_GetObject(weakref);
34063412
assert(ref2 == obj);
3407-
assert(Py_REFCNT(obj) == refcnt);
34083413

3409-
// test PyWeakref_GET_OBJECT()
3414+
// test PyWeakref_GET_OBJECT(), reference is alive
34103415
PyObject *ref3 = PyWeakref_GET_OBJECT(weakref);
34113416
assert(ref3 == obj);
3412-
assert(Py_REFCNT(obj) == refcnt);
34133417

3414-
// delete the object
3415-
assert(refcnt == 1);
3418+
// delete the referenced object
3419+
assert(Py_REFCNT(obj) == 1);
34163420
Py_DECREF(obj);
3421+
3422+
// test PyWeakref_GET_OBJECT(), reference is dead
34173423
assert(PyWeakref_GET_OBJECT(weakref) == Py_None);
3418-
PyObject *ref4;
3424+
3425+
// test PyWeakref_GetRef(), reference is dead
3426+
PyObject *ref4 = Py_True; // marker to check that value was set
34193427
assert(PyWeakref_GetRef(weakref, &ref4) == 0);
34203428
assert(ref4 == NULL);
34213429

@@ -3425,12 +3433,15 @@ check_weakref_capi(PyObject *self, PyObject *factory)
34253433
assert(!PyWeakref_CheckRefExact(invalid_weakref));
34263434
assert(!PyWeakref_CheckRefExact(invalid_weakref));
34273435

3436+
// test PyWeakref_GetRef(), invalid type
34283437
assert(!PyErr_Occurred());
3429-
PyObject *ref5 = factory; // marker to check that value was set
3438+
PyObject *ref5 = Py_True; // marker to check that value was set
34303439
assert(PyWeakref_GetRef(invalid_weakref, &ref5) == -1);
34313440
assert(PyErr_ExceptionMatches(PyExc_TypeError));
34323441
PyErr_Clear();
3442+
assert(ref5 == NULL);
34333443

3444+
// test PyWeakref_GetObject(), invalid type
34343445
assert(PyWeakref_GetObject(invalid_weakref) == NULL);
34353446
assert(PyErr_ExceptionMatches(PyExc_SystemError));
34363447
PyErr_Clear();
@@ -3583,7 +3594,7 @@ static PyMethodDef TestMethods[] = {
35833594
{"function_set_kw_defaults", function_set_kw_defaults, METH_VARARGS, NULL},
35843595
{"test_atexit", test_atexit, METH_NOARGS},
35853596
{"check_pyimport_addmodule", check_pyimport_addmodule, METH_VARARGS},
3586-
{"check_weakref_capi", check_weakref_capi, METH_O},
3597+
{"test_weakref_capi", test_weakref_capi, METH_NOARGS},
35873598
{NULL, NULL} /* sentinel */
35883599
};
35893600

0 commit comments

Comments
 (0)