[issue1293] Trailing slash in sys.path cause import failure

Guido van Rossum report at bugs.python.org
Tue Nov 6 21:19:38 CET 2007


Guido van Rossum added the comment:

> How do you like
> #ifdef MS_WINDOWS
>                 /* Remove trailing / and \ - Windows' stat doesn't like them - but
>                  * keep the trailing slash of C:/
>                  */
>
>                 Py_ssize_t i;
>                 char mangled[MAXPATHLEN+1];
>
>                 if (pathlen > MAXPATHLEN) {
>                         PyErr_SetString(PyExc_OverflowError,
>                                         "path is too long");
>                         return -1;
>                 }
>                 strcpy(mangled, path);
>
>                 for (i = pathlen-1; i > 3; i--) {
>                         if (mangled[i] != '/' && mangled[i] != '\\') {
>                                 break;
>                         }
>                         mangled[i] = '\0';
>                 }
>                 rv = stat(mangled, &statbuf);
> #else
>                 rv = stat(path, &statbuf);
> #endif
>
> i > 3 should take care of C:/ and C:\

But the C: is optional. You will need to do more parsing. Some more
examples (all with slashes; but backslashes work the same):

//share/  -> //share/
//share/x/ -> //share/x
/x/ -> /x
/ -> /
// -> // (probably illegal but doesn't matter)
C:/ -> C:/
x/ -> x
C:x/ -> C:x

Isn't it easier to handle this at the Python level? There probably
already is code for parsing Windows paths. (Hm, maybe we have it in C
too somewhere?)

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1293>
__________________________________


More information about the Python-bugs-list mailing list