Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,7 @@ Porting to Python 3.10

Removed
-------

* Remove the :c:func:`_Py_NewReference` and :c:func:`_Py_ForgetReference`
functions from the public C API: move them to the internal C API.
(Contributed by Victor Stinner in :issue:`40989`.)
7 changes: 0 additions & 7 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
# error "this header file must not be included directly"
#endif

PyAPI_FUNC(void) _Py_NewReference(PyObject *op);

#ifdef Py_TRACE_REFS
/* Py_TRACE_REFS is such major surgery that we call external routines. */
PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
#endif

#ifdef Py_REF_DEBUG
PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
#endif
Expand Down
11 changes: 8 additions & 3 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ extern "C" {
PyAPI_FUNC(int) _PyType_CheckConsistency(PyTypeObject *type);
PyAPI_FUNC(int) _PyDict_CheckConsistency(PyObject *mp, int check_content);

/* Update the Python traceback of an object. This function must be called
when a memory block is reused from a free list.
PyAPI_FUNC(void) _Py_NewReference(PyObject *op);

Internal function called by _Py_NewReference(). */
/* Update the Python traceback of an object. This function must be called
when a memory block is reused from a free list. */
extern int _PyTraceMalloc_NewReference(PyObject *op);

#ifdef Py_TRACE_REFS
/* Py_TRACE_REFS is such major surgery that we call external routines. */
PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
#endif

// Fast inlined version of PyType_HasFeature()
static inline int
_PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_finalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import weakref

try:
from _testcapi import with_tp_del
from _testinternalcapi import with_tp_del
except ImportError:
def with_tp_del(cls):
class C(object):
def __new__(cls, *args, **kwargs):
raise TypeError('requires _testcapi.with_tp_del')
raise TypeError('requires _testinternalcapi.with_tp_del')
return C

from test import support
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
import weakref

try:
from _testcapi import with_tp_del
from _testinternalcapi import with_tp_del
except ImportError:
def with_tp_del(cls):
class C(object):
def __new__(cls, *args, **kwargs):
raise TypeError('requires _testcapi.with_tp_del')
raise TypeError('requires _testinternalcapi.with_tp_del')
return C

try:
Expand Down Expand Up @@ -664,8 +664,8 @@ def test_garbage_at_shutdown(self):
import subprocess
code = """if 1:
import gc
import _testcapi
@_testcapi.with_tp_del
import _testinternalcapi
@_testinternalcapi.with_tp_del
class X:
def __init__(self, name):
self.name = name
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,11 +1177,11 @@ def test_other_bug(self):
@support.cpython_only
def test_findleaks(self):
code = textwrap.dedent(r"""
import _testcapi
import _testinternalcapi
import gc
import unittest

@_testcapi.with_tp_del
@_testinternalcapi.with_tp_del
class Garbage:
def __tp_del__(self):
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Remove the :c:func:`_Py_NewReference` and :c:func:`_Py_ForgetReference`
functions from the public C API:(move them to the internal C API.
1 change: 1 addition & 0 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_object.h" // _Py_NewReference()
#include "pycore_pyerrors.h" // _PyErr_ClearExcState()
#include <stddef.h> // offsetof()

Expand Down
74 changes: 0 additions & 74 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3544,79 +3544,6 @@ test_pytime_object_to_timespec(PyObject *self, PyObject *args)
return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
}

static void
slot_tp_del(PyObject *self)
{
_Py_IDENTIFIER(__tp_del__);
PyObject *del, *res;
PyObject *error_type, *error_value, *error_traceback;

/* Temporarily resurrect the object. */
assert(Py_REFCNT(self) == 0);
Py_SET_REFCNT(self, 1);

/* Save the current exception, if any. */
PyErr_Fetch(&error_type, &error_value, &error_traceback);

/* Execute __del__ method, if any. */
del = _PyObject_LookupSpecial(self, &PyId___tp_del__);
if (del != NULL) {
res = _PyObject_CallNoArg(del);
if (res == NULL)
PyErr_WriteUnraisable(del);
else
Py_DECREF(res);
Py_DECREF(del);
}

/* Restore the saved exception. */
PyErr_Restore(error_type, error_value, error_traceback);

/* Undo the temporary resurrection; can't use DECREF here, it would
* cause a recursive call.
*/
assert(Py_REFCNT(self) > 0);
Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
if (Py_REFCNT(self) == 0) {
/* this is the normal path out */
return;
}

/* __del__ resurrected it! Make it look like the original Py_DECREF
* never happened.
*/
{
Py_ssize_t refcnt = Py_REFCNT(self);
_Py_NewReference(self);
Py_SET_REFCNT(self, refcnt);
}
assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self));
/* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
_Py_RefTotal, so we need to undo that. */
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
#endif
}

static PyObject *
with_tp_del(PyObject *self, PyObject *args)
{
PyObject *obj;
PyTypeObject *tp;

if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj))
return NULL;
tp = (PyTypeObject *) obj;
if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
PyErr_Format(PyExc_TypeError,
"heap type expected, got %R", obj);
return NULL;
}
tp->tp_del = slot_tp_del;
Py_INCREF(obj);
return obj;
}

static PyMethodDef ml;

static PyObject *
Expand Down Expand Up @@ -5418,7 +5345,6 @@ static PyMethodDef TestMethods[] = {
{"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS},
{"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS},
{"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS},
{"with_tp_del", with_tp_del, METH_VARARGS},
{"create_cfunction", create_cfunction, METH_NOARGS},
{"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS},
{"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS},
Expand Down
77 changes: 77 additions & 0 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "pycore_bitutils.h" // _Py_bswap32()
#include "pycore_initconfig.h" // _Py_GetConfigsAsDict()
#include "pycore_hashtable.h" // _Py_hashtable_new()
#include "pycore_object.h" // _Py_NewReference()
#include "pycore_gc.h" // PyGC_Head


Expand Down Expand Up @@ -231,13 +232,89 @@ test_hashtable(PyObject *self, PyObject *Py_UNUSED(args))
}


static void
slot_tp_del(PyObject *self)
{
_Py_IDENTIFIER(__tp_del__);
PyObject *del, *res;
PyObject *error_type, *error_value, *error_traceback;

/* Temporarily resurrect the object. */
assert(Py_REFCNT(self) == 0);
Py_SET_REFCNT(self, 1);

/* Save the current exception, if any. */
PyErr_Fetch(&error_type, &error_value, &error_traceback);

/* Execute __del__ method, if any. */
del = _PyObject_LookupSpecial(self, &PyId___tp_del__);
if (del != NULL) {
res = _PyObject_CallNoArg(del);
if (res == NULL)
PyErr_WriteUnraisable(del);
else
Py_DECREF(res);
Py_DECREF(del);
}

/* Restore the saved exception. */
PyErr_Restore(error_type, error_value, error_traceback);

/* Undo the temporary resurrection; can't use DECREF here, it would
* cause a recursive call.
*/
assert(Py_REFCNT(self) > 0);
Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
if (Py_REFCNT(self) == 0) {
/* this is the normal path out */
return;
}

/* __del__ resurrected it! Make it look like the original Py_DECREF
* never happened.
*/
{
Py_ssize_t refcnt = Py_REFCNT(self);
_Py_NewReference(self);
Py_SET_REFCNT(self, refcnt);
}
assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self));
/* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
_Py_RefTotal, so we need to undo that. */
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
#endif
}


static PyObject *
with_tp_del(PyObject *self, PyObject *args)
{
PyObject *obj;
PyTypeObject *tp;

if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj))
return NULL;
tp = (PyTypeObject *) obj;
if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
PyErr_Format(PyExc_TypeError,
"heap type expected, got %R", obj);
return NULL;
}
tp->tp_del = slot_tp_del;
Py_INCREF(obj);
return obj;
}


static PyMethodDef TestMethods[] = {
{"get_configs", get_configs, METH_NOARGS},
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
{"test_bswap", test_bswap, METH_NOARGS},
{"test_popcount", test_popcount, METH_NOARGS},
{"test_bit_length", test_bit_length, METH_NOARGS},
{"test_hashtable", test_hashtable, METH_NOARGS},
{"with_tp_del", with_tp_del, METH_VARARGS},
{NULL, NULL} /* sentinel */
};

Expand Down