[Zope-Checkins]
SVN: Zope/branches/zope33-port/lib/python/ZPublisher/
Make request.debug be backward-compatible with old Zope 2 code as
Philipp von Weitershausen
philikon at philikon.de
Tue May 2 04:22:57 EDT 2006
Log message for revision 67815:
Make request.debug be backward-compatible with old Zope 2 code as
suggested by Florent in http://mail.zope.org/pipermail/zope-dev/2006-April/027424.html.
request.debug will resolve to a form variable for all code except
code that lies in a 'zope.*' package. Eventually, we should deprecate
form variable access through getattr. I'm leaving a big comment to remind
us of that.
Changed:
U Zope/branches/zope33-port/lib/python/ZPublisher/HTTPRequest.py
U Zope/branches/zope33-port/lib/python/ZPublisher/tests/testHTTPRequest.py
-=-
Modified: Zope/branches/zope33-port/lib/python/ZPublisher/HTTPRequest.py
===================================================================
--- Zope/branches/zope33-port/lib/python/ZPublisher/HTTPRequest.py 2006-05-02 06:30:58 UTC (rev 67814)
+++ Zope/branches/zope33-port/lib/python/ZPublisher/HTTPRequest.py 2006-05-02 08:22:56 UTC (rev 67815)
@@ -13,7 +13,7 @@
__version__='$Revision: 1.96 $'[11:-2]
-import re, sys, os, time, random, codecs
+import re, sys, os, time, random, codecs, inspect
from types import StringType, UnicodeType
from BaseRequest import BaseRequest
from HTTPResponse import HTTPResponse
@@ -258,13 +258,13 @@
have_env=environ.has_key
get_env=environ.get
self.response=response
- other=self.other={'RESPONSE': response,
- 'debug': DebugFlags()}
+ other=self.other={'RESPONSE': response}
self.form={}
self.taintedform={}
self.steps=[]
self._steps=[]
self._lazies={}
+ self._debug = DebugFlags()
if environ.has_key('REMOTE_ADDR'):
@@ -1216,7 +1216,18 @@
raise KeyError, key
return v
+ # Using the getattr protocol to retrieve form values and similar
+ # is discouraged and is likely to be deprecated in the future.
+ # request.get(key) or request[key] should be used instead
def __getattr__(self, key, default=_marker, returnTaints=0):
+ # ugly hack to make request.debug work for Zope 3 code (the
+ # ZPT engine, to be exact) while retaining request.debug
+ # functionality for all other code
+ if key == 'debug':
+ lastframe = inspect.currentframe().f_back
+ if lastframe.f_globals['__name__'].startswith('zope.'):
+ return self._debug
+
v = self.get(key, default, returnTaints=returnTaints)
if v is _marker:
raise AttributeError, key
Modified: Zope/branches/zope33-port/lib/python/ZPublisher/tests/testHTTPRequest.py
===================================================================
--- Zope/branches/zope33-port/lib/python/ZPublisher/tests/testHTTPRequest.py 2006-05-02 06:30:58 UTC (rev 67814)
+++ Zope/branches/zope33-port/lib/python/ZPublisher/tests/testHTTPRequest.py 2006-05-02 08:22:56 UTC (rev 67815)
@@ -701,6 +701,45 @@
f.seek(0)
self.assertEqual(f.xreadlines(),f)
+ def testDebug(self):
+ TEST_ENVIRON = {
+ 'REQUEST_METHOD': 'GET',
+ 'SERVER_NAME': 'localhost',
+ 'SERVER_PORT': '80',
+ }
+ from StringIO import StringIO
+ from ZPublisher.HTTPRequest import HTTPRequest
+ s = StringIO('')
+
+ # accessing request.debug from non-Zope3 code will raise an
+ # AttributeError
+ env = TEST_ENVIRON.copy()
+ request = HTTPRequest(s, env, None)
+ request.processInputs()
+ self.assertRaises(AttributeError, getattr, request, 'debug')
+
+ # or it will actually yield a 'debug' form variable if it
+ # exists
+ env = TEST_ENVIRON.copy()
+ env['QUERY_STRING'] = 'debug=1'
+ request = HTTPRequest(s, env, None)
+ request.processInputs()
+ self.assertEqual(request.debug, '1')
+
+ # if we access request.debug from a Zope 3 package, however,
+ # we will see the DebugFlags instance
+ def getDebug(request):
+ return request.debug
+ # make a forged copy of getDebug that looks as if its module
+ # was a Zope 3 package
+ z3globals = globals().copy()
+ z3globals['__name__'] = 'zope.apackage'
+ import new
+ getDebugFromZope3 = new.function(getDebug.func_code, z3globals)
+ from zope.publisher.base import DebugFlags
+ self.assertEqual(getDebug(request), '1')
+ self.assert_(isinstance(getDebugFromZope3(request), DebugFlags))
+
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(AuthCredentialsTestsa, 'test'))
More information about the Zope-Checkins
mailing list