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

simon holness simon.holness at gmail.com
Fri May 11 16:47:24 EDT 2007


Hi,

I've found an apparent issue with apache.import_module() - it seems to
key the module on a case sensitive path/name even if the operating
system (i.e. windows) doesn't. Thus a module can be wrongly loaded
twice by using different capitalisation in different import requests.

For most applications this will not be an issue since the separately
loaded modules will be distinct. If, however, and application is
relying on a module being loaded only once per interpreter (yeah,
guess which case I've got) it can cause problems.

This can be trivially reproduced by importing the same module twice
with differing pathnames.

e.g.

mytest.psp
-------------------------
<%
mod1 = apache.import_module("c:\\work\\Webroot\\mymodule.py") # nb cap W
mod2 = apache.import_module("c:\\work\\webroot\\mymodule.py")
%>
<html>
<head><title></title></head>
<body>go read your apache error log to see what got loaded</body>
</html>
------------------------
mymodule.py
------------------------
# I'm a very boring module
pass
------------------------

error.log contains
------------------------
....
... Importing module 'c:\\work\\Webroot\\mymodule.py'
... Importing module 'c:\\work\\webroot\\mymodule.py'

This, obviously, won't be an issue on any case-sensitive OS (*nix).

Having had a quick look at the code (and run a very simple test) it
appears that altering importer.py line 454 to

file = os.path.normcase(os.path.normpath(file))

fixes the issue.
I haven't looked to see if there are other places where the path would
need to be normalised. Possibly in _setup_current_cache() ?

normcase() should be safe & correct on both types of system.

Cheers
Simon Holness


More information about the Mod_python mailing list