You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mod_python-dev@quetz.apache.org by "Dwayne C. Litzenberger" <dl...@dlitz.net> on 2004/05/31 09:08:53 UTC

[PATCH] Missing feature: psp.exit()

Mod_python's PSP interpreter currently leaves no way for PSP pages to exit 
prematurely without error (except for sys.exit(), which is generally not a 
good idea).  This especially causes problems when you want a script to 
check a few conditions and crap out if something goes wrong (such as
when the user requests data which doesn't exist or is inaccessible for 
some reason).

The patch below (against Debian version 3.1.3-1, but I see no reason why it 
wouldn't apply fine to CVS) adds an exit() function to PSPInterface, and a 
corresponding PSPExit exception.  Its usage is similar to sys.exit, except 
it doesn't take an optional parameter.

If there are any problems with the patch (there shouldn't be any), let me 
know and I'll change it.  Otherwise, someone who has CVS write access can 
feel free to apply it.

Thanks,

	Dwayne

== BEGIN PATCH =================================================
diff -ru orig/libapache2-mod-python-3.1.3/Doc/modpython4.tex libapache2-mod-python-3.1.3/Doc/modpython4.tex
--- orig/libapache2-mod-python-3.1.3/Doc/modpython4.tex	2004-02-05 06:22:42.000000000 -0600
+++ libapache2-mod-python-3.1.3/Doc/modpython4.tex	2004-05-31 00:41:04.000000000 -0600
@@ -2291,8 +2291,22 @@
     \end{verbatim}
   \end{methoddesc}
 
+  \begin{methoddesc}{exit}{}
+    This method causes processing of the current PSP page to terminate without
+    error.  It is analogous to the \code{sys.exit} function, except it takes
+    no arguments and does not cause the Python interpreter to exit.
+  \end{methoddesc}
+
 \end{classdesc}
 
+\begin{excclassdesc}{PSPExit}{}
+  This exception is raised by the \class{psp.exit} function, and is treated
+  as a non-error by the PSP interpreter.  When it is not handled, it will
+  cause processing of the current PSP page to stop.  As \method{psp.exit} is
+  analogous to \code{sys.exit}, \exception{PSPExit} is analogous
+  to the \exception{SystemExit}.
+\end{excclassdesc}
+
 Additionally, the \module{psp} module provides the following low level
 functions:
 
diff -ru orig/libapache2-mod-python-3.1.3/lib/python/mod_python/psp.py libapache2-mod-python-3.1.3/lib/python/mod_python/psp.py
--- orig/libapache2-mod-python-3.1.3/lib/python/mod_python/psp.py	2004-02-16 13:47:27.000000000 -0600
+++ libapache2-mod-python-3.1.3/lib/python/mod_python/psp.py	2004-05-31 00:34:49.000000000 -0600
@@ -55,6 +55,12 @@
 
     return new.code(*marshal.loads(s))
 
+class PSPExit(Exception):
+    """
+    Prematurely exit a PSP page.
+    """
+    
+
 class PSPInterface:
 
     def __init__(self, req, filename, form):
@@ -83,6 +89,10 @@
 
         util.redirect(self.req, location, permanent)
 
+    def exit(self):
+        
+        raise PSPExit
+
 class PSP:
 
     code = None
@@ -205,7 +215,10 @@
             global_scope.update(self.vars) # passed in __init__()
             global_scope.update(vars)      # passed in run()
             try:
-                exec code in global_scope
+                try:
+                    exec code in global_scope
+                except PSPExit:
+                    pass
                 req.flush()
                 
                 # the mere instantiation of a session changes it
== END PATCH ===================================================

-- 
Dwayne C. Litzenberger <dl...@dlitz.net>

This message contains an OpenPGP/MIME signature, which can be used to verify 
its authenticity.  If the message itself appears as an attachment, you are 
probably using using a broken mail program, such as Microsoft Outlook Express.