Skip to content

Commit a4834a8

Browse files
committed
gh-141510: Use frozendict for errno.errorcode
1 parent 6ef2578 commit a4834a8

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

Doc/library/errno.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ all-inclusive.
1818
underlying system. For instance, ``errno.errorcode[errno.EPERM]`` maps to
1919
``'EPERM'``.
2020

21+
.. versionchanged:: next
22+
The dictionary type is now a :class:`frozendict`.
23+
2124
To translate a numeric error code to an error message, use :func:`os.strerror`.
2225

2326
Of the following list, symbols that are not used on the current platform are not

Lib/test/test_errno.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def test_using_errorcode(self):
1919
for value in errno.errorcode.values():
2020
self.assertHasAttr(errno, value)
2121

22+
def test_readonly_errorcode(self):
23+
with self.assertRaises(TypeError):
24+
errno.errorcode[1] = 'hack'
25+
2226

2327
class ErrorcodeTests(unittest.TestCase):
2428

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:data:`errno.errorcode` is now read-only: use :class:`frozendict` type
2+
instead of :class:`dict`. Patch by Victor Stinner.

Modules/errnomodule.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
/* Errno module */
22

3-
// Need limited C API version 3.13 for Py_mod_gil
4-
#include "pyconfig.h" // Py_GIL_DISABLED
5-
#ifndef Py_GIL_DISABLED
6-
# define Py_LIMITED_API 0x030d0000
7-
#endif
8-
93
#include "Python.h"
104
#include <errno.h> // EPIPE
115

@@ -96,10 +90,6 @@ errno_exec(PyObject *module)
9690
if (error_dict == NULL) {
9791
return -1;
9892
}
99-
if (PyDict_SetItemString(module_dict, "errorcode", error_dict) < 0) {
100-
Py_DECREF(error_dict);
101-
return -1;
102-
}
10393

10494
/* Macro so I don't have to edit each and every line below... */
10595
#define add_errcode(name, code, comment) \
@@ -947,7 +937,17 @@ errno_exec(PyObject *module)
947937
add_errcode("ENOTCAPABLE", ENOTCAPABLE, "Capabilities insufficient");
948938
#endif
949939

940+
PyObject *frozendict = PyFrozenDict_New(error_dict);
950941
Py_DECREF(error_dict);
942+
if (frozendict == NULL) {
943+
return -1;
944+
}
945+
if (PyDict_SetItemString(module_dict, "errorcode", frozendict) < 0) {
946+
Py_DECREF(frozendict);
947+
return -1;
948+
}
949+
Py_DECREF(frozendict);
950+
951951
return 0;
952952
}
953953

0 commit comments

Comments
 (0)