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
3 changes: 3 additions & 0 deletions Doc/library/errno.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ all-inclusive.
underlying system. For instance, ``errno.errorcode[errno.EPERM]`` maps to
``'EPERM'``.

.. versionchanged:: next
The dictionary type is now a :class:`frozendict`.

To translate a numeric error code to an error message, use :func:`os.strerror`.

Of the following list, symbols that are not used on the current platform are not
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_errno.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def test_using_errorcode(self):
for value in errno.errorcode.values():
self.assertHasAttr(errno, value)

def test_readonly_errorcode(self):
with self.assertRaises(TypeError):
errno.errorcode[1] = 'hack'


class ErrorcodeTests(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:data:`errno.errorcode` is now read-only: use :class:`frozendict` type
instead of :class:`dict`. Patch by Victor Stinner.
20 changes: 10 additions & 10 deletions Modules/errnomodule.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/* Errno module */

// Need limited C API version 3.13 for Py_mod_gil
#include "pyconfig.h" // Py_GIL_DISABLED
#ifndef Py_GIL_DISABLED
# define Py_LIMITED_API 0x030d0000
#endif

#include "Python.h"
#include <errno.h> // EPIPE

Expand Down Expand Up @@ -96,10 +90,6 @@ errno_exec(PyObject *module)
if (error_dict == NULL) {
return -1;
}
if (PyDict_SetItemString(module_dict, "errorcode", error_dict) < 0) {
Py_DECREF(error_dict);
return -1;
}

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

PyObject *frozendict = PyFrozenDict_New(error_dict);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we have to add new C API for frozendict with builder pattern.
This is inefficient for many people.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, frozendict is not widely used in C code yet. IMO it's too early to design an optimized API to build a new frozendict object. PyFrozenDict_New() should be good for now.

Py_DECREF(error_dict);
if (frozendict == NULL) {
return -1;
}
if (PyDict_SetItemString(module_dict, "errorcode", frozendict) < 0) {
Py_DECREF(frozendict);
return -1;
}
Py_DECREF(frozendict);

return 0;
}

Expand Down
Loading