[mod_python] psp import issue

Graham Dumpleton graham.dumpleton at gmail.com
Thu Jun 21 20:03:28 EDT 2007


On 22/06/07, maker joe <makerjoe at gmail.com> wrote:
> many thanks for your suggestions
> but let me explain myself more carefully
>
> you said:
> and then reference via module rather than trying to do import '*' into
> local namespace
>
> how can i avoid using the reference via module and have
> module.vars/defs as local varsdefs as if i use "from module import * ?

All your problems will be solved when the bug I referenced in the
first place is fixed. Ie., you will be able to use 'from module import
*' for a module in the same directory as your PSP page.

If you can't wait that long, do not put your modules in the same
directory as your .psp files, put them in a directory which is outside
of your document tree. Then use PythonPath directive to add that
external directory to the standard module search path. By doing that
the mod_python module importer will be skipped and 'from module import
*' will work for you.

Note, do not add the document directory itself to PythonPath as you
shouldn't overlap mod_python module path and standard Python module
path.

Also note though that by putting them in a directory outside of the
document tree and thereby using the standard Python module importer,
those modules will not be candidates for automatic module reloading.
Thus, whenever you make a change to them, you will need to restart
Apache for the change to be picked up.

The only other hack I can suggest is to do something like:

  module = apache.import_module(module_name)
  globals().update(module.__dict__)

Graham

> On 6/21/07, Graham Dumpleton <graham.dumpleton at gmail.com> wrote:
> > On 22/06/07, maker joe <makerjoe at gmail.com> wrote:
> > > hi graham
> > > how insecure is the following code
> > >
> > > exec "from %s import * " % 'test'
> > >
> > > if this is insecure what would be a secure alternative to get imported
> > > vars/funcs at local namespace?
> >
> > In this case 'test' is a literal string. If it wasn't a literal string
> > but somehow derived from user input it would be very dangerous.
> >
> > Anyway, you don't need to do that. Use:
> >
> >   module = apache.import_module(module_name)
> >
> > and then reference via module rather than trying to do import '*' into
> > local namespace.
> >
> > See documentation for import_module() in:
> >
> >   http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html
> >
> > BTW, never call modules 'test' as Python provides a standard module
> > called that and thus you can get yourself in a knot when it somehow
> > picks up the standard one and not yours.
> >
> > Graham
> >
> > > thank you
> > > joseluis
> > >
> > > On 6/19/07, Graham Dumpleton <graham.dumpleton at gmail.com> wrote:
> > > > Issue noted at:
> > > >
> > > >   https://issues.apache.org/jira/browse/MODPYTHON-220
> > > >
> > > > You should be able to use:
> > > >
> > > >   somemodule = apache.import_module("somemodule")
> > > >
> > > > instead.
> > > >
> > > > Graham
> > > >
> > > > On 20/06/07, maker joe <makerjoe at gmail.com> wrote:
> > > > > hi
> > > > > how can i import a module from the current directory on a psp file
> > > > > eg
> > > > > test.psp
> > > > > <%
> > > > > import somemodule
> > > > > %>
> > > > > somemodule.py is at the same directory as test.psp
> > > > >
> > > > > the same works importing from a file.py
> > > > > im using publisher and psp hanlers
> > > > > python 2.5 modputhon 3.31
> > > > >
> > > > > thanks
> > > > > joseluis
> > > > > _______________________________________________
> > > > > Mod_python mailing list
> > > > > Mod_python at modpython.org
> > > > > http://mailman.modpython.org/mailman/listinfo/mod_python
> > > > >
> > > >
> > >
> >
>


More information about the Mod_python mailing list