From 4196b0a1fe0d21254e7f67e27aad5ca5f35af2fe Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 6 Nov 2023 22:48:49 +0000 Subject: [PATCH] revert deprecation, treat as bugfix --- Doc/reference/datamodel.rst | 14 ++++------ Doc/whatsnew/3.13.rst | 3 ++- Lib/test/test_frame.py | 23 ++++++---------- ...3-11-06-16-44-09.gh-issue-79932.2qv7uD.rst | 2 +- Objects/frameobject.c | 26 +++++-------------- 5 files changed, 23 insertions(+), 45 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index fea5e6ccd48e49..67c5c2e38d0df9 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1206,7 +1206,7 @@ by writing to f_lineno. Frame objects support one method: -.. method:: frame.clear([raise_if_suspended]) +.. method:: frame.clear() This method clears all references to local variables held by the frame. Also, if the frame belonged to a generator, the generator @@ -1214,18 +1214,14 @@ Frame objects support one method: objects (for example when catching an exception and storing its traceback for later use). - :exc:`RuntimeError` is raised if the frame is currently executing. - - Clearing a suspended frame is deprecated. - The optional argument *raise_if_suspended* can be passed ``True`` to - make this function raise a :exc:`RuntimeError` instead of issuing a - deprecation warning if the frame is suspended. + :exc:`RuntimeError` is raised if the frame is currently executing + or suspended. .. versionadded:: 3.4 .. versionchanged:: 3.13 - Clearing a suspended frame is deprecated. Added the *raise_if_suspended* - argument. + Attempting to clear a suspended frame raises :exc:`RuntimeError`. + .. _traceback-objects: diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index e137e5479fb6aa..0d1de78d07abbb 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -397,7 +397,8 @@ Deprecated and methods that consider plural forms even if the translation was not found. (Contributed by Serhiy Storchaka in :gh:`88434`.) -* Calling :meth:`frame.clear` on a suspended frame is deprecated. +* Calling :meth:`frame.clear` on a suspended frame raises :exc:`RuntimeError`, + as has always been the case for an executing frame. (Contributed by Irit Katriel in :gh:`79932`.) diff --git a/Lib/test/test_frame.py b/Lib/test/test_frame.py index e6d4067727e0ac..32ea619ec018c7 100644 --- a/Lib/test/test_frame.py +++ b/Lib/test/test_frame.py @@ -6,7 +6,6 @@ import threading import types import unittest -import warnings import weakref try: import _testcapi @@ -84,17 +83,13 @@ def g(): # Raise exception when attempting to clear a suspended frame with self.assertRaisesRegex(RuntimeError, r'suspended frame'): - gen.gi_frame.clear(True) + gen.gi_frame.clear() self.assertFalse(endly) - # Clearing the frame closes the generator - try: - with self.assertWarnsRegex(DeprecationWarning, r'suspended frame'): - gen.gi_frame.clear() - except DeprecationWarning: - # Suppress the warning when running with -We - pass - self.assertTrue(endly) + # Cannot clear a suspended frame + with self.assertRaisesRegex(RuntimeError, r'suspended frame'): + gen.gi_frame.clear() + self.assertFalse(endly) def test_clear_executing(self): # Attempting to clear an executing frame is forbidden. @@ -126,12 +121,10 @@ def g(): gen = g() f = next(gen) self.assertFalse(endly) - # Clearing the frame closes the generator - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', category=DeprecationWarning) + # Cannot clear a suspended frame + with self.assertRaisesRegex(RuntimeError, 'suspended frame'): f.clear() - - self.assertTrue(endly) + self.assertFalse(endly) def test_lineno_with_tracing(self): def record_line(): diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-06-16-44-09.gh-issue-79932.2qv7uD.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-06-16-44-09.gh-issue-79932.2qv7uD.rst index 67071ee60b8a11..543dbe4413027a 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2023-11-06-16-44-09.gh-issue-79932.2qv7uD.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2023-11-06-16-44-09.gh-issue-79932.2qv7uD.rst @@ -1 +1 @@ -Deprecate clearing a suspended frame. +Raise exception if :meth:`frame.clear` is called on a suspended frame. diff --git a/Objects/frameobject.c b/Objects/frameobject.c index bd265ec2e07f23..fc82ba0f0f566c 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -933,31 +933,15 @@ frame_tp_clear(PyFrameObject *f) } static PyObject * -frame_clear(PyFrameObject *f, PyObject *args, PyObject *kwds) +frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored)) { - bool raise_if_suspended = false; - PyObject *v = NULL; - if (!PyArg_UnpackTuple(args, "clear", 0, 1, &v)) { - return NULL; - } - if (v != NULL && PyObject_IsTrue(v)) { - raise_if_suspended = true; - } - if (f->f_frame->owner == FRAME_OWNED_BY_GENERATOR) { PyGenObject *gen = _PyFrame_GetGenerator(f->f_frame); if (gen->gi_frame_state == FRAME_EXECUTING) { goto running; } if (FRAME_STATE_SUSPENDED(gen->gi_frame_state)) { - if (raise_if_suspended) { - PyErr_SetString(PyExc_RuntimeError, "cannot clear a suspended frame"); - return NULL; - } - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "clearing a suspended frame is deprecated", 1) < 0) { - return NULL; - } + goto suspended; } _PyGen_Finalize((PyObject *)gen); } @@ -973,6 +957,10 @@ frame_clear(PyFrameObject *f, PyObject *args, PyObject *kwds) PyErr_SetString(PyExc_RuntimeError, "cannot clear an executing frame"); return NULL; +suspended: + PyErr_SetString(PyExc_RuntimeError, + "cannot clear a suspended frame"); + return NULL; } PyDoc_STRVAR(clear__doc__, @@ -1002,7 +990,7 @@ frame_repr(PyFrameObject *f) } static PyMethodDef frame_methods[] = { - {"clear", (PyCFunction)frame_clear, METH_VARARGS, + {"clear", (PyCFunction)frame_clear, METH_NOARGS, clear__doc__}, {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS, sizeof__doc__},