diff --git a/mercurial/osutil.c b/mercurial/osutil.c
index 6156edaa82aab1193875f8ccd9c2cbe5661996b9_bWVyY3VyaWFsL29zdXRpbC5j..2d2c0a8eeeb8764daa94400e7de16a02971361a9_bWVyY3VyaWFsL29zdXRpbC5j 100644
--- a/mercurial/osutil.c
+++ b/mercurial/osutil.c
@@ -410,8 +410,8 @@
 		return NULL;
 
 	for (i = 0; i < count; i++) {
-		PyObject *stat;
+		PyObject *stat, *pypath;
 		struct stat st;
 		int ret, kind;
 		char *path;
 
@@ -414,6 +414,9 @@
 		struct stat st;
 		int ret, kind;
 		char *path;
 
-		path = PyString_AsString(PySequence_GetItem(names, i));
+		pypath = PySequence_GetItem(names, i);
+		if (!pypath)
+			return NULL;
+		path = PyString_AsString(pypath);
 		if (path == NULL) {
@@ -419,5 +422,6 @@
 		if (path == NULL) {
+			Py_DECREF(pypath);
 			PyErr_SetString(PyExc_TypeError, "not a string");
 			goto bail;
 		}
 		ret = lstat(path, &st);
@@ -420,7 +424,8 @@
 			PyErr_SetString(PyExc_TypeError, "not a string");
 			goto bail;
 		}
 		ret = lstat(path, &st);
+		Py_DECREF(pypath);
 		kind = st.st_mode & S_IFMT;
 		if (ret != -1 && (kind == S_IFREG || kind == S_IFLNK)) {
 			stat = makestat(&st);