[PATCH] Improve error handling in osutil.c

Petr Kodl petrkodl at gmail.com
Wed Oct 1 07:41:33 CDT 2008


# HG changeset patch
# User Petr Kodl <petrkodl at gmail.com>
# Date 1222864878 14400
# Node ID 906930654fd950fa21a2becacfcc43efa7775b60
# Parent  9e6d6568bf7a21f900273de027d8975a4e0cecae
Improve error handling in osutil.c

1) In posix part set error when path is too long so instead of

SystemError: error returned without exception set

it will raise

ValueError: path too long

2) In Win32 part replace generic

PyErr_SetExcFromWindowsErrWithFilename

by

PyErr_SetFromWindowsErrWithFilename

The exception returned is WinError(based on OSError) and
some rudimentary errno translation is performed from Windows error range
to errno module friendly range so errors like ENOENT can be handled via symbolic
constant and consistently between Win32 and Posix.

diff -r 9e6d6568bf7a -r 906930654fd9 mercurial/osutil.c
--- a/mercurial/osutil.c	Mon Sep 29 12:12:53 2008 +0200
+++ b/mercurial/osutil.c	Wed Oct 01 08:41:18 2008 -0400
@@ -204,15 +204,11 @@
 			pattern[plen++] = '\\';
 	}
 	strcpy(pattern + plen, "*");
-
 	fh = FindFirstFileA(pattern, &fd);
 	if (fh == INVALID_HANDLE_VALUE) {
-		PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
-							GetLastError(),
-							path);
+		PyErr_SetFromWindowsErrWithFilename(GetLastError(),path);
 		goto error_file;
 	}
-
 	list = PyList_New(0);
 	if (!list)
 		goto error_list;
@@ -242,14 +238,10 @@
 
 		Py_XDECREF(item);
 	} while (FindNextFileA(fh, &fd));
-
 	if (GetLastError() != ERROR_NO_MORE_FILES) {
-		PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
-							GetLastError(),
-							path);
+		PyErr_SetFromWindowsErrWithFilename(GetLastError(),path);
 		goto error;
 	}
-
 	rval = list;
 	Py_XINCREF(rval);
 error:
@@ -293,10 +285,10 @@
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|Os:listdir", kwlist,
 					 &path, &pathlen, &statflag, &skip))
 		goto error_parse;
-
-	if (pathlen >= PATH_MAX)
+	if (pathlen >= PATH_MAX) {
+		PyErr_SetString(PyExc_ValueError,"path too long");
 		goto error_parse;
-
+	}
 	strncpy(fullpath, path, PATH_MAX);
 	fullpath[pathlen] = '/';
 	keepstat = statflag && PyObject_IsTrue(statflag);


More information about the Mercurial-devel mailing list