Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-1635741: Port errno module to multiphase initialization #19923

Merged
merged 5 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port :mod:`errno` to multiphase initialization (:pep:`489`).
78 changes: 42 additions & 36 deletions Modules/errnomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,43 +66,15 @@ _inscode(PyObject *d, PyObject *de, const char *name, int code)
Py_XDECREF(v);
}

PyDoc_STRVAR(errno__doc__,
"This module makes available standard errno system symbols.\n\
\n\
The value of each symbol is the corresponding integer value,\n\
e.g., on most systems, errno.ENOENT equals the integer 2.\n\
\n\
The dictionary errno.errorcode maps numeric codes to symbol names,\n\
e.g., errno.errorcode[2] could be the string 'ENOENT'.\n\
\n\
Symbols that are not relevant to the underlying system are not defined.\n\
\n\
To map error codes to error messages, use the function os.strerror(),\n\
e.g. os.strerror(2) could return 'No such file or directory'.");

static struct PyModuleDef errnomodule = {
PyModuleDef_HEAD_INIT,
"errno",
errno__doc__,
-1,
errno_methods,
NULL,
NULL,
NULL,
NULL
};

PyMODINIT_FUNC
PyInit_errno(void)
static int
errno_exec(PyObject *module)
{
PyObject *m, *d, *de;
m = PyModule_Create(&errnomodule);
if (m == NULL)
return NULL;
d = PyModule_GetDict(m);
PyObject *d, *de;
corona10 marked this conversation as resolved.
Show resolved Hide resolved
d = PyModule_GetDict(module);
de = PyDict_New();
if (!d || !de || PyDict_SetItemString(d, "errorcode", de) < 0)
return NULL;
if (!d || !de || PyDict_SetItemString(d, "errorcode", de) < 0) {
return -1;
}

/* Macro so I don't have to edit each and every line below... */
#define inscode(d, ds, de, name, code, comment) _inscode(d, de, name, code)
Expand Down Expand Up @@ -931,5 +903,39 @@ PyInit_errno(void)
#endif

Py_DECREF(de);
return m;
return 0;
}

static PyModuleDef_Slot errno_slots[] = {
{Py_mod_exec, errno_exec},
{0, NULL}
};

PyDoc_STRVAR(errno__doc__,
"This module makes available standard errno system symbols.\n\
\n\
The value of each symbol is the corresponding integer value,\n\
e.g., on most systems, errno.ENOENT equals the integer 2.\n\
\n\
The dictionary errno.errorcode maps numeric codes to symbol names,\n\
e.g., errno.errorcode[2] could be the string 'ENOENT'.\n\
\n\
Symbols that are not relevant to the underlying system are not defined.\n\
\n\
To map error codes to error messages, use the function os.strerror(),\n\
e.g. os.strerror(2) could return 'No such file or directory'.");

static struct PyModuleDef errnomodule = {
PyModuleDef_HEAD_INIT,
.m_name = "errno",
.m_doc = errno__doc__,
.m_size = 0,
.m_methods = errno_methods,
.m_slots = errno_slots,
};

PyMODINIT_FUNC
PyInit_errno(void)
{
return PyModuleDef_Init(&errnomodule);
}