bpo-40950: Port nis module to multiphase initialization (GH-20811)

This commit is contained in:
Dong-hee Na 2020-06-12 11:26:00 +09:00 committed by GitHub
parent 756180b4bf
commit ddef3bdc7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 107 additions and 47 deletions

View File

@ -0,0 +1,2 @@
Add a state to the :mod:`nis` module (:pep:`3121`) and apply
the multiphase initialization. Patch by Dong-hee Na.

View File

@ -44,12 +44,42 @@ PyDoc_STRVAR(maps__doc__,
Returns an array of all available NIS maps within a domain. If domain\n\ Returns an array of all available NIS maps within a domain. If domain\n\
is not specified it defaults to the system default domain.\n"); is not specified it defaults to the system default domain.\n");
static PyObject *NisError; typedef struct {
PyObject *nis_error;
} nis_state;
static inline nis_state*
get_nis_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (nis_state *)state;
}
static int
nis_clear(PyObject *m)
{
Py_CLEAR(get_nis_state(m)->nis_error);
return 0;
}
static int
nis_traverse(PyObject *m, visitproc visit, void *arg)
{
Py_VISIT(get_nis_state(m)->nis_error);
return 0;
}
static void
nis_free(void *m)
{
nis_clear((PyObject *) m);
}
static PyObject * static PyObject *
nis_error (int err) nis_error(nis_state *state, int err)
{ {
PyErr_SetString(NisError, yperr_string(err)); PyErr_SetString(state->nis_error, yperr_string(err));
return NULL; return NULL;
} }
@ -137,21 +167,22 @@ nis_foreach (int instatus, char *inkey, int inkeylen, char *inval,
} }
static PyObject * static PyObject *
nis_get_default_domain (PyObject *self, PyObject *Py_UNUSED(ignored)) nis_get_default_domain(PyObject *module, PyObject *Py_UNUSED(ignored))
{ {
char *domain; char *domain;
int err; int err;
PyObject *res; PyObject *res;
nis_state *state = get_nis_state(module);
if ((err = yp_get_default_domain(&domain)) != 0) if ((err = yp_get_default_domain(&domain)) != 0) {
return nis_error(err); return nis_error(state, err);
}
res = PyUnicode_FromStringAndSize (domain, strlen(domain)); res = PyUnicode_FromStringAndSize (domain, strlen(domain));
return res; return res;
} }
static PyObject * static PyObject *
nis_match (PyObject *self, PyObject *args, PyObject *kwdict) nis_match(PyObject *module, PyObject *args, PyObject *kwdict)
{ {
char *match; char *match;
char *domain = NULL; char *domain = NULL;
@ -165,18 +196,22 @@ nis_match (PyObject *self, PyObject *args, PyObject *kwdict)
if (!PyArg_ParseTupleAndKeywords(args, kwdict, if (!PyArg_ParseTupleAndKeywords(args, kwdict,
"Us|s:match", kwlist, "Us|s:match", kwlist,
&ukey, &map, &domain)) &ukey, &map, &domain)) {
return NULL; return NULL;
if ((bkey = PyUnicode_EncodeFSDefault(ukey)) == NULL) }
if ((bkey = PyUnicode_EncodeFSDefault(ukey)) == NULL) {
return NULL; return NULL;
}
/* check for embedded null bytes */ /* check for embedded null bytes */
if (PyBytes_AsStringAndSize(bkey, &key, &keylen) == -1) { if (PyBytes_AsStringAndSize(bkey, &key, &keylen) == -1) {
Py_DECREF(bkey); Py_DECREF(bkey);
return NULL; return NULL;
} }
nis_state *state = get_nis_state(module);
if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) { if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) {
Py_DECREF(bkey); Py_DECREF(bkey);
return nis_error(err); return nis_error(state, err);
} }
map = nis_mapname (map, &fix); map = nis_mapname (map, &fix);
if (fix) if (fix)
@ -187,15 +222,16 @@ nis_match (PyObject *self, PyObject *args, PyObject *kwdict)
Py_DECREF(bkey); Py_DECREF(bkey);
if (fix) if (fix)
len--; len--;
if (err != 0) if (err != 0) {
return nis_error(err); return nis_error(state, err);
}
res = PyUnicode_DecodeFSDefaultAndSize(match, len); res = PyUnicode_DecodeFSDefaultAndSize(match, len);
free (match); free (match);
return res; return res;
} }
static PyObject * static PyObject *
nis_cat (PyObject *self, PyObject *args, PyObject *kwdict) nis_cat(PyObject *module, PyObject *args, PyObject *kwdict)
{ {
char *domain = NULL; char *domain = NULL;
char *map; char *map;
@ -206,10 +242,13 @@ nis_cat (PyObject *self, PyObject *args, PyObject *kwdict)
static char *kwlist[] = {"map", "domain", NULL}; static char *kwlist[] = {"map", "domain", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat", if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat",
kwlist, &map, &domain)) kwlist, &map, &domain)) {
return NULL; return NULL;
if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) }
return nis_error(err); nis_state *state = get_nis_state(module);
if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) {
return nis_error(state, err);
}
dict = PyDict_New (); dict = PyDict_New ();
if (dict == NULL) if (dict == NULL)
return NULL; return NULL;
@ -222,7 +261,7 @@ nis_cat (PyObject *self, PyObject *args, PyObject *kwdict)
PyEval_RestoreThread(data.state); PyEval_RestoreThread(data.state);
if (err != 0) { if (err != 0) {
Py_DECREF(dict); Py_DECREF(dict);
return nis_error(err); return nis_error(state, err);
} }
return dict; return dict;
} }
@ -352,7 +391,7 @@ nisproc_maplist_2(domainname *argp, CLIENT *clnt)
static static
nismaplist * nismaplist *
nis_maplist (char *dom) nis_maplist(nis_state *state, char *dom)
{ {
nisresp_maplist *list; nisresp_maplist *list;
CLIENT *cl; CLIENT *cl;
@ -364,12 +403,12 @@ nis_maplist (char *dom)
mapi++; mapi++;
} }
if (!server) { if (!server) {
PyErr_SetString(NisError, "No NIS master found for any map"); PyErr_SetString(state->nis_error, "No NIS master found for any map");
return NULL; return NULL;
} }
cl = clnt_create(server, YPPROG, YPVERS, "tcp"); cl = clnt_create(server, YPPROG, YPVERS, "tcp");
if (cl == NULL) { if (cl == NULL) {
PyErr_SetString(NisError, clnt_spcreateerror(server)); PyErr_SetString(state->nis_error, clnt_spcreateerror(server));
goto finally; goto finally;
} }
list = nisproc_maplist_2 (&dom, cl); list = nisproc_maplist_2 (&dom, cl);
@ -388,7 +427,7 @@ nis_maplist (char *dom)
} }
static PyObject * static PyObject *
nis_maps (PyObject *self, PyObject *args, PyObject *kwdict) nis_maps (PyObject *module, PyObject *args, PyObject *kwdict)
{ {
char *domain = NULL; char *domain = NULL;
nismaplist *maps; nismaplist *maps;
@ -397,17 +436,22 @@ nis_maps (PyObject *self, PyObject *args, PyObject *kwdict)
static char *kwlist[] = {"domain", NULL}; static char *kwlist[] = {"domain", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwdict, if (!PyArg_ParseTupleAndKeywords(args, kwdict,
"|s:maps", kwlist, &domain)) "|s:maps", kwlist, &domain)) {
return NULL;
if (!domain && ((err = yp_get_default_domain (&domain)) != 0)) {
nis_error(err);
return NULL; return NULL;
} }
if ((maps = nis_maplist (domain)) == NULL) nis_state *state = get_nis_state(module);
if (!domain && ((err = yp_get_default_domain (&domain)) != 0)) {
nis_error(state, err);
return NULL; return NULL;
if ((list = PyList_New(0)) == NULL) }
if ((maps = nis_maplist(state, domain)) == NULL) {
return NULL; return NULL;
}
if ((list = PyList_New(0)) == NULL) {
return NULL;
}
for (; maps; maps = maps->next) { for (; maps; maps = maps->next) {
PyObject *str = PyUnicode_FromString(maps->map); PyObject *str = PyUnicode_FromString(maps->map);
if (!str || PyList_Append(list, str) < 0) if (!str || PyList_Append(list, str) < 0)
@ -439,31 +483,45 @@ static PyMethodDef nis_methods[] = {
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
static int
nis_exec(PyObject *module)
{
nis_state* state = get_nis_state(module);
state->nis_error = PyErr_NewException("nis.error", NULL, NULL);
if (state->nis_error == NULL) {
return -1;
}
Py_INCREF(state->nis_error);
if (PyModule_AddObject(module, "error", state->nis_error) < 0) {
Py_DECREF(state->nis_error);
return -1;
}
return 0;
}
static PyModuleDef_Slot nis_slots[] = {
{Py_mod_exec, nis_exec},
{0, NULL}
};
PyDoc_STRVAR(nis__doc__, PyDoc_STRVAR(nis__doc__,
"This module contains functions for accessing NIS maps.\n"); "This module contains functions for accessing NIS maps.\n");
static struct PyModuleDef nismodule = { static struct PyModuleDef nismodule = {
PyModuleDef_HEAD_INIT, PyModuleDef_HEAD_INIT,
"nis", .m_name = "nis",
nis__doc__, .m_doc = nis__doc__,
-1, .m_size = sizeof(nis_state),
nis_methods, .m_methods = nis_methods,
NULL, .m_traverse = nis_traverse,
NULL, .m_clear = nis_clear,
NULL, .m_free = nis_free,
NULL .m_slots = nis_slots,
}; };
PyMODINIT_FUNC PyMODINIT_FUNC
PyInit_nis(void) PyInit_nis(void)
{ {
PyObject *m, *d; return PyModuleDef_Init(&nismodule);
m = PyModule_Create(&nismodule);
if (m == NULL)
return NULL;
d = PyModule_GetDict(m);
NisError = PyErr_NewException("nis.error", NULL, NULL);
if (NisError != NULL)
PyDict_SetItemString(d, "error", NisError);
return m;
} }