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 19:05:16 UTC

cvs commit: httpd-python/src requestobject.c

grisha      2003/05/30 10:05:16

  Modified:    Doc      modpython4.tex modpython6.tex
               src      requestobject.c
  Log:
  Initial stab at PSP documentation.
  
  Revision  Changes    Path
  1.32      +130 -0    httpd-python/Doc/modpython4.tex
  
  Index: modpython4.tex
  ===================================================================
  RCS file: /home/cvs/httpd-python/Doc/modpython4.tex,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- modpython4.tex	22 May 2003 20:25:07 -0000	1.31
  +++ modpython4.tex	30 May 2003 17:05:15 -0000	1.32
  @@ -1439,3 +1439,133 @@
   
   \end{funcdesc}
   
  +\section{\module{psp} -- Python Server Pages\label{pyapi-psp}}
  +\declaremodule[psp]{extension}{psp}
  +\modulesynopsis{Python Server Pages}
  +\moduleauthor{Gregory Trubetskoy}{grisha@modpython.org}
  +
  +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.
  +
  +The parser used by \module{psp} is written in C (genrated 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.
  +
  +\subsection{PSP Syntax\label{pyapi-psp-syntax}}
  +
  +Inside the document, Python \dfn{code} needs to be surrounded by "<\%"
  +and "\%>". Python \dfn{expressions} are enclosed in "<\%=" and
  +"\%>".
  +
  +Here is a primitive PSP page that demonstrated use of both code and
  +expression embedded in an HTML document:
  +
  +\begin{verbatim}
  +<html>
  +<%
  +import time
  +%>
  +Hello world, the time is: <%=time.strftime("%Y-%m-%d, %H:%M:%S")%>
  +</html>
  +\end{verbatim}
  +
  +The PSP parser would convert the above page into the following Python code:
  +
  +\begin{verbatim}
  +req.write("""<html>
  +""")
  +import time
  +req.write("""
  +Hello world, the time is: """); req.write(str(time.strftime("%Y-%m-%d, %H:%M:%S"))); req.write("""
  +</html>
  +""")
  +\end{verbatim}
  +
  +This code, when executed inside a handler would result in a page
  +displaying words "Hello world, the time is: " followed by current time.
  +
  +Python code can be used to output parts of the page conditionally or
  +in loops. Blocks are denoted from within Python code by
  +indentation. The last indentation in Python code (even if it is a
  +comment) will persist through the document until either end of
  +document or more Python code.
  +
  +Here is an example:
  +\begin{verbatim}
  +<html>
  +<%
  +for n in range(3):
  +    # This indent will persist
  +%>
  + <p>This paragraph will be 
  +repeated 3 times.</p>
  +<%
  +# This line will cause the block to end
  +%>
  +This line will only be shown once.<br>
  +</html>
  +\end{verbatim}
  +
  +The above will be converted to the following Python code:
  +
  +\begin{verbatim}
  +req.write("""<html>
  +""")
  +for n in range(3):
  +    # This indent will persist
  +    req.write("""
  + <p>This paragraph will be
  +repeated 3 times.</p>
  +""")
  +# This line will cause the block to end
  +req.write("""
  +This line will only be shown once.<br>
  +</html>
  +""")
  +\end{verbatim}
  +
  +The parser is also smart enough to figure out the indent if the last
  +line of Python ends with ":" (colon). Considering this, and that the
  +indent is reset when a newline is encountered inside <\% \%>, the
  +above page can be written as:
  +
  +\begin{verbatim}
  +<html>
  +<%
  +for n in range(3):
  +%>
  + <p>This paragraph will be 
  +repeated 3 times.</p>
  +<%
  +%>
  +This line will only be shown once.<br>
  +</html>
  +\end{verbatim}
  +
  +However, the above code can be confusing, thus having descriptive
  +comments denoting blocks is highly recommended as a good practice.
  +
  +\subsection{Functions\label{pyapi-psp-funcs}}
  +
  +\begin{funcdesc}{parse}{filename}
  +
  +This function will open file named \var{filename}, read and parse its
  +content and return a string of resulting Python code.
  +
  +\end{funcdesc}
  +
  +\begin{funcdesc}{parsestring}{string}
  +
  +This function will parse contents of \var{string} and return a string
  +of resulting Python code.
  +
  +\end{funcdesc}
  +
  +
  
  
  
  1.13      +24 -3     httpd-python/Doc/modpython6.tex
  
  Index: modpython6.tex
  ===================================================================
  RCS file: /home/cvs/httpd-python/Doc/modpython6.tex,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- modpython6.tex	22 May 2003 18:36:21 -0000	1.12
  +++ modpython6.tex	30 May 2003 17:05:16 -0000	1.13
  @@ -262,6 +262,22 @@
   using the Publisher handler and should use
   \class{Request.form} instead.
   
  +
  +\section{PSP Handler\label{hand-psp}}
  +
  +\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}.
  +
  +To use it, simply add this to your httpd configuration: 
  +
  +\begin{verbatim}
  +AddHandler python-program .psp
  +PythonHandler mod_python.psp
  +\end{verbatim}
  +
   \section{CGI Handler\label{hand-cgi}}
   
   \index{CGI}
  @@ -277,9 +293,14 @@
   be able to read/write from standard input/output with the results expected
   in a "true" CGI environment.
   
  -The handler is provided as a stepping stone for the migration of legacy
  -code away from CGI. It is not recommended that you settle on using
  -this handler as the preferred way to use mod_python for the long term.
  +The handler is provided as a stepping stone for the migration of
  +legacy code away from CGI. It is not recommended that you settle on
  +using this handler as the preferred way to use mod_python for the long
  +term. This is because the CGI environment was not intended for
  +execution within threads (e.g. requires changing of current directory
  +with is inherently not thread-safe) and therefore can only be
  +implemented in a way that defeats many of the advantages of using
  +mod_python in the first place.
   
   To use it, simply add this to your \file{.htaccess} file: 
   
  
  
  
  1.47      +11 -8     httpd-python/src/requestobject.c
  
  Index: requestobject.c
  ===================================================================
  RCS file: /home/cvs/httpd-python/src/requestobject.c,v
  retrieving revision 1.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- requestobject.c	29 May 2003 14:44:33 -0000	1.46
  +++ requestobject.c	30 May 2003 17:05:16 -0000	1.47
  @@ -793,13 +793,16 @@
       if (! PyArg_ParseTuple(args, "s#", &buff, &len))
           return NULL;  /* bad args */
   
  -    Py_BEGIN_ALLOW_THREADS
  -    ap_rwrite(buff, len, self->request_rec);
  -    rc = ap_rflush(self->request_rec);
  -    Py_END_ALLOW_THREADS
  -    if (rc == EOF) {
  -        PyErr_SetString(PyExc_IOError, "Write failed, client closed connection.");
  -        return NULL;
  +    if (len > 0 ) {
  +
  +        Py_BEGIN_ALLOW_THREADS
  +        ap_rwrite(buff, len, self->request_rec);
  +        rc = ap_rflush(self->request_rec);
  +        Py_END_ALLOW_THREADS
  +            if (rc == EOF) {
  +                PyErr_SetString(PyExc_IOError, "Write failed, client closed connection.");
  +                return NULL;
  +            }
       }
   
       Py_INCREF(Py_None);