Skip to content

Commit

Permalink
Use a context manager to explicitly open/close the ZODB connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-hof committed Jul 28, 2017
1 parent c09b561 commit 904241d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
21 changes: 12 additions & 9 deletions src/Testing/ZopeTestCase/sandbox.py
Expand Up @@ -13,9 +13,10 @@
"""Support for ZODB sandboxes in ZTC
"""

import contextlib
import transaction

from App.ZApplication import ZApplicationWrapper
import ZPublisher.WSGIPublisher
from Testing.makerequest import makerequest
from Testing.ZopeTestCase import connections
from Testing.ZopeTestCase import ZopeLite as Zope2
Expand Down Expand Up @@ -62,15 +63,17 @@ def app(self):
return self._app


def __bobo_traverse__(self, REQUEST=None, name=None):
'''Makes ZPublisher.publish() use the current app object.'''
@contextlib.contextmanager
def load_app(module_info):
"""Let the Publisher use the current app object."""
app = AppZapper().app()
if app is not None:
return app
return self.__old_bobo_traverse__(REQUEST, name)
yield app, module_info[1], module_info[2]
else:
with ZPublisher.WSGIPublisher.__old_load_app__(module_info) as ret:
yield ret


if not hasattr(ZApplicationWrapper, '__old_bobo_traverse__'):
ZApplicationWrapper.__old_bobo_traverse__ = (
ZApplicationWrapper.__bobo_traverse__)
ZApplicationWrapper.__bobo_traverse__ = __bobo_traverse__
if not hasattr(ZPublisher.WSGIPublisher, '__old_load_app__'):
ZPublisher.WSGIPublisher.__old_load_app__ = ZPublisher.WSGIPublisher.load_app
ZPublisher.WSGIPublisher.load_app = load_app
20 changes: 18 additions & 2 deletions src/ZPublisher/WSGIPublisher.py
Expand Up @@ -213,6 +213,18 @@ def _publish_response(request, response, module_info, _publish=publish):
return response


@contextmanager
def load_app(module_info):
app_wrapper, realm, debug_mode = module_info
# Loads the 'OFS.Application' from ZODB.
app = app_wrapper()

try:
yield (app, realm, debug_mode)
finally:
app._p_jar.close()


def publish_module(environ, start_response,
_publish=publish, # only for testing
_response=None,
Expand All @@ -234,8 +246,12 @@ def publish_module(environ, start_response,

for i in range(getattr(request, 'retry_max_count', 3) + 1):
try:
response = _publish_response(
request, response, module_info, _publish=_publish)
with load_app(module_info) as new_mod_info:
response = _publish_response(
request,
response,
new_mod_info,
_publish=_publish)
break
except (ConflictError, TransientError) as exc:
if request.supports_retry():
Expand Down
7 changes: 6 additions & 1 deletion src/ZPublisher/tests/test_pubevents.py
Expand Up @@ -159,8 +159,13 @@ def _succeed():
return 'success'


# Poor man's mock.
class _Application(object):
pass
def __getattr__(self, name):
return self

def __call__(self, *args, **kwargs):
return self


class _Reporter(object):
Expand Down

0 comments on commit 904241d

Please sign in to comment.