diff options
Diffstat (limited to 'Modules/nismodule.c')
-rw-r--r-- | Modules/nismodule.c | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/Modules/nismodule.c b/Modules/nismodule.c index 6acab63ddfd..a81ca8ca4df 100644 --- a/Modules/nismodule.c +++ b/Modules/nismodule.c @@ -117,8 +117,8 @@ nis_foreach (int instatus, char *inkey, int inkeylen, char *inval, if (invallen > 0 && inval[invallen-1] == '\0') invallen--; } - key = PyString_FromStringAndSize(inkey, inkeylen); - val = PyString_FromStringAndSize(inval, invallen); + key = PyUnicode_DecodeFSDefaultAndSize(inkey, inkeylen); + val = PyUnicode_DecodeFSDefaultAndSize(inval, invallen); if (key == NULL || val == NULL) { /* XXX error -- don't know how to handle */ PyErr_Clear(); @@ -150,7 +150,7 @@ nis_get_default_domain (PyObject *self) if ((err = yp_get_default_domain(&domain)) != 0) return nis_error(err); - res = PyString_FromStringAndSize (domain, strlen(domain)); + res = PyUnicode_FromStringAndSize (domain, strlen(domain)); return res; } @@ -159,30 +159,40 @@ nis_match (PyObject *self, PyObject *args, PyObject *kwdict) { char *match; char *domain = NULL; - int keylen, len; + Py_ssize_t keylen; + int len; char *key, *map; int err; - PyObject *res; + PyObject *ukey, *bkey, *res; int fix; static char *kwlist[] = {"key", "map", "domain", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwdict, - "t#s|s:match", kwlist, - &key, &keylen, &map, &domain)) + "Us|s:match", kwlist, + &ukey, &map, &domain)) return NULL; - if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) + if ((bkey = PyUnicode_EncodeFSDefault(ukey)) == NULL) + return NULL; + if (PyBytes_AsStringAndSize(bkey, &key, &keylen) == -1) { + Py_DECREF(bkey); + return NULL; + } + if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) { + Py_DECREF(bkey); return nis_error(err); + } map = nis_mapname (map, &fix); if (fix) keylen++; Py_BEGIN_ALLOW_THREADS err = yp_match (domain, map, key, keylen, &match, &len); Py_END_ALLOW_THREADS + Py_DECREF(bkey); if (fix) len--; if (err != 0) return nis_error(err); - res = PyString_FromStringAndSize (match, len); + res = PyUnicode_DecodeFSDefaultAndSize(match, len); free (match); return res; } @@ -402,7 +412,7 @@ nis_maps (PyObject *self, PyObject *args, PyObject *kwdict) if ((list = PyList_New(0)) == NULL) return NULL; for (maps = maps; maps; maps = maps->next) { - PyObject *str = PyString_FromString(maps->map); + PyObject *str = PyUnicode_FromString(maps->map); if (!str || PyList_Append(list, str) < 0) { Py_DECREF(list); @@ -434,15 +444,28 @@ static PyMethodDef nis_methods[] = { PyDoc_STRVAR(nis__doc__, "This module contains functions for accessing NIS maps.\n"); -void -initnis (void) +static struct PyModuleDef nismodule = { + PyModuleDef_HEAD_INIT, + "nis", + nis__doc__, + -1, + nis_methods, + NULL, + NULL, + NULL, + NULL +}; + +PyObject* +PyInit_nis (void) { PyObject *m, *d; - m = Py_InitModule3("nis", nis_methods, nis__doc__); + m = PyModule_Create(&nismodule); if (m == NULL) - return; + return NULL; d = PyModule_GetDict(m); NisError = PyErr_NewException("nis.error", NULL, NULL); if (NisError != NULL) PyDict_SetItemString(d, "error", NisError); + return m; } |