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/09/08 21:31:51 UTC

cvs commit: httpd-python/src mod_python.c

grisha      2003/09/08 12:31:51

  Modified:    Doc      modpython.tex modpython4.tex modpython6.tex
               lib/python/mod_python apache.py psp.py
               src      mod_python.c
  Log:
  Updated docs to reflect the new PSP class.
  
  Fixed the way mod_python figures out maximum possible number of clients
  (it was incorrectly arrivind at what amounted to ServerLimit as oppsed
  to MaxClients).
  
  It now will free one mutex if it runs out of mutexes in the process of
  allocation, thereby avoiding the problem with server startup on RedHat
  which has a default limit of 128 semaphores. The freed last mutex  is
  needed by another module (mod_rewrite i think).
  
  Revision  Changes    Path
  1.24      +1 -1      httpd-python/Doc/modpython.tex
  
  Index: modpython.tex
  ===================================================================
  RCS file: /home/cvs/httpd-python/Doc/modpython.tex,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- modpython.tex	26 Aug 2003 18:44:34 -0000	1.23
  +++ modpython.tex	8 Sep 2003 19:31:50 -0000	1.24
  @@ -7,7 +7,7 @@
   % Please at least include a long-lived email address;
   % the rest is at your discretion.
   \authoraddress{
  -	E-mail: \email{grisha@modpython.org}
  +	E-mail: \email{grisha@apache.org}
   }
   
   % do not mess with the 2 lines below, they are written by make dist
  
  
  
  1.56      +144 -21   httpd-python/Doc/modpython4.tex
  
  Index: modpython4.tex
  ===================================================================
  RCS file: /home/cvs/httpd-python/Doc/modpython4.tex,v
  retrieving revision 1.55
  retrieving revision 1.56
  diff -u -r1.55 -r1.56
  --- modpython4.tex	22 Aug 2003 02:22:44 -0000	1.55
  +++ modpython4.tex	8 Sep 2003 19:31:50 -0000	1.56
  @@ -279,7 +279,7 @@
   \section{\module{apache} -- Access to Apache Internals.}
   \declaremodule[apache]{extension}{apache}
   \modulesynopsis{Access to Apache Internals}
  -\moduleauthor{Gregory Trubetskoy}{grisha@modpython.org}
  +\moduleauthor{Gregory Trubetskoy}{grisha@apache.org}
   
   The Python interface to Apache internals is contained in a module
   appropriately named \module{apache}, located inside the
  @@ -1348,7 +1348,7 @@
   \section{\module{util} -- Miscellaneous Utilities\label{pyapi-util}}
   \declaremodule[util]{extension}{util}
   \modulesynopsis{Miscellaneous Utilities}
  -\moduleauthor{Gregory Trubetskoy}{grisha@modpython.org}
  +\moduleauthor{Gregory Trubetskoy}{grisha@apache.org}
   
   The \module{util} module provides a number of utilities handy to a web
   application developer similar to those in the standard library
  @@ -1559,7 +1559,7 @@
   \section{\module{Cookie} -- HTTP State Management\label{pyapi-cookie}}
   \declaremodule[Cookie]{extension}{Cookie}
   \modulesynopsis{HTTP State Management}
  -\moduleauthor{Gregory Trubetskoy}{grisha@modpython.org}
  +\moduleauthor{Gregory Trubetskoy}{grisha@apache.org}
   
   The \module{Cookie} module provides convenient ways for creating,
   parsing, sending and receiving HTTP Cookies, as defined in the
  @@ -1786,7 +1786,7 @@
   \section{\module{Session} -- Session Management\label{pyapi-sess}}
   \declaremodule[Session]{extension}{Session}
   \modulesynopsis{Session Management}
  -\moduleauthor{Gregory Trubetskoy}{grisha@modpython.org}
  +\moduleauthor{Gregory Trubetskoy}{grisha@apache.org}
   
   The \module{Session} module provides objects for maintaining persistent
   sessions across requests.
  @@ -1981,30 +1981,24 @@
   
   \end{classdesc}
   
  -\section{\module{_psp} -- Python Server Pages\label{pyapi-psp}}
  -\declaremodule[psp]{extension}{_psp}
  +\section{\module{psp} -- Python Server Pages\label{pyapi-psp}}
  +\declaremodule[psp]{extension}{psp}
   \modulesynopsis{Python Server Pages}
  -\moduleauthor{Gregory Trubetskoy}{grisha@modpython.org}
  +\moduleauthor{Gregory Trubetskoy}{grisha@apache.org}
   
  -The \module{_psp} module provides a way to convert text documents
  +The \module{psp} module provides a way to convert text documents
   (including, but not limited to HTML documents) containing Python code
   embedded in special brackets into pure Python code suitable for
   execution within a mod_python handler, thereby providing a versatile
  -mechanism for delivering dynamic content in a style similar to similar
  -to ASP, JSP and others.
  +mechanism for delivering dynamic content in a style similar to ASP,
  +JSP and others.
   
  -The parser used by \module{_psp} is written in C (generated using flex)
  +The parser used by \module{psp} is written in C (generated using flex)
   and is therefore very fast.
   
  -Unlike other mod_python modules, \module{_psp} is written in such a way
  -that it can be imported outside of the Apache httpd process, e.g. in
  -stand-alone command-line programs.
  -
   \emph{See \ref{hand-psp} ``PSP Handler'' for additional PSP
   information.}
   
  -\subsection{PSP Syntax\label{pyapi-psp-syntax}}
  -
   Inside the document, Python \dfn{code} needs to be surrounded by
   \samp{<\%} and \samp{\%>}. Python \dfn{expressions} are enclosed in
   \samp{<\%=} and \samp{\%>}. A \dfn{directive} can be enclosed in
  @@ -2111,7 +2105,139 @@
   argument, then the file can be specified as a relative path, otherwise
   it has to be absolute.
   
  -\subsection{Functions\label{pyapi-psp-funcs}}
  +\begin{classdesc}{PSP}{req, \optional{, filename, string}}
  +  This class represents a PSP object.
  +
  +  \var{req} is a request object; \var{filename} and \var{string} are
  +  optional keyword arguments which indicate the source of the PSP
  +  code. Only one of these can be specified. If neither is specified,
  +  \code{req.filename} is used as \var{filename}.
  +
  +  This class is used internally by the PSP handler, but can also be
  +  used as a general purpose templating tool.
  +
  +  When a file is used as the source, the code object resulting from
  +  the specified file is stored in a memory cache keyed on file name
  +  and file modification time. The cache is global to the Python
  +  interpreter. Therefore, unless the file modification time changes,
  +  the file is parsed and resulting code is compiled only once per
  +  interpreter.
  +
  +  The cache is limited to 512 pages, which depending on the size of
  +  the pages could potentially occupy a significant amount of
  +  memory. If memory is of concern, then you can switch to dbm file
  +  caching. Our simple tests showed only 20\% slower performance using
  +  bsd db. You will need to check which implementation \module{anydbm}
  +  defaults to on your system as some dbm libraries impose a limit on
  +  the size of the entry making them unsuitable. Dbm caching can be
  +  enabled via \code{PSPDbmCache} Python option, e.g.:
  +
  +\begin{verbatim}
  +PythonOption PSPDbmCache ``/tmp/pspcache.dbm''
  +\end{verbatim}
  +  Note that the dbm cache file is not deleted when the server
  +  restarts.
  +
  +  Unlike with files, the code objects resulting from a string are
  +  cached in memory only. There is no option to cache in a dbm file at
  +  this time.
  +
  +  \begin{methoddesc}[PSP]{run}{\optional{vars}}
  +    This method will execute the code (produced at object
  +    initialization time by parsing and compiling the PSP
  +    source). Optional argument \var{vars} is a dictionary keyed by
  +    strings that will be passed in as global variables.
  +
  +    Additionally, the PSP code will be given global variables
  +    \code{req}, \code{psp}, \code{session} and \code{form}. A session
  +    will be created and assigned to \code{session} variable only if
  +    \code{session} is referenced in the code (the PSP handler examines
  +    \code{co_names} of the code object to make that
  +    determination). Remember that a mere mention of \code{session}
  +    will generate cookies and turn on session locking, which may or
  +    may not be what you want. Similarly, a mod_python
  +    \class{FieldStorage} object will be instantiated if \code{form} is
  +    referenced in the code.
  +
  +    The object passed in \code{psp} is an instance of
  +    \class{PSPInstance}.
  +
  +  \end{methoddesc}
  +
  +  \begin{methoddesc}[PSP]{display_code}{}
  +    Returns an HTML-formatted string representing a side-by-side
  +    listing of the original PSP code and resulting Python code
  +    produced by the PSP parser. 
  +  \end{methoddesc}
  +
  +  Here is an example of how \class{PSP} can be used as a templating
  +  mechanism:
  +  
  +  The template file:
  +  \begin{verbatim}
  +<html>
  +  <!-- This is a simple psp template called template.html -->
  +  <h1>Hello, <%=what%>!</h1>
  +</html>
  +  \end{verbatim}
  +  The handler code:
  +  \begin{verbatim}
  +from mod_python import apache, psp
  +
  +def handler(req):
  +    template = psp.PSP(req, filename='template.html')
  +    template.run({'what':'world'})
  +    return apache.OK
  +  \end{verbatim}
  +
  +\end{classdesc}
  +
  +\begin{classdesc}{PSPInstance}{}
  +  An object of this class is passed as a global variable \code{psp} to
  +  the PSP code. Objects of this class are instantiated internally and
  +  the interface to \method{__init__} is purposely undocumented.
  +
  +  \begin{methoddesc}[PSPInstance]{set_error_page}{filename}
  +    Used to set a psp page to be processed when an exception
  +    occurs. If the path is absolute, it will be appended to document
  +    root, otherwise the file is assumed to exist in the same directory
  +    as the current page. The error page will receive one additional
  +    variable, \code{exception}, which is a 3-tuple returned by
  +    \code{sys.exc_info()}.
  +  \end{methoddesc}
  +
  +  \begin{methoddesc}[PSPInstance]{apply_data}{object\optional{, **kw}}
  +    This method will call the callable object \var{object}, passing form
  +    data as keyword arguments, and return the result.
  +  \end{methoddesc}
  +
  +  \begin{methoddesc}[PSPInstance]{redirect}{location\optional{, permanent=0}}
  +    This method will redirect the browser to location
  +    \var{location}. If \var{permanent} is true, then
  +    \constant{MOVED_PERMANENTLY} will be sent (as opposed to
  +    \constant{MOVED_TEMPORARILY}).
  +
  +    \begin{notice}
  +      Redirection can only happen before any data is sent to the
  +      client, therefore the Python code block calling this method must
  +      be at the very beginning of the page. Otherwise an
  +      \exception{IOError} exception will be raised.
  +    \end{notice}
  +
  +    Example:
  +    \begin{verbatim}
  +<%
  +
  +# note that the '<' above is the first byte of the page!
  +psp.redirect('http://www.modpython.org')
  +%>
  +    \end{verbatim}
  +  \end{methoddesc}
  +
  +\end{classdesc}
  +
  +Additionally, the \module{psp} module provides the following low level
  +functions:
   
   \begin{funcdesc}{parse}{filename\optional{, dir}}
   
  @@ -2131,6 +2257,3 @@
     of resulting Python code.
   
   \end{funcdesc}
  -
  -
  -
  
  
  
  1.25      +2 -68     httpd-python/Doc/modpython6.tex
  
  Index: modpython6.tex
  ===================================================================
  RCS file: /home/cvs/httpd-python/Doc/modpython6.tex,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- modpython6.tex	22 Aug 2003 02:22:44 -0000	1.24
  +++ modpython6.tex	8 Sep 2003 19:31:50 -0000	1.25
  @@ -269,8 +269,7 @@
   \index{PSP}
   
   PSP handler is a handler that processes documents using the
  -\code{mod_python._psp} module. For more details on the PSP syntax, see
  -Section \ref{pyapi-psp}.
  +\class{PSP} class in \code{mod_python.psp} module.
   
   To use it, simply add this to your httpd configuration: 
   
  @@ -279,57 +278,7 @@
     PythonHandler mod_python.psp
   \end{verbatim}
   
  -The PSP code will be given local variables \code{req}, \code{psp},
  -\code{session} and \code{form}. A session will be created and assigned
  -to \code{session} variable only if \code{session} is referenced in the
  -code (the PSP handler examines \code{co_names} of the code object to
  -make that determination). Remember that a mere mention of
  -\code{session} will generate cookies and turn on session locking,
  -which may or may not be what you want. Similarly, a mod_python
  -\class{FieldStorage} object will be instantiated if \code{form} is
  -referenced in the code.
  -
  -The object passed in \code{psp} is an instance of \class{PSPInstance}.
  -
  -\begin{classdesc}{PSPInstance}{}
  -  \begin{methoddesc}[PSPInstance]{set_error_page}{filename}
  -    Used to set a psp page to be processed when an exception
  -    occurs. If the path is absolute, it will be appended to document
  -    root, otherwise the file is assumed to exist in the same directory
  -    as the current page. The error page will receive one additional
  -    variable, \code{exception}, which is a 3-tuple returned by
  -    \code{sys.exc_info()}.
  -  \end{methoddesc}
  -
  -  \begin{methoddesc}[PSPInstance]{apply_data}{object\optional{, **kw}}
  -    This method will call the callable object \var{object}, passing form
  -    data as keyword arguments, and return the result.
  -  \end{methoddesc}
  -
  -  \begin{methoddesc}[PSPInstance]{redirect}{location\optional{, permanent=0}}
  -    This method will redirect the browser to location
  -    \var{location}. If \var{permanent} is true, then
  -    \constant{MOVED_PERMANENTLY} will be sent (as opposed to
  -    \constant{MOVED_TEMPORARILY}).
  -
  -    \begin{notice}
  -      Redirection can only happen before any data is sent to the
  -      client, therefore the Python code block calling this method must
  -      be at the very beginning of the page. Otherwise an
  -      \exception{IOError} exception will be raised.
  -    \end{notice}
  -
  -    Example:
  -    \begin{verbatim}
  -<\%
  -
  -# note that the '<' above is the first byte of the page!
  -psp.redirect('http://www.modpython.org')
  -\%>
  -    \end{verbatim}
  -  \end{methoddesc}
  -
  -\end{classdesc}
  +For more details on the PSP syntax, see Section \ref{pyapi-psp}.
   
   If \code{PythonDebug} server configuration is \code{On}, then by
   appending an underscore (\samp{_}) to the end of the url you can get a
  @@ -341,21 +290,6 @@
   Leaving debug on in a production environment will allow remote users
   to display source code of your PSP pages!
   \end{notice}
  -
  -By default, compiled PSP pages are cached in memory. The cache is
  -limited to 512 pages, which depending on the size of the pages could
  -potentially occupy a lot of memory. If memory is of concern, then you
  -can switch to dbm file caching. Our simple tests showed only 20\%
  -slower performance using bsd db. You will need to check which
  -implementation \module{anydbm} defaults to on your system as some dbm
  -libraries impose a limit on the size of the entry making them
  -unsuitable. Dbm caching can be enabled via \code{PSPDbmCache} Python
  -option, e.g.:
  -
  -\begin{verbatim}
  -PythonOption PSPDbmCache "/tmp/pspcache.dbm"
  -\end{verbatim}
  -Note that the dbm cache file is not deleted when the server restarts.
   
   \section{CGI Handler\label{hand-cgi}}
   
  
  
  
  1.78      +5 -9      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.77
  retrieving revision 1.78
  diff -u -r1.77 -r1.78
  --- apache.py	5 Sep 2003 15:04:43 -0000	1.77
  +++ apache.py	8 Sep 2003 19:31:50 -0000	1.78
  @@ -461,15 +461,11 @@
           file = module.__dict__.get("__file__")
   
           # the "and not" part of this condition is to prevent execution
  -        # of arbitrary already imported modules, such as os. the
  -        # not-so-obvious filter(lambda...) call means:
  -        # return entries from path (which is a list) which fully match at
  -        # beginning the dirname of file. E.g. if file is /a/b/c/d.py, a path
  -        # of /a/b/c will pass, because the file is below the /a/b/c path, but
  -        # path of /a/b/c/g will not pass.
  +        # of arbitrary already imported modules, such as os. The
  +        # reason we use startswith as opposed to exact match is that
  +        # modules inside packages are actually in subdirectories.
   
  -        if (not file or path and not
  -            filter(lambda a: os.path.dirname(file).find(a) == 0, path)):
  +        if not file or (path and not filter(file.startswith, path)):
               # there is a script by this name already imported, but it's in
               # a different directory, therefore it's a different script
               mtime, oldtime = 0, -1
  
  
  
  1.23      +2 -2      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.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- psp.py	5 Sep 2003 15:04:44 -0000	1.22
  +++ psp.py	8 Sep 2003 19:31:50 -0000	1.23
  @@ -75,7 +75,7 @@
   def path_split(filename):
   
       dir, fname = os.path.split(filename)
  -    if os.name == "nt":
  +    if sys.platform.startswith("win"):
           dir += "\\"
       else:
           dir += "/"
  
  
  
  1.100     +11 -5     httpd-python/src/mod_python.c
  
  Index: mod_python.c
  ===================================================================
  RCS file: /home/cvs/httpd-python/src/mod_python.c,v
  retrieving revision 1.99
  retrieving revision 1.100
  diff -u -r1.99 -r1.100
  --- mod_python.c	21 Aug 2003 18:25:12 -0000	1.99
  +++ mod_python.c	8 Sep 2003 19:31:50 -0000	1.100
  @@ -329,8 +329,11 @@
       int n;
   
       /* figre out maximum possible concurrent connections */
  +    /* MAX_DAEMON_USED seems to account for MaxClients, as opposed to
  +       MAX_DAEMONS, which is ServerLimit
  +    */
       ap_mpm_query(AP_MPMQ_MAX_THREADS, &max_threads);
  -    ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_procs);
  +    ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_procs);
       max_clients = (((!max_threads) ? 1 : max_threads) *
   		   ((!max_procs) ? 1 : max_procs));
       locks = max_clients; /* XXX this is completely out of the blue */
  @@ -362,8 +365,8 @@
   	    ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
   			 "mod_python: Failed to create global mutex %d of %d (%s).",
   			 n, locks, (!fname) ? "<null>" : fname);
  -	    if (n > 0) {
  -		/* we were able to crate at least one, so lets just print a 
  +	    if (n > 1) {
  +		/* we were able to crate at least two, so lets just print a 
   		   warning/hint and proceed
   		*/
   		ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
  @@ -372,7 +375,10 @@
   		ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
   			     "mod_python: Hint: On Linux, the problem may be the number of "
   			     "available semaphores, check 'sysctl kernel.sem'");
  -		glb->nlocks = n;
  +		/* now free one lock so that if there is a module that wants a lock, it
  +		   will be ok */
  +		apr_global_mutex_destroy(mutex[n-1]);
  +		glb->nlocks = n-1;
   		break;
   		
   	    }