Mercurial > hg
changeset 7098:8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
---|---|
date | Wed, 15 Oct 2008 14:06:46 +0200 |
parents | 6dab29f6df37 |
children | 6f750e76fb46 |
files | mercurial/osutil.c |
diffstat | 1 files changed, 35 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/osutil.c Wed Oct 15 01:14:29 2008 +0200 +++ b/mercurial/osutil.c Wed Oct 15 14:06:46 2008 +0200 @@ -172,29 +172,19 @@ kind, py_st); } -static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) +static PyObject *_listdir(char *path, int plen, int wantstat, char *skip) { PyObject *rval = NULL; /* initialize - return value */ - PyObject *statobj = NULL; /* initialize - optional arg */ PyObject *list; HANDLE fh; WIN32_FIND_DATAA fd; - char *path, *pattern, *skip = NULL; - int plen, wantstat; - - static char *kwlist[] = {"path", "stat", "skip", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|Os:listdir", - kwlist, &path, &plen, &statobj, &skip)) - goto error_parse; - - wantstat = statobj && PyObject_IsTrue(statobj); + char *pattern; /* build the path + \* pattern string */ pattern = malloc(plen+3); /* path + \* + \0 */ if (!pattern) { PyErr_NoMemory(); - goto error_parse; + goto error_nomem; } strcpy(pattern, path); @@ -254,7 +244,7 @@ FindClose(fh); error_file: free(pattern); -error_parse: +error_nomem: return rval; } @@ -276,33 +266,27 @@ return -1; } -static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) +static PyObject *_listdir(char *path, int pathlen, int keepstat, char *skip) { - static char *kwlist[] = { "path", "stat", "skip", NULL }; - PyObject *statflag = NULL, *list, *elem, *stat, *ret = NULL; - char fullpath[PATH_MAX + 10], *path, *skip = NULL; - int pathlen, keepstat, kind, dfd = -1, err; + PyObject *list, *elem, *stat, *ret = NULL; + char fullpath[PATH_MAX + 10]; + int kind, dfd = -1, err; struct stat st; struct dirent *ent; DIR *dir; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|Os:listdir", kwlist, - &path, &pathlen, &statflag, &skip)) - goto error_parse; - if (pathlen >= PATH_MAX) { PyErr_SetString(PyExc_ValueError, "path too long"); - goto error_parse; + goto error_value; } strncpy(fullpath, path, PATH_MAX); fullpath[pathlen] = '/'; - keepstat = statflag && PyObject_IsTrue(statflag); #ifdef AT_SYMLINK_NOFOLLOW dfd = open(path, O_RDONLY); if (dfd == -1) { PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); - goto error_parse; + goto error_value; } dir = fdopendir(dfd); #else @@ -375,12 +359,36 @@ #ifdef AT_SYMLINK_NOFOLLOW close(dfd); #endif -error_parse: +error_value: return ret; } #endif /* ndef _WIN32 */ +static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *statobj = NULL; /* initialize - optional arg */ + PyObject *skipobj = NULL; /* initialize - optional arg */ + char *path, *skip = NULL; + int wantstat, plen; + + static char *kwlist[] = {"path", "stat", "skip", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|OO:listdir", + kwlist, &path, &plen, &statobj, &skipobj)) + return NULL; + + wantstat = statobj && PyObject_IsTrue(statobj); + + if (skipobj && skipobj != Py_None) { + skip = PyString_AsString(skipobj); + if (!skip) + return NULL; + } + + return _listdir(path, plen, wantstat, skip); +} + static char osutil_doc[] = "Native operating system services."; static PyMethodDef methods[] = {