[mod_python] psp.PSP(req, string='hello world') causes segfault

Graham Dumpleton grahamd at dscpl.com.au
Tue Apr 26 19:46:53 EDT 2005


dharana wrote ..
> -- site-packages/mod_python/psp.py snippet ---------------------
>          if filename:
> 
>              # if filename is not absolute, default to our guess
>              # of current directory
>              if not os.path.isabs(filename):
>                  base = os.path.split(req.filename)[0]
>                  self.filename = os.path.join(base, filename)
> 
>              self.load_from_file()
>          else:
> 
>              cached = mem_scache.get(string)
>              if cached:
>                  self.code = cached
>              else:
> (line 118)      self.code = _psp.parsestring(string)
>                  mem_scache.store(string)
> -----------------------------------------------------------------
> 
> Commenting out line 118 and trying to run the script in the previous 
> email (the one that is causing the segfault) gives me this error:
> 
> --- traceback -------------------------------------------------------
>
> ...
> TypeError: store() takes exactly 3 arguments (2 given)
> -------------------------------------------------------------------
> 
> I don't know why this is failling, maybe it has something to do with the
> problem. _psp.parsestring(string) shouldn't modify "string" in any way
> nor should it be able to pass hidden args to the mem_scache.store() 
> function, right?

The store() method is expecting a key and value argument. The string
would be the value argument, but what could the key be set to. Can't
see any way you could automagically produce a value for the key. You
can't use req.filename by itself as the file could create multiple PSP
objects with different strings.

My thinking is that you almost need to have the user of the PSP class
supply an argument to optionally tag the string based PSP object.
The default key could be req.filename and if that optional tag is set,
it could be appended to the req.filename separated by a "?" or other
magic character.

Thus:

  class PSP:
    
    def __init__(self, req, filename=None, string=None, vars={}, tag=""): 

       ...

            key = "%s?%s" % (req.filename,tag)
            cached = mem_scache.get(key,string)
            if cached:
                self.code = cached
            else:
                self.code = _psp.parsestring(string)
                mem_scache.store(key,string)                                          

Graham


More information about the Mod_python mailing list