Skip to content

Commit

Permalink
Use RuntimeError and some internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian committed Oct 17, 2024
1 parent 124aaa0 commit c8a8eb4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions Lib/test/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,22 +399,22 @@ def test_delete(self):
d = sys._getframe().f_locals

# This needs to be tested before f_extra_locals is created
with self.assertRaises(KeyError):
with self.assertRaisesRegex(KeyError, 'non_exist'):
del d['non_exist']

with self.assertRaises(KeyError):
d.pop('non_exist')

with self.assertRaises(KeyError):
with self.assertRaisesRegex(RuntimeError, 'local variables'):
del d['x']

with self.assertRaises(AttributeError):
d.clear()

with self.assertRaises(KeyError):
with self.assertRaises(RuntimeError):
d.pop('x')

with self.assertRaises(KeyError):
with self.assertRaises(RuntimeError):
d.pop('x', None)

# 'm', 'n' is stored in f_extra_locals
Expand Down
16 changes: 8 additions & 8 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pycore_code.h" // CO_FAST_LOCAL, etc.
#include "pycore_function.h" // _PyFunction_FromConstructor()
#include "pycore_moduleobject.h" // _PyModule_GetDict()
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_opcode_metadata.h" // _PyOpcode_Deopt, _PyOpcode_Caches

Expand Down Expand Up @@ -164,7 +165,7 @@ framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value)
}
if (i >= 0) {
if (value == NULL) {
PyErr_SetString(PyExc_KeyError, "cannot remove local variables from FrameLocalsProxy");
PyErr_SetString(PyExc_RuntimeError, "cannot remove local variables from FrameLocalsProxy");
return -1;
}

Expand Down Expand Up @@ -203,7 +204,7 @@ framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value)

if (extra == NULL) {
if (value == NULL) {
PyErr_Format(PyExc_KeyError, "'%R'", key);
_PyErr_SetKeyError(key);
return -1;
}
extra = PyDict_New();
Expand Down Expand Up @@ -687,8 +688,7 @@ framelocalsproxy_setdefault(PyObject* self, PyObject *const *args, Py_ssize_t na
static PyObject*
framelocalsproxy_pop(PyObject* self, PyObject *const *args, Py_ssize_t nargs)
{
if (nargs < 1 || nargs > 2) {
PyErr_SetString(PyExc_TypeError, "pop expected 1 or 2 arguments");
if (!_PyArg_CheckPositional("pop", nargs, 1, 2)) {
return NULL;
}

Expand All @@ -707,17 +707,17 @@ framelocalsproxy_pop(PyObject* self, PyObject *const *args, Py_ssize_t nargs)
}

if (i >= 0) {
PyErr_SetString(PyExc_KeyError, "cannot remove local variables from FrameLocalsProxy");
PyErr_SetString(PyExc_RuntimeError, "cannot remove local variables from FrameLocalsProxy");
return NULL;
}

PyObject* result = NULL;
PyObject *result = NULL;

if (frame->f_extra_locals == NULL) {
if (default_value != NULL) {
return Py_XNewRef(default_value);
} else {
PyErr_Format(PyExc_KeyError, "'%R'", key);
_PyErr_SetKeyError(key);
return NULL;
}
}
Expand All @@ -730,7 +730,7 @@ framelocalsproxy_pop(PyObject* self, PyObject *const *args, Py_ssize_t nargs)
if (default_value != NULL) {
return Py_XNewRef(default_value);
} else {
PyErr_Format(PyExc_KeyError, "'%R'", key);
_PyErr_SetKeyError(key);
return NULL;
}
}
Expand Down

0 comments on commit c8a8eb4

Please sign in to comment.