Skip to content

Commit f0cf865

Browse files
author
Peter Bierma
committed
Implement PyUnstable_Object_SetDeferredRefcount
1 parent 782217f commit f0cf865

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Include/cpython/object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,5 @@ typedef enum {
526526
typedef int (*PyRefTracer)(PyObject *, PyRefTracerEvent event, void *);
527527
PyAPI_FUNC(int) PyRefTracer_SetTracer(PyRefTracer tracer, void *data);
528528
PyAPI_FUNC(PyRefTracer) PyRefTracer_GetTracer(void**);
529+
530+
PyAPI_FUNC(int) PyUnstable_Object_SetDeferredRefcount(PyObject *);

Objects/object.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,6 +2482,37 @@ _PyObject_SetDeferredRefcount(PyObject *op)
24822482
#endif
24832483
}
24842484

2485+
int
2486+
PyUnstable_Object_SetDeferredRefcount(PyObject *op)
2487+
{
2488+
#ifdef Py_GIL_DISABLED
2489+
if (!PyType_IS_GC(Py_TYPE(op)))
2490+
{
2491+
PyErr_SetString(PyExc_TypeError,
2492+
"object is not tracked by the garbage collector");
2493+
return -1;
2494+
}
2495+
2496+
if (!_Py_IsOwnedByCurrentThread(op))
2497+
{
2498+
PyErr_SetString(PyExc_ValueError,
2499+
"object is not owned by this thread");
2500+
return -1;
2501+
}
2502+
2503+
if (op->ob_ref_shared != 0)
2504+
{
2505+
PyErr_SetString(PyExc_ValueError,
2506+
"object is already in use by another thread");
2507+
return -1;
2508+
}
2509+
2510+
_PyObject_SET_GC_BITS(op, _PyGC_BITS_DEFERRED);
2511+
op->ob_ref_shared = _Py_REF_SHARED(_Py_REF_DEFERRED, 0);
2512+
#endif
2513+
return 0;
2514+
}
2515+
24852516
void
24862517
_Py_ResurrectReference(PyObject *op)
24872518
{

0 commit comments

Comments
 (0)