gh-112529: Simplify PyObject_GC_IsTracked and PyObject_GC_IsFinalized (#114732)

This commit is contained in:
Sam Gross 2024-02-28 15:37:59 -05:00 committed by GitHub
parent c43b26d02e
commit df5212df6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 28 deletions

View File

@ -469,6 +469,25 @@ PyDoc_STRVAR(gc_is_tracked__doc__,
#define GC_IS_TRACKED_METHODDEF \ #define GC_IS_TRACKED_METHODDEF \
{"is_tracked", (PyCFunction)gc_is_tracked, METH_O, gc_is_tracked__doc__}, {"is_tracked", (PyCFunction)gc_is_tracked, METH_O, gc_is_tracked__doc__},
static int
gc_is_tracked_impl(PyObject *module, PyObject *obj);
static PyObject *
gc_is_tracked(PyObject *module, PyObject *obj)
{
PyObject *return_value = NULL;
int _return_value;
_return_value = gc_is_tracked_impl(module, obj);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
return_value = PyBool_FromLong((long)_return_value);
exit:
return return_value;
}
PyDoc_STRVAR(gc_is_finalized__doc__, PyDoc_STRVAR(gc_is_finalized__doc__,
"is_finalized($module, obj, /)\n" "is_finalized($module, obj, /)\n"
"--\n" "--\n"
@ -478,6 +497,25 @@ PyDoc_STRVAR(gc_is_finalized__doc__,
#define GC_IS_FINALIZED_METHODDEF \ #define GC_IS_FINALIZED_METHODDEF \
{"is_finalized", (PyCFunction)gc_is_finalized, METH_O, gc_is_finalized__doc__}, {"is_finalized", (PyCFunction)gc_is_finalized, METH_O, gc_is_finalized__doc__},
static int
gc_is_finalized_impl(PyObject *module, PyObject *obj);
static PyObject *
gc_is_finalized(PyObject *module, PyObject *obj)
{
PyObject *return_value = NULL;
int _return_value;
_return_value = gc_is_finalized_impl(module, obj);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
return_value = PyBool_FromLong((long)_return_value);
exit:
return return_value;
}
PyDoc_STRVAR(gc_freeze__doc__, PyDoc_STRVAR(gc_freeze__doc__,
"freeze($module, /)\n" "freeze($module, /)\n"
"--\n" "--\n"
@ -547,4 +585,4 @@ gc_get_freeze_count(PyObject *module, PyObject *Py_UNUSED(ignored))
exit: exit:
return return_value; return return_value;
} }
/*[clinic end generated code: output=258f92524c1141fc input=a9049054013a1b77]*/ /*[clinic end generated code: output=0a7e91917adcb937 input=a9049054013a1b77]*/

View File

@ -383,7 +383,7 @@ error:
/*[clinic input] /*[clinic input]
gc.is_tracked gc.is_tracked -> bool
obj: object obj: object
/ /
@ -393,21 +393,15 @@ Returns true if the object is tracked by the garbage collector.
Simple atomic objects will return false. Simple atomic objects will return false.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static int
gc_is_tracked(PyObject *module, PyObject *obj) gc_is_tracked_impl(PyObject *module, PyObject *obj)
/*[clinic end generated code: output=14f0103423b28e31 input=d83057f170ea2723]*/ /*[clinic end generated code: output=91c8d086b7f47a33 input=423b98ec680c3126]*/
{ {
PyObject *result; return PyObject_GC_IsTracked(obj);
if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj))
result = Py_True;
else
result = Py_False;
return Py_NewRef(result);
} }
/*[clinic input] /*[clinic input]
gc.is_finalized gc.is_finalized -> bool
obj: object obj: object
/ /
@ -415,14 +409,11 @@ gc.is_finalized
Returns true if the object has been already finalized by the GC. Returns true if the object has been already finalized by the GC.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static int
gc_is_finalized(PyObject *module, PyObject *obj) gc_is_finalized_impl(PyObject *module, PyObject *obj)
/*[clinic end generated code: output=e1516ac119a918ed input=201d0c58f69ae390]*/ /*[clinic end generated code: output=401ff5d6fc660429 input=ca4d111c8f8c4e3a]*/
{ {
if (_PyObject_IS_GC(obj) && _PyGC_FINALIZED(obj)) { return PyObject_GC_IsFinalized(obj);
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
} }
/*[clinic input] /*[clinic input]

View File

@ -1693,19 +1693,13 @@ PyObject_GC_Del(void *op)
int int
PyObject_GC_IsTracked(PyObject* obj) PyObject_GC_IsTracked(PyObject* obj)
{ {
if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)) { return _PyObject_GC_IS_TRACKED(obj);
return 1;
}
return 0;
} }
int int
PyObject_GC_IsFinalized(PyObject *obj) PyObject_GC_IsFinalized(PyObject *obj)
{ {
if (_PyObject_IS_GC(obj) && _PyGC_FINALIZED(obj)) { return _PyGC_FINALIZED(obj);
return 1;
}
return 0;
} }
struct custom_visitor_args { struct custom_visitor_args {