You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mod_python-commits@quetz.apache.org by gr...@apache.org on 2003/05/30 06:37:10 UTC

cvs commit: httpd-python/src _pspmodule.c psp_parser.l

grisha      2003/05/29 21:37:10

  Modified:    lib/python/mod_python apache.py psp.py
               src      _pspmodule.c psp_parser.l
  Log:
  Added support for "compiled" psp files. The compiled files contain marshalled
  tuples representing code objects that can be easily reconstructed using the new
  module. The actual speed up does not seem as spectacular - 1.5 to 3 times in my
  tests... oh well.
  
  Revision  Changes    Path
  1.69      +4 -3      httpd-python/lib/python/mod_python/apache.py
  
  Index: apache.py
  ===================================================================
  RCS file: /home/cvs/httpd-python/lib/python/mod_python/apache.py,v
  retrieving revision 1.68
  retrieving revision 1.69
  diff -u -r1.68 -r1.69
  --- apache.py	28 Feb 2003 04:55:15 -0000	1.68
  +++ apache.py	30 May 2003 04:37:09 -0000	1.69
  @@ -415,11 +415,12 @@
                       return HTTP_INTERNAL_SERVER_ERROR
                   else:
                       # write to client
  -                    req.content_type = 'text/plain'
  +                    req.content_type = 'text/html'
   
  -                    s = '\nMod_python error: "%s %s"\n\n' % (phase, hname)
  +                    s = '\n<pre>\nMod_python error: "%s %s"\n\n' % (phase, hname)
                       for e in traceback.format_exception(etype, evalue, etb):
                           s = s + e + '\n'
  +                    s = s + "</pre>\n"
                           
                       if filter:
                           filter.write(s)
  
  
  
  1.6       +62 -7     httpd-python/lib/python/mod_python/psp.py
  
  Index: psp.py
  ===================================================================
  RCS file: /home/cvs/httpd-python/lib/python/mod_python/psp.py,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- psp.py	29 May 2003 20:52:25 -0000	1.5
  +++ psp.py	30 May 2003 04:37:09 -0000	1.6
  @@ -56,10 +56,20 @@
    #
    # $Id$
   
  -from mod_python import apache
  -import sys
  +# this trick lets us be used outside apache
  +try:
  +    from mod_python import apache
  +except:
  +    from mod_python import apache
  +    apache.OK = 0
  +    
   import _psp
   
  +import sys
  +import os
  +import marshal
  +import new
  +
   def parse(filename):
   
       return _psp.parse(req.filename)
  @@ -68,13 +78,58 @@
   
       return _psp.parsestring(str)
   
  -def handler(req):
  +def code2str(c):
   
  -    source = _psp.parse(req.filename)
  +    ctuple = (c.co_argcount, c.co_nlocals, c.co_stacksize, c.co_flags,
  +              c.co_code, c.co_consts, c.co_names, c.co_varnames, c.co_filename,
  +              c.co_name, c.co_firstlineno, c.co_lnotab)
  +    
  +    return marshal.dumps(ctuple)
  +
  +def str2code(s):
  +
  +    return apply(new.code, marshal.loads(s))
  +
  +def load_file(filename):
  +
  +    """ This function will check for existence of a file with same
  +    name, but ending with c and load it instead. The c file contains
  +    already compiled code (see code2str above). My crude tests showed
  +    that mileage vaires greatly as to what the actual speedup would
  +    be, but on average for files that are mostly strings and not a lot
  +    of Python it was 1.5 - 3 times. The good news is that this means
  +    that the PSP lexer and the Python parser are *very* fast, so
  +    compiling PSP pages is only necessary in extreme cases. """
  +
  +    smtime = 0
  +    cmtime = 0
  +
  +    if os.path.isfile(filename):
  +        smtime = os.path.getmtime(filename)
  +
  +    name, ext = os.path.splitext(filename)
  +
  +    cext = ext[:-1] + "c"
  +    cname = name + cext
  +    
  +    if os.path.isfile(name + cext):
  +        cmtime = os.path.getmtime(cname)
  +
  +        if cmtime > smtime:
  +            # we've got a code file!
  +            code = str2code(open(name + cext).read())
  +
  +            return code
  +                
  +    source = _psp.parse(filename)
  +    return compile(source, filename, "exec")
  +
  +
  +def handler(req):
   
  -    code = compile(source, req.filename, "exec")
  +    code = load_file(req.filename)
   
       # give it it's own locals
       exec code in globals(), {"req":req}
   
  -    return mod_python.apache.OK
  +    return apache.OK
  
  
  
  1.3       +13 -6     httpd-python/src/_pspmodule.c
  
  Index: _pspmodule.c
  ===================================================================
  RCS file: /home/cvs/httpd-python/src/_pspmodule.c,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- _pspmodule.c	29 May 2003 20:52:25 -0000	1.2
  +++ _pspmodule.c	30 May 2003 04:37:09 -0000	1.3
  @@ -110,13 +110,16 @@
           return NULL;
       }
       
  +    Py_BEGIN_ALLOW_THREADS
       f = fopen(filename, "rb");
  -    
  +    Py_END_ALLOW_THREADS
  +
       if (f == NULL) {
           PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
           return NULL;
       }
   
  +    Py_BEGIN_ALLOW_THREADS
       parser = psp_parser_init();
   
       yylex_init(&scanner);
  @@ -124,12 +127,13 @@
       yyset_extra(parser, scanner);
       yylex(scanner);
       yylex_destroy(scanner);
  -    
  -    fclose(f);
   
  +    fclose(f);
       psp_string_0(&parser->pycode);
  -    code = PyString_FromString(parser->pycode.blob);
   
  +    Py_END_ALLOW_THREADS
  +
  +    code = PyString_FromString(parser->pycode.blob);
       psp_parser_cleanup(parser);
       
       return code; 
  @@ -147,6 +151,7 @@
           return NULL;
       }
   
  +    Py_BEGIN_ALLOW_THREADS
       parser = psp_parser_init();
       yylex_init(&scanner);
       yyset_extra(parser, scanner);
  @@ -158,10 +163,12 @@
       yylex_destroy(scanner);
       
       psp_string_0(&parser->pycode);
  +    Py_END_ALLOW_THREADS
  +
       code = PyString_FromString(parser->pycode.blob);
   
       psp_parser_cleanup(parser);
  -    
  +
       return code; 
   }
   
  
  
  
  1.7       +7 -1      httpd-python/src/psp_parser.l
  
  Index: psp_parser.l
  ===================================================================
  RCS file: /home/cvs/httpd-python/src/psp_parser.l,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- psp_parser.l	29 May 2003 20:52:25 -0000	1.6
  +++ psp_parser.l	30 May 2003 04:37:09 -0000	1.7
  @@ -59,6 +59,12 @@
    * 
    */
   
  +/* NOTE The seemingly unusual generated Python code (sometime using
  + * ";" to separate statements, newline placement, etc) is such that
  + * for vast majority of cases the line number of the input file will
  + * match the line number of the output!
  + */
  +
   #include "psp_parser.h"
   
   #define OUTPUT_WHITESPACE(__wsstring) \