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

Christian Heimes report at bugs.python.org
Tue Nov 6 21:07:55 CET 2007


Christian Heimes added the comment:

Guido van Rossum wrote:
> (1) Don't use s#; it will allow null bytes in the string, which we don't
> want.
> 
> (2) Put the entire trailing slash removal inside #ifdef MS_WINDOWS.

Done aand done

> (3) Careful!  It seems the code you wrote would transform "C:/" into
> "C:" which isn't the same thing (the latter refers to the current
> directory on the C drive, while the former is the root of the C drive).

Oh, good catch! I haven't thought of C:/

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:\

Christian

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


More information about the Python-bugs-list mailing list