-
-
Notifications
You must be signed in to change notification settings - Fork 34
Add PyUnstable_TryIncref and PyUnstable_EnableTryIncRef. #159
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2448,6 +2448,46 @@ test_tuple(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | |
| return test_tuple_fromarray(); | ||
| } | ||
|
|
||
| // Test adapted from CPython's _testcapi/object.c | ||
| static int MyObject_dealloc_called = 0; | ||
|
|
||
| static void | ||
| MyObject_dealloc(PyObject *op) | ||
|
||
| { | ||
| // PyUnstable_TryIncRef should return 0 if object is being deallocated | ||
| assert(Py_REFCNT(op) == 0); | ||
| assert(!PyUnstable_TryIncRef(op)); | ||
| assert(Py_REFCNT(op) == 0); | ||
|
|
||
| MyObject_dealloc_called++; | ||
| Py_TYPE(op)->tp_free(op); | ||
| } | ||
|
|
||
| static PyTypeObject MyType; | ||
|
|
||
| static PyObject* | ||
| test_try_incref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | ||
| { | ||
| MyObject_dealloc_called = 0; | ||
|
|
||
| PyObject *obj = PyObject_New(PyObject, &MyType); | ||
| if (obj == _Py_NULL) { | ||
| return _Py_NULL; | ||
| } | ||
|
|
||
| PyUnstable_EnableTryIncRef(obj); | ||
|
|
||
| Py_ssize_t refcount = Py_REFCNT(obj); | ||
| assert(PyUnstable_TryIncRef(obj)); | ||
| assert(Py_REFCNT(obj) == refcount + 1); | ||
|
|
||
| Py_DECREF(obj); | ||
| Py_DECREF(obj); | ||
|
|
||
| assert(MyObject_dealloc_called == 1); | ||
| Py_RETURN_NONE; | ||
| } | ||
|
|
||
|
|
||
| static struct PyMethodDef methods[] = { | ||
| {"test_object", test_object, METH_NOARGS, _Py_NULL}, | ||
|
|
@@ -2504,6 +2544,7 @@ static struct PyMethodDef methods[] = { | |
| {"test_uniquely_referenced", test_uniquely_referenced, METH_NOARGS, _Py_NULL}, | ||
| {"test_byteswriter", test_byteswriter, METH_NOARGS, _Py_NULL}, | ||
| {"test_tuple", test_tuple, METH_NOARGS, _Py_NULL}, | ||
| {"test_try_incref", test_try_incref, METH_NOARGS, _Py_NULL}, | ||
| {_Py_NULL, _Py_NULL, 0, _Py_NULL} | ||
| }; | ||
|
|
||
|
|
@@ -2532,6 +2573,13 @@ module_exec(PyObject *module) | |
| return -1; | ||
| } | ||
| #endif | ||
| MyType.tp_name = "MyType"; | ||
|
||
| MyType.tp_basicsize = sizeof(PyObject); | ||
| MyType.tp_dealloc = MyObject_dealloc; | ||
| MyType.tp_free = PyObject_Del; | ||
| if (PyType_Ready(&MyType) < 0) { | ||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.