I wrote code a couple of weeks ago to switch from using execfile to using the "imp" module to import contexts as packages, and subdirectories of those contexts as subpackages. In my testing this fixes all the problems you referred to, including your case of support.py in a directory as well as a subdirectory, and also the case of modules with the same name in different contexts. I'll clean up my changes and check into CVS sometime after 0.5.1 is finalized.
Terrel Shumway wrote:
> In Application.getServlet there is a fragment that reads
> if not dir in sys.path:
> sys.path.insert(0, dir)
> I am curious about why this is necessary. Since PythonServletFactory uses execfile to load the servlet, the path does not matter, provided the servlet does not import any modules in the same directory. If the servlet in question does do this (A Bad Thing(tm)), then the fragment above can break things badly.
> (yes,these are bad names, but they illustrate the problem)
> GET /bugware/
> maps to bugware/index.py which says "import support"
> GET /bugware/foo/
> maps to bugware/foo/index.py which says "import support"
> but, since support was already imported, this index also gets <module 'support' from 'bugware/support.py'>
> Granted, this is a pathological case, but what if two independently developed applications deploy in different contexts, with each application defining a class SitePage (or worse: SecurePage): the first context accessed wins.
> I already know the answer: run them in separate instances of Webware. This is a very good idea if you care at all about security.
> In addition to this idea, I would suggest:
> 1) Use qualified module names in imports: "myorg.myapp.web.SitePage"
> 2) Add a single context defined library directory to sys.path (probably in bugware/__init__.py) Put all support code there.
> 3) Do not support importing sibling modules from within the document tree (i.e. remove the snippet mentioned at the beginning of this message). If it breaks immediately with an ImportError, people will be able to fix it, rather than waiting two years for a problem to appear "sometimes" and have the maintainers puzzle over why.
> Am I missing somthing? Is there another reason to add os.path.dirname(servletfile) to sys.path?
> -- Terrel
> BTW: It would be nice for each context to have its own private sys.path and sys.modules, but this would be difficult in a threaded environment without modifying the python interpreter.
> Webware-discuss mailing list
- Geoff Talvola