Commit 4371d5c8 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

Localizer: update the 'Global Request' monkey-patch to support WSGIPublisher

parent 59212692
...@@ -28,7 +28,7 @@ from .itools.i18n import AcceptLanguageType ...@@ -28,7 +28,7 @@ from .itools.i18n import AcceptLanguageType
# Import from Zope # Import from Zope
import Globals import Globals
from ZPublisher import Publish from ZPublisher import Publish, WSGIPublisher
from ZPublisher.HTTPRequest import HTTPRequest from ZPublisher.HTTPRequest import HTTPRequest
...@@ -65,45 +65,44 @@ def get_request(): ...@@ -65,45 +65,44 @@ def get_request():
"""Get a request object""" """Get a request object"""
return _requests.get(get_ident(), None) return _requests.get(get_ident(), None)
def get_new_publish(zope_publish):
def new_publish(request, module_name, after_list, debug=0, def publish(request, *args, **kwargs):
zope_publish=Publish.publish): # Get the process id
# Get the process id ident = get_ident()
ident = get_ident()
# Add the request object to the global dictionnary
# Add the request object to the global dictionnary with _requests_lock:
_requests_lock.acquire() _requests[ident] = request
try:
_requests[ident] = request # Call the old publish
finally: try:
_requests_lock.release() # Publish
return zope_publish(request, *args, **kwargs)
# Call the old publish finally:
try: # Remove the request object.
# Publish # When conflicts occur the "publish" method is called again,
x = zope_publish(request, module_name, after_list, debug) # recursively. In this situation the "_requests dictionary would
finally: # be cleaned in the innermost call, hence outer calls find the
# Remove the request object. # request does not exist anymore. For this reason we check first
# When conflicts occur the "publish" method is called again, # wether the request is there or not.
# recursively. In this situation the "_requests dictionary would if ident in _requests:
# be cleaned in the innermost call, hence outer calls find the with _requests_lock:
# request does not exist anymore. For this reason we check first del _requests[ident]
# wether the request is there or not. return publish
if ident in _requests:
_requests_lock.acquire()
try:
del _requests[ident]
finally:
_requests_lock.release()
return x
if patch is False: if patch is False:
logger.info('Install "Globals.get_request".') logger.info('Install "Globals.get_request".')
# Apply the patch # Apply the patch
Publish.publish = new_publish Publish.publish = get_new_publish(Publish.publish)
WSGIPublisher.publish = get_new_publish(WSGIPublisher.publish)
# Update WSGIPublisher.publish_module.__defaults__, otherwise it will use
# the unpatched WSGIPublisher.publish.
WSGIPublisher.publish_module.__defaults__ = (
WSGIPublisher.publish,
) + WSGIPublisher.publish_module.__defaults__[1:]
# First import (it's not a refresh operation). # First import (it's not a refresh operation).
# We need to apply the patches. # We need to apply the patches.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment