[mod_python] psp import issue

Graham Dumpleton graham.dumpleton at gmail.com
Thu Jun 21 20:45:53 EDT 2007


On 22/06/07, maker joe <makerjoe at gmail.com> wrote:
> great!!!!! your suggeted hack worked

There is also now a patch attached to:

  https://issues.apache.org/jira/browse/MODPYTHON-220

which fixes the problem in mod_python if you want to apply it. Look at
man page for 'patch' program if you don't know how to do this.

> btw
> do you know how to setup publisher/psp to replace:
>
> req.write('something') by print 'something'  eg (sys.stdout=rec)

Use:

  print >> req, "something"

To be able to say just:

  print "something"

needs more elaborate techniques. The web.py package allows this but it
requires special thread keyed version of sys.stdout.

> and
> req.form['varx']  by _varx

Don't recommend pushing form fields in local name space as doesn't
make it as obvious that it is a form field and there would be a
tendency not to perform checks to make sure the field you are looking
for actually exists. Also, it will all possibly blow up if someone
supplied a field you weren't expecting which replaced some important
data or clashed with a keyword.

If you really must do something like that though, you might adapt the
following code from mod_python.util.apply_fs_data(). Replace 'fs' with
req.form and replace args with locals().

    # add form data to args
    for field in fs.list:
        if field.filename:
            val = field
        else:
            val = field.value
        args.setdefault(field.name, []).append(val)

    # replace lists with single values
    for arg in args:
        if ((type(args[arg]) is ListType) and
            (len(args[arg]) == 1)):
            args[arg] = args[arg][0]

> like in karrigell

Having too much magic is not always good. Karrigell severely limits
its deployment options because of how it is implemented. Not as bad as
Karrigell, but web.py also pays for allowing 'print' to work to
generate response content in that it can cause conflicts when multiple
applications are being hosted. You can see a bit of discussion about
the problems these packages have when used with Apache at:

  http://code.google.com/p/modwsgi/wiki/IntegrationWithKarrigell
  http://code.google.com/p/modwsgi/wiki/IntegrationWithWebPy

This documents are about mod_wsgi, but similar issues exist if
mod_python were being used.

PS. Can you use reply-all so messages go back to list.

Graham

> cheers
>
>
> On 6/21/07, Graham Dumpleton <graham.dumpleton at gmail.com> wrote:
> > 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