[Zope-Checkins] SVN: Zope/branches/elro-remove-request-container/ Remove RequestContainer and make app.REQUEST a property looking up the globalrequest.
Laurence Rowe
l at lrowe.co.uk
Thu Jan 20 10:40:54 EST 2011
Log message for revision 119792:
Remove RequestContainer and make app.REQUEST a property looking up the globalrequest.
This mostly works, but causes two problems:
1. Some very old request using Testing.makerequest are unable to clean up the globalrequest after themselves, leading to failures because of lack of test isolation.
2. The Application object itself is no longer aq wrapped, so does not have an aq_explicit attribute.
(Part of http://wiki.zope.org/zope2/SetParentAndNameInOFS)
Changed:
U Zope/branches/elro-remove-request-container/setup.py
U Zope/branches/elro-remove-request-container/sources.cfg
U Zope/branches/elro-remove-request-container/src/OFS/Application.py
U Zope/branches/elro-remove-request-container/src/Products/Five/browser/tests/aqlegacy_ftest.txt
U Zope/branches/elro-remove-request-container/src/Products/Five/component/makesite.txt
U Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/base.py
U Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/connections.py
U Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/functional.py
U Zope/branches/elro-remove-request-container/src/Testing/makerequest.py
U Zope/branches/elro-remove-request-container/src/Testing/tests/test_makerequest.py
U Zope/branches/elro-remove-request-container/src/ZPublisher/BaseRequest.py
U Zope/branches/elro-remove-request-container/src/ZPublisher/Publish.py
U Zope/branches/elro-remove-request-container/versions.cfg
-=-
Modified: Zope/branches/elro-remove-request-container/setup.py
===================================================================
--- Zope/branches/elro-remove-request-container/setup.py 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/setup.py 2011-01-20 15:40:54 UTC (rev 119792)
@@ -82,6 +82,7 @@
'zope.deferredimport',
'zope.event',
'zope.exceptions',
+ 'zope.globalrequest',
'zope.i18n [zcml]',
'zope.i18nmessageid',
'zope.interface',
Modified: Zope/branches/elro-remove-request-container/sources.cfg
===================================================================
--- Zope/branches/elro-remove-request-container/sources.cfg 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/sources.cfg 2011-01-20 15:40:54 UTC (rev 119792)
@@ -17,3 +17,4 @@
zExceptions = svn ^/zExceptions/trunk
ZODB3 = svn ^/ZODB/trunk
ZopeUndo = svn ^/ZopeUndo/trunk
+zope.globalrequest = svn ^/zope.globalrequest/trunk
Modified: Zope/branches/elro-remove-request-container/src/OFS/Application.py
===================================================================
--- Zope/branches/elro-remove-request-container/src/OFS/Application.py 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/src/OFS/Application.py 2011-01-20 15:40:54 UTC (rev 119792)
@@ -39,6 +39,7 @@
from webdav.NullResource import NullResource
from zExceptions import Redirect as RedirectException, Forbidden
+from zope.globalrequest import getRequest
from zope.interface import implements
import Folder
@@ -225,6 +226,25 @@
reg = {}
return reg.get(flag)
+ @property
+ def REQUEST(self):
+ # Return the current request
+ request = self.__dict__.get('REQUEST', None)
+ if request is None:
+ request = getRequest()
+ if request is None:
+ raise AttributeError('REQUEST')
+ return request
+
+ @REQUEST.setter
+ def REQUEST(self, value):
+ # Set the current request as an attribute (used in tests)
+ self.__dict__['REQUEST'] = value
+
+ @REQUEST.deleter
+ def REQUEST(self):
+ del self.__dict__['REQUEST']
+
InitializeClass(Application)
Modified: Zope/branches/elro-remove-request-container/src/Products/Five/browser/tests/aqlegacy_ftest.txt
===================================================================
--- Zope/branches/elro-remove-request-container/src/Products/Five/browser/tests/aqlegacy_ftest.txt 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/src/Products/Five/browser/tests/aqlegacy_ftest.txt 2011-01-20 15:40:54 UTC (rev 119792)
@@ -23,8 +23,7 @@
>>> print browser.contents
[<Products.Five.metaclass.LegacyAttributes object at ...>,
<Folder at /test_folder_1_>,
- <Application at >,
- <ZPublisher.BaseRequest.RequestContainer object at ...>]
+ <Application at >]
The same goes for browser views that just mix in Acquisition.Explicit:
@@ -32,8 +31,7 @@
>>> print browser.contents
[<Products.Five.metaclass.ExplicitLegacyAttributes object at ...>,
<Folder at /test_folder_1_>,
- <Application at >,
- <ZPublisher.BaseRequest.RequestContainer object at ...>]
+ <Application at >]
Let's do some more manual tests with the view object. But first we
must get it:
Modified: Zope/branches/elro-remove-request-container/src/Products/Five/component/makesite.txt
===================================================================
--- Zope/branches/elro-remove-request-container/src/Products/Five/component/makesite.txt 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/src/Products/Five/component/makesite.txt 2011-01-20 15:40:54 UTC (rev 119792)
@@ -32,6 +32,7 @@
>>> from Testing.testbrowser import Browser
>>> browser = Browser()
+ >>> browser.handleErrors = False
>>> browser.addHeader('Authorization', 'Basic manager:r00t')
Let's add a folder:
Modified: Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/base.py
===================================================================
--- Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/base.py 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/base.py 2011-01-20 15:40:54 UTC (rev 119792)
@@ -21,6 +21,7 @@
import connections
import layer
+from zope.globalrequest import setRequest, clearRequest
from zope.interface import implements
from AccessControl.SecurityManagement import noSecurityManager
@@ -120,6 +121,7 @@
self.beforeClose()
self._close()
self.logout()
+ clearRequest()
self.afterClear()
def _close(self):
Modified: Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/connections.py
===================================================================
--- Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/connections.py 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/connections.py 2011-01-20 15:40:54 UTC (rev 119792)
@@ -25,10 +25,12 @@
self._conns = []
def register(self, conn):
- self._conns.append(conn)
+ connset = self._getConnSet(conn)
+ self._conns.append(connset)
def contains(self, conn):
- return conn in self._conns
+ connset = self._getConnSet(conn)
+ return connset in self._conns
def __len__(self):
return len(self._conns)
@@ -37,23 +39,26 @@
return len(self)
def close(self, conn):
+ connset = self._getConnSet(conn)
if self.contains(conn):
- self._conns.remove(conn)
- self._do_close(conn)
+ self._conns.remove(connset)
+ self._do_close(connset)
def closeAll(self):
- for conn in self._conns:
- self._do_close(conn)
+ for connset in self._conns:
+ self._do_close(connset)
self._conns = []
- def _do_close(self, conn):
- if hasattr(conn, 'close'):
+ def _do_close(self, connset):
+ for conn in connset:
conn.close()
+
+ def _getConnSet(self, conn):
+ if hasattr(conn, '_p_jar'):
+ return (conn.REQUEST, conn._p_jar)
else:
- conn.REQUEST.close()
- conn._p_jar.close()
+ return (conn,)
-
registry = ConnectionRegistry()
register = registry.register
contains = registry.contains
Modified: Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/functional.py
===================================================================
--- Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/functional.py 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/src/Testing/ZopeTestCase/functional.py 2011-01-20 15:40:54 UTC (rev 119792)
@@ -31,14 +31,16 @@
from AccessControl.SecurityManagement import setSecurityManager
from zope.site.hooks import getSite
from zope.site.hooks import setSite
+ from zope.globalrequest import getRequest, setRequest
def wrapped_func(*args, **kw):
- sm, site = getSecurityManager(), getSite()
+ sm, site, request = getSecurityManager(), getSite(), getRequest()
try:
return func(*args, **kw)
finally:
setSecurityManager(sm)
setSite(site)
+ setRequest(request)
return wrapped_func
Modified: Zope/branches/elro-remove-request-container/src/Testing/makerequest.py
===================================================================
--- Zope/branches/elro-remove-request-container/src/Testing/makerequest.py 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/src/Testing/makerequest.py 2011-01-20 15:40:54 UTC (rev 119792)
@@ -19,7 +19,7 @@
from sys import stdin, stdout
from ZPublisher.HTTPRequest import HTTPRequest
from ZPublisher.HTTPResponse import HTTPResponse
-from ZPublisher.BaseRequest import RequestContainer
+from zope.globalrequest import setRequest
def makerequest(app, stdout=stdout, environ=None):
"""
@@ -61,6 +61,6 @@
# Zope3-style view look-ups.
from zope.publisher.browser import setDefaultSkin
setDefaultSkin(req)
+ setRequest(req)
- requestcontainer = RequestContainer(REQUEST = req)
- return app.__of__(requestcontainer)
+ return app
Modified: Zope/branches/elro-remove-request-container/src/Testing/tests/test_makerequest.py
===================================================================
--- Zope/branches/elro-remove-request-container/src/Testing/tests/test_makerequest.py 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/src/Testing/tests/test_makerequest.py 2011-01-20 15:40:54 UTC (rev 119792)
@@ -17,24 +17,29 @@
from Acquisition import Implicit
from Testing.makerequest import makerequest
-from OFS.SimpleItem import SimpleItem
+from OFS.Application import Application
+from zope.globalrequest import getRequest, clearRequest
class MakerequestTests(unittest.TestCase):
+ def tearDown(self):
+ clearRequest()
+
def test_makerequest(self):
# The argument must support acquisition.
- self.assertRaises(AttributeError, makerequest, object())
+ #self.assertRaises(AttributeError, makerequest, object())
# After the call, it will have a REQUEST attribute.
- item = Implicit()
- self.assertFalse(hasattr(item, 'REQUEST'))
- item = makerequest(item)
- self.assertTrue(hasattr(item, 'REQUEST'))
+ app = Application()
+ self.assertFalse(hasattr(app, 'REQUEST'))
+ app = makerequest(app)
+ self.failUnless(getRequest() is not None)
+ self.assertTrue(hasattr(app, 'REQUEST'))
def test_dont_break_getPhysicalPath(self):
# see http://www.zope.org/Collectors/Zope/2057. If you want
# to call getPhysicalPath() on the wrapped object, be sure
# that it provides a non-recursive getPhysicalPath().
- class FakeRoot(SimpleItem):
+ class FakeRoot(Application):
def getPhysicalPath(self):
return ('foo',)
item = FakeRoot()
@@ -45,8 +50,8 @@
# You can pass a stdout arg and it's used by the response.
import cStringIO
out = cStringIO.StringIO()
- item = makerequest(SimpleItem(), stdout=out)
- item.REQUEST.RESPONSE.write('aaa')
+ app = makerequest(Application(), stdout=out)
+ app.REQUEST.RESPONSE.write('aaa')
out.seek(0)
written = out.read()
self.assertTrue(written.startswith('Status: 200 OK\r\n'))
@@ -55,8 +60,8 @@
def test_environ(self):
# You can pass an environ argument to use in the request.
environ = {'foofoo': 'barbar'}
- item = makerequest(SimpleItem(), environ=environ)
- self.assertEqual(item.REQUEST.environ['foofoo'], 'barbar')
+ app = makerequest(Application(), environ=environ)
+ self.assertEqual(app.REQUEST.environ['foofoo'], 'barbar')
def test_suite():
suite = unittest.TestSuite()
Modified: Zope/branches/elro-remove-request-container/src/ZPublisher/BaseRequest.py
===================================================================
--- Zope/branches/elro-remove-request-container/src/ZPublisher/BaseRequest.py 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/src/ZPublisher/BaseRequest.py 2011-01-20 15:40:54 UTC (rev 119792)
@@ -41,23 +41,6 @@
return urllib_quote(text, '/+@')
try:
- from ExtensionClass import Base
- from ZPublisher.Converters import type_converters
- class RequestContainer(Base):
- __roles__=None
- def __init__(self,**kw):
- for k,v in kw.items(): self.__dict__[k]=v
-
- def manage_property_types(self):
- return type_converters.keys()
-
-except ImportError:
- class RequestContainer:
- __roles__=None
- def __init__(self,**kw):
- for k,v in kw.items(): self.__dict__[k]=v
-
-try:
from AccessControl.ZopeSecurityPolicy import getRoles
except ImportError:
def getRoles(container, name, value, default):
@@ -413,10 +396,6 @@
return response.forbiddenError(self['URL'])
# Traverse the URL to find the object:
- if hasattr(object, '__of__'):
- # Try to bind the top-level object to the request
- # This is how you get 'self.REQUEST'
- object=object.__of__(RequestContainer(REQUEST=request))
parents.append(object)
steps=self.steps
Modified: Zope/branches/elro-remove-request-container/src/ZPublisher/Publish.py
===================================================================
--- Zope/branches/elro-remove-request-container/src/ZPublisher/Publish.py 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/src/ZPublisher/Publish.py 2011-01-20 15:40:54 UTC (rev 119792)
@@ -24,6 +24,7 @@
from zope.publisher.skinnable import setDefaultSkin
from zope.security.management import newInteraction, endInteraction
from zope.event import notify
+from zope.globalrequest import setRequest, clearRequest
from pubevents import PubStart, PubSuccess, PubFailure, \
PubBeforeCommit, PubAfterTraversal, PubBeforeAbort
@@ -227,13 +228,14 @@
if request is None:
request=Request(stdin, environ, response)
-
+
# make sure that the request we hand over has the
# default layer/skin set on it; subsequent code that
# wants to look up views will likely depend on it
if ISkinnable.providedBy(request):
setDefaultSkin(request)
+ setRequest(request)
response = publish(request, module_name, after_list, debug=debug)
except (SystemExit, ImportError):
# XXX: Rendered ImportErrors were never caught here because they
@@ -261,6 +263,7 @@
if after_list[0] is not None: after_list[0]()
finally:
+ clearRequest()
if request is not None: request.close()
if must_die:
Modified: Zope/branches/elro-remove-request-container/versions.cfg
===================================================================
--- Zope/branches/elro-remove-request-container/versions.cfg 2011-01-20 15:36:14 UTC (rev 119791)
+++ Zope/branches/elro-remove-request-container/versions.cfg 2011-01-20 15:40:54 UTC (rev 119792)
@@ -38,4 +38,5 @@
repoze.retry = 1.0
repoze.tm2 = 1.0a5
repoze.who = 2.0a2
+zope.globalrequest = 1.0
zope.testbrowser = 3.10.3
More information about the Zope-Checkins
mailing list