[albatross-users] mod_python and global namespace

Dave Cole djc at object-craft.com.au
Mon Dec 23 13:33:24 EST 2002


> When running as a CGI, I can access the script global namespace from
> templates, so I can do things like:
> 	<al-value expr="os.path">
> 
> Under mod_python(3.0.1), this doesn't work, and I have to import
> globals into ctx.locals to make them available.
> 
> Is this:
>  - An inevitable consequence of using mod_python
>  - Exploiting an unexpected bug in the CGI case
>  - a bug in albatross.apacheapp

It looks like it is a bug in Albatross.  What Albatross tries to do is
create an environment for expression evaluation such that
        eval(expr, globals, locals)

maps globals to the global namespace of the module which called
run_template() and locals to ctx.locals.  The method for locating the
global namespace (context._caller_globals()) is not working in your
case.

At the moment _caller_globals() just counts back a number of stack
frames.  It should probably be modified to keep going back past a
named function.  So, AppContext.run_template() would look something
like this:

    def run_template(self, name):
        templ = self.app.load_template(name)
        self.set_globals(_caller_globals('run_template'))
        templ.to_html(self)

Then _caller_globals() would have to be something like this:

    def caller_globals(name):
        try:
            raise ZeroDivisionError
        except ZeroDivisionError:
            frame = sys.exc_traceback.tb_frame
        while frame.f_code.co_name != name:
            frame = frame.f_back
        while frame.f_code.co_name == name:
            frame = frame.f_back
        return frame.f_globals

- Dave

-- 
http://www.object-craft.com.au




More information about the Albatross-users mailing list