Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ace88ae

Browse files
committed
Patch by Brian Hooper, somewhat augmented by GvR, to strip a trailing
backslash from the pathname argument to stat() on Windows -- while on Unix, stat("/bin/") succeeds and does the same thing as stat("/bin"), on Windows, stat("\\windows\\") fails while stat("\\windows") succeeds. This modified version of the patch recognizes both / and \. (This is odd behavior of the MS C library, since os.listdir("\\windows\\") succeeds!)
1 parent e0cd291 commit ace88ae

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Modules/posixmodule.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,36 @@ posix_do_stat(self, args, format, statfunc)
549549
struct stat st;
550550
char *path;
551551
int res;
552+
553+
#ifdef MS_WIN32
554+
int pathlen;
555+
char pathcopy[MAX_PATH];
556+
#endif /* MS_WIN32 */
557+
552558
if (!PyArg_ParseTuple(args, format, &path))
553559
return NULL;
560+
561+
#ifdef MS_WIN32
562+
pathlen = strlen(path);
563+
/* the library call can blow up if the file name is too long! */
564+
if (pathlen > MAX_PATH) {
565+
errno = ENAMETOOLONG;
566+
return posix_error();
567+
}
568+
569+
if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) {
570+
/* exception for drive root */
571+
if (!((pathlen == 3) &&
572+
(path[1] == ':') &&
573+
(path[2] == '\\' || path[2] == '/')))
574+
{
575+
strncpy(pathcopy, path, pathlen);
576+
pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */
577+
path = pathcopy;
578+
}
579+
}
580+
#endif /* MS_WIN32 */
581+
554582
Py_BEGIN_ALLOW_THREADS
555583
res = (*statfunc)(path, &st);
556584
Py_END_ALLOW_THREADS

0 commit comments

Comments
 (0)