[mod_python] import_module is case sensitive even if the OS isn't

Simon Holness simon.holness at gmail.com
Mon May 14 10:09:18 EDT 2007

On 5/14/07, Mike Looijmans <nlv11281 at natlab.research.philips.com> wrote:
> There is a system call that discloses whether a file system is case sensitive or not. However, the
> problem is not trivial, as there may be links to other devices in the path. For example,
> C:\mnt\somedir might point to another drive with a case sensitive filesystem, which would make the
> "mnt" word case insensitive and the "somedir" word case sensitive. I doubt that the normcase()
> function can cope with that, but i did not even know of its existance up until now.
> I'm using an ext2 filesystem under Windows 2000 on a harddisk, and that's local and case sensitive
> indeed.

Interesting. Have you looked at how normpath() and normcase() work?
I've not seen that used before - does windows behave 'correctly' wrt
case sensitivity? (i.e. rejecting paths which are case incorrect)?

> Probably the safest thing is to open the file and ask whether it's the same file.
> The same question then would apply to unix/linux systems, would you want the module loaded as
> "/real/path.py" to be considered the same module as "/softlink/path.py" ?
> With all of that - i think it's reasonable to require the same module to be spelled the same way.
> The cure seems more harmful than the disease, since it allows the module importer to "see" through
> links and unexpectedly share globals in those modules.

import already uses 'normpath()' so that paths do not have to be
identical. There is a cryptic comment in the os.path docs about
symlinks, too: "It should be understood that this may change the
meaning of the path if it contains symbolic links!", so I suspect that
this can of worms is already open (maybe entirely wrong - I don't know
enough about the subtleties of normpath/normcase).

normcase() could also be used just to produce the label used to key
the module within the cache, rather than performing the lookup / file
operations on a case-munged path. Thus file-system interraction (e.g.
using a case-sensitive file system where normcase doesn't expect it).

e.g.    label = self._module_label(os.path.normcase(file))

This use of normcase() would only "break" things if you were loading
modules from two paths which differed only by case (after a normpath()
operation, I think?) on an OS which is (normally) case insensitive,
which is a fairly pathological and ill-advised thing to be doing.

Having just looked at the code, all the os.path stuff is specific to
operating system - posix / NT / mac. I would assume that these
behaviours are also embedded pretty deeply elsewhere in python.

I'm just a little wary about having import_path silently deviate from
its stated behaviour ("distinguishing modules by location"). I was
watching imports carefully so spotted it, but I would imagine most
people would struggle to understand why "~/mymodule" and
"c:\mypath\mymodule" was causing the module to be loaded twice even
though it's in the same location.


More information about the Mod_python mailing list