##// END OF EJS Templates
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot -
r7098:8a5c88c7 default
parent child Browse files
Show More
@@ -172,29 +172,19 b' static PyObject *make_item(const WIN32_F'
172 kind, py_st);
172 kind, py_st);
173 }
173 }
174
174
175 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
175 static PyObject *_listdir(char *path, int plen, int wantstat, char *skip)
176 {
176 {
177 PyObject *rval = NULL; /* initialize - return value */
177 PyObject *rval = NULL; /* initialize - return value */
178 PyObject *statobj = NULL; /* initialize - optional arg */
179 PyObject *list;
178 PyObject *list;
180 HANDLE fh;
179 HANDLE fh;
181 WIN32_FIND_DATAA fd;
180 WIN32_FIND_DATAA fd;
182 char *path, *pattern, *skip = NULL;
181 char *pattern;
183 int plen, wantstat;
184
185 static char *kwlist[] = {"path", "stat", "skip", NULL};
186
187 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|Os:listdir",
188 kwlist, &path, &plen, &statobj, &skip))
189 goto error_parse;
190
191 wantstat = statobj && PyObject_IsTrue(statobj);
192
182
193 /* build the path + \* pattern string */
183 /* build the path + \* pattern string */
194 pattern = malloc(plen+3); /* path + \* + \0 */
184 pattern = malloc(plen+3); /* path + \* + \0 */
195 if (!pattern) {
185 if (!pattern) {
196 PyErr_NoMemory();
186 PyErr_NoMemory();
197 goto error_parse;
187 goto error_nomem;
198 }
188 }
199 strcpy(pattern, path);
189 strcpy(pattern, path);
200
190
@@ -254,7 +244,7 b' error_list:'
254 FindClose(fh);
244 FindClose(fh);
255 error_file:
245 error_file:
256 free(pattern);
246 free(pattern);
257 error_parse:
247 error_nomem:
258 return rval;
248 return rval;
259 }
249 }
260
250
@@ -276,33 +266,27 b' int entkind(struct dirent *ent)'
276 return -1;
266 return -1;
277 }
267 }
278
268
279 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
269 static PyObject *_listdir(char *path, int pathlen, int keepstat, char *skip)
280 {
270 {
281 static char *kwlist[] = { "path", "stat", "skip", NULL };
271 PyObject *list, *elem, *stat, *ret = NULL;
282 PyObject *statflag = NULL, *list, *elem, *stat, *ret = NULL;
272 char fullpath[PATH_MAX + 10];
283 char fullpath[PATH_MAX + 10], *path, *skip = NULL;
273 int kind, dfd = -1, err;
284 int pathlen, keepstat, kind, dfd = -1, err;
285 struct stat st;
274 struct stat st;
286 struct dirent *ent;
275 struct dirent *ent;
287 DIR *dir;
276 DIR *dir;
288
277
289 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|Os:listdir", kwlist,
290 &path, &pathlen, &statflag, &skip))
291 goto error_parse;
292
293 if (pathlen >= PATH_MAX) {
278 if (pathlen >= PATH_MAX) {
294 PyErr_SetString(PyExc_ValueError, "path too long");
279 PyErr_SetString(PyExc_ValueError, "path too long");
295 goto error_parse;
280 goto error_value;
296 }
281 }
297 strncpy(fullpath, path, PATH_MAX);
282 strncpy(fullpath, path, PATH_MAX);
298 fullpath[pathlen] = '/';
283 fullpath[pathlen] = '/';
299 keepstat = statflag && PyObject_IsTrue(statflag);
300
284
301 #ifdef AT_SYMLINK_NOFOLLOW
285 #ifdef AT_SYMLINK_NOFOLLOW
302 dfd = open(path, O_RDONLY);
286 dfd = open(path, O_RDONLY);
303 if (dfd == -1) {
287 if (dfd == -1) {
304 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
288 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
305 goto error_parse;
289 goto error_value;
306 }
290 }
307 dir = fdopendir(dfd);
291 dir = fdopendir(dfd);
308 #else
292 #else
@@ -375,12 +359,36 b' error_dir:'
375 #ifdef AT_SYMLINK_NOFOLLOW
359 #ifdef AT_SYMLINK_NOFOLLOW
376 close(dfd);
360 close(dfd);
377 #endif
361 #endif
378 error_parse:
362 error_value:
379 return ret;
363 return ret;
380 }
364 }
381
365
382 #endif /* ndef _WIN32 */
366 #endif /* ndef _WIN32 */
383
367
368 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
369 {
370 PyObject *statobj = NULL; /* initialize - optional arg */
371 PyObject *skipobj = NULL; /* initialize - optional arg */
372 char *path, *skip = NULL;
373 int wantstat, plen;
374
375 static char *kwlist[] = {"path", "stat", "skip", NULL};
376
377 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|OO:listdir",
378 kwlist, &path, &plen, &statobj, &skipobj))
379 return NULL;
380
381 wantstat = statobj && PyObject_IsTrue(statobj);
382
383 if (skipobj && skipobj != Py_None) {
384 skip = PyString_AsString(skipobj);
385 if (!skip)
386 return NULL;
387 }
388
389 return _listdir(path, plen, wantstat, skip);
390 }
391
384 static char osutil_doc[] = "Native operating system services.";
392 static char osutil_doc[] = "Native operating system services.";
385
393
386 static PyMethodDef methods[] = {
394 static PyMethodDef methods[] = {
General Comments 0
You need to be logged in to leave comments. Login now