/usr/share/pyshared/zope/publisher/publish.py is in python-zope.publisher 3.12.6-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | ##############################################################################
#
# Copyright (c) 2001, 2002, 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Python Object Publisher -- Publish Python objects on web servers
Provide an apply-like facility that works with any mapping object
"""
import sys
from zope import component
from zope.interface import implements
from zope.publisher.interfaces import Retry, IReRaiseException
from zope.proxy import removeAllProxies
_marker = object() # Create a new marker object.
def unwrapMethod(obj):
"""obj -> (unwrapped, wrapperCount)
Unwrap 'obj' until we get to a real function, counting the number of
unwrappings.
Bail if we find a class or something we can't identify as callable.
"""
wrapperCount = 0
unwrapped = obj
for i in range(10):
bases = getattr(unwrapped, '__bases__', None)
if bases is not None:
raise TypeError("mapply() can not call class constructors")
im_func = getattr(unwrapped, 'im_func', None)
if im_func is not None:
unwrapped = im_func
wrapperCount += 1
elif getattr(unwrapped, 'func_code', None) is not None:
break
else:
unwrapped = getattr(unwrapped, '__call__' , None)
if unwrapped is None:
raise TypeError("mapply() can not call %s" % repr(obj))
else:
raise TypeError("couldn't find callable metadata, mapply() error on %s"
% repr(obj))
return unwrapped, wrapperCount
def mapply(obj, positional=(), request={}):
__traceback_info__ = obj
# we need deep access for introspection. Waaa.
unwrapped = removeAllProxies(obj)
unwrapped, wrapperCount = unwrapMethod(unwrapped)
code = unwrapped.func_code
defaults = unwrapped.func_defaults
names = code.co_varnames[wrapperCount:code.co_argcount]
nargs = len(names)
if not positional:
args = []
else:
args = list(positional)
if len(args) > nargs:
given = len(args)
if wrapperCount:
given += wrapperCount
raise TypeError('%s() takes at most %d argument%s(%d given)' % (
getattr(unwrapped, '__name__', repr(obj)),
code.co_argcount,
(code.co_argcount > 1 and 's ' or ' '),
given))
get = request.get
nrequired = len(names)
if defaults:
nrequired -= len(defaults)
for index in range(len(args), nargs):
name = names[index]
v = get(name, _marker)
if v is _marker:
if name == 'REQUEST':
v = request
elif index < nrequired:
raise TypeError('Missing argument to %s(): %s' % (
getattr(unwrapped, '__name__', repr(obj)), name))
else:
v = defaults[index - nrequired]
args.append(v)
args = tuple(args)
if __debug__:
return debug_call(obj, args)
return obj(*args)
def debug_call(obj, args):
# The presence of this function allows us to set a pdb breakpoint
return obj(*args)
def publish(request, handle_errors=True):
try: # finally to clean up to_raise and close request
to_raise = None
while True:
publication = request.publication
try:
try:
obj = None
try:
try:
request.processInputs()
publication.beforeTraversal(request)
obj = publication.getApplication(request)
obj = request.traverse(obj)
publication.afterTraversal(request, obj)
result = publication.callObject(request, obj)
response = request.response
if result is not response:
response.setResult(result)
publication.afterCall(request, obj)
except:
exc_info = sys.exc_info()
publication.handleException(
obj, request, exc_info, True)
if not handle_errors:
# Reraise only if there is no adapter
# indicating that we shouldn't
reraise = component.queryAdapter(
exc_info[1], IReRaiseException,
default=None)
if reraise is None or reraise():
raise
finally:
publication.endRequest(request, obj)
break # Successful.
except Retry, retryException:
if request.supportsRetry():
# Create a copy of the request and use it.
newrequest = request.retry()
request.close()
request = newrequest
elif handle_errors:
# Output the original exception.
publication = request.publication
publication.handleException(
obj, request,
retryException.getOriginalException(), False)
break
else:
to_raise = retryException.getOriginalException()
if to_raise is None:
# There is no original exception inside
# the Retry, so just reraise it.
raise
break
except:
# Bad exception handler or retry method.
# Re-raise after outputting the response.
if handle_errors:
request.response.internalError()
to_raise = sys.exc_info()
break
else:
raise
response = request.response
if to_raise is not None:
raise to_raise[0], to_raise[1], to_raise[2]
finally:
to_raise = None # Avoid circ. ref.
request.close() # Close database connections, etc.
# Return the request, since it might be a different object than the one
# that was passed in.
return request
class DoNotReRaiseException(object):
"""Marker adapter for exceptions that should not be re-raised"""
implements(IReRaiseException)
def __init__(self, exc):
pass
def __call__(self):
return False
|