[mod_python] Include file without writing output

Andreas Sommer AndiDog at web.de
Wed Jul 1 08:46:15 EDT 2009


I want to include a common "header" file in each of my PSP files (which 
should produce XML), for example

<%@ include file="common.inc" %><%
req.content_type = "text/xml"
# output some XML code here
%>


The "common.inc" file begins with <% and ends with %>, but because 
editors automatically add a newline, it ends with a newline character.
My problem is that the XML output follows after the newline character, 
but *must* be at the very beginning of the output - just try opening 
such a XML file in Firefox and it will give you an error "XML or text 
declaration not at start of entity". Removing the newline character 
causes mod_python to throw an error.


I tried to work around that problem by including the common file like this:

PSP(req, "common.inc").run()

...but that gives me the following error:

MOD_PYTHON ERROR

ProcessId:      2787
Interpreter:    '127.0.1.1'

ServerName:     '127.0.1.1'
DocumentRoot:   '/var/www/'

URI:            '/login.psp'
Location:       None
Directory:      '/var/www/'
Filename:       '/var/www/login.psp'
PathInfo:       ''

Phase:          'PythonHandler'
Handler:        'mod_python.psp'

Traceback (most recent call last):

  File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 
1537, in HandlerDispatch
    default=default_handler, arg=req, silent=hlist.silent)
  File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 
1229, in _process_target
    result = _execute_target(config, req, object, arg)
  File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 
1128, in _execute_target
    result = object(arg)
  File "/usr/lib/python2.5/site-packages/mod_python/psp.py", line 337, 
in handler
    p.run()
  File "/usr/lib/python2.5/site-packages/mod_python/psp.py", line 243, 
in run
    exec code in global_scope
  File "/var/www/login.psp", line 2, in <module>
    PSP(req, filename = "common.inc")
  File "/usr/lib/python2.5/site-packages/mod_python/psp.py", line 119, 
in __init__
    self.load_from_file()
  File "/usr/lib/python2.5/site-packages/mod_python/psp.py", line 185, 
in load_from_file
    code = self.cfile_get(filename, mtime)
  File "/usr/lib/python2.5/site-packages/mod_python/psp.py", line 169, 
in cfile_get
    return str2code(open(cname).read())
  File "/usr/lib/python2.5/site-packages/mod_python/psp.py", line 57, in 
str2code
    return new.code(*marshal.loads(s))
MemoryError


Using the absolute path /var/www/common.inc doesn't work either. Any 
suggestions how to work around this problem??


More information about the Mod_python mailing list