Mercurial > p > roundup > code
annotate roundup/cgi/PageTemplates/PythonExpr.py @ 3896:fca0365521fc
ignore client shutdown exceptions when sending responses
patch from Ulrik Miaelsson
If the user clicks the stop button, or click another link before
the previous has finished loading, or something similar an IOError
exception will be raised which results in the admin being sent an
email.
This can understandably be pretty annoying if your users are
doing that on a regular basis. So we'll trap that exception
and ignore it.
| author | Justus Pendleton <jpend@users.sourceforge.net> |
|---|---|
| date | Tue, 11 Sep 2007 21:30:14 +0000 |
| parents | b43efe461b3e |
| children | 6e3e4f24c753 |
| rev | line source |
|---|---|
| 1049 | 1 ############################################################################## |
| 2 # | |
| 3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
4 # |
| 1049 | 5 # This software is subject to the provisions of the Zope Public License, |
| 6 # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. | |
| 7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | |
| 8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | |
| 10 # FOR A PARTICULAR PURPOSE | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
11 # |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
12 ############################################################################## |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
13 # Modified for Roundup: |
| 1049 | 14 # |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
15 # 1. more informative traceback info |
| 1049 | 16 |
| 17 """Generic Python Expression Handler | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
18 """ |
|
1071
c08b3820edd1
Adhering to ZPL
Richard Jones <richard@users.sourceforge.net>
parents:
1065
diff
changeset
|
19 |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
20 __version__='$Revision: 1.6 $'[11:-2] |
| 1049 | 21 |
| 22 from TALES import CompilerError | |
| 23 from sys import exc_info | |
| 24 | |
| 25 class getSecurityManager: | |
| 26 '''Null security manager''' | |
| 27 def validate(self, *args, **kwargs): | |
| 28 return 1 | |
| 29 addContext = removeContext = validateValue = validate | |
| 30 | |
| 31 class PythonExpr: | |
| 32 def __init__(self, name, expr, engine): | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
33 self.expr = expr = expr.strip().replace('\n', ' ') |
| 1049 | 34 try: |
| 35 d = {} | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
36 exec 'def f():\n return %s\n' % expr.strip() in d |
| 1049 | 37 self._f = d['f'] |
| 38 except: | |
| 39 raise CompilerError, ('Python expression error:\n' | |
| 40 '%s: %s') % exc_info()[:2] | |
| 41 self._get_used_names() | |
| 42 | |
| 43 def _get_used_names(self): | |
| 44 self._f_varnames = vnames = [] | |
| 45 for vname in self._f.func_code.co_names: | |
| 46 if vname[0] not in '$_': | |
| 47 vnames.append(vname) | |
| 48 | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
49 def _bind_used_names(self, econtext, _marker=[]): |
| 1049 | 50 # Bind template variables |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
51 names = {'CONTEXTS': econtext.contexts} |
| 1049 | 52 vars = econtext.vars |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
53 getType = econtext.getCompiler().getTypes().get |
| 1049 | 54 for vname in self._f_varnames: |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
55 val = vars.get(vname, _marker) |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
56 if val is _marker: |
| 1049 | 57 has = val = getType(vname) |
| 58 if has: | |
| 59 val = ExprTypeProxy(vname, val, econtext) | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
60 names[vname] = val |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
61 else: |
| 1049 | 62 names[vname] = val |
| 63 return names | |
| 64 | |
| 65 def __call__(self, econtext): | |
|
1065
0f9aa62917bd
much nicer error messages when there's a templating error
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
66 __traceback_info__ = 'python expression "%s"'%self.expr |
| 1049 | 67 f = self._f |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
68 f.func_globals.update(self._bind_used_names(econtext)) |
| 1049 | 69 return f() |
| 70 | |
| 71 def __str__(self): | |
| 72 return 'Python expression "%s"' % self.expr | |
| 73 def __repr__(self): | |
| 74 return '<PythonExpr %s>' % self.expr | |
| 75 | |
| 76 class ExprTypeProxy: | |
| 77 '''Class that proxies access to an expression type handler''' | |
| 78 def __init__(self, name, handler, econtext): | |
| 79 self._name = name | |
| 80 self._handler = handler | |
| 81 self._econtext = econtext | |
| 82 def __call__(self, text): | |
| 83 return self._handler(self._name, text, | |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
84 self._econtext.getCompiler())(self._econtext) |
| 1049 | 85 |
