Commit b0435230 authored by Jason Madden's avatar Jason Madden

Add kwargs to patch_all for plugins.

parent 57b465b3
......@@ -398,7 +398,12 @@ class IGeventWillPatchAllEvent(IGeventWillPatchEvent):
"""
patch_all_arguments = Attribute(
"A dictionary all the arguments to `gevent.monkey.patch_all`. "
"A dictionary of all the arguments to `gevent.monkey.patch_all`. "
"This dictionary should not be modified. "
)
patch_all_kwargs = Attribute(
"A dictionary of the extra arguments to `gevent.monkey.patch_all`. "
"This dictionary should not be modified. "
)
......@@ -408,14 +413,19 @@ class IGeventWillPatchAllEvent(IGeventWillPatchEvent):
"""
class _PatchAllMixin(object):
def __init__(self, patch_all_arguments):
def __init__(self, patch_all_arguments, patch_all_kwargs):
super(_PatchAllMixin, self).__init__(None, None)
self._patch_all_arguments = patch_all_arguments
self._patch_all_kwargs = patch_all_kwargs
@property
def patch_all_arguments(self):
return self._patch_all_arguments.copy()
@property
def patch_all_kwargs(self):
return self._patch_all_kwargs.copy()
def __repr__(self):
return '<%s %r at %x>' % (self.__class__.__name__,
self._patch_all_arguments,
......@@ -443,8 +453,15 @@ class IGeventDidPatchBuiltinModulesEvent(IGeventDidPatchEvent):
patch_all_arguments = Attribute(
"A dictionary of all the arguments to `gevent.monkey.patch_all`. "
"This dictionary should not be modified. "
)
patch_all_kwargs = Attribute(
"A dictionary of the extra arguments to `gevent.monkey.patch_all`. "
"This dictionary should not be modified. "
)
@implementer(IGeventDidPatchBuiltinModulesEvent)
class GeventDidPatchBuiltinModulesEvent(_PatchAllMixin, GeventDidPatchEvent):
"""
Implementation of `IGeventDidPatchBuiltinModulesEvent`.
......@@ -463,7 +480,7 @@ class IGeventDidPatchAllEvent(IGeventDidPatchEvent):
"""
@implementer(IGeventDidPatchAllEvent)
class GeventDidPatchAllEvent(GeventDidPatchEvent):
class GeventDidPatchAllEvent(_PatchAllMixin, GeventDidPatchEvent):
"""
Implementation of `IGeventDidPatchAllEvent`.
"""
......@@ -471,6 +488,3 @@ class GeventDidPatchAllEvent(GeventDidPatchEvent):
#: The name of the setuptools entry point that is called when this
#: event is emitted.
ENTRY_POINT_NAME = 'gevent.plugins.monkey.did_patch_all'
def __init__(self):
super(GeventDidPatchAllEvent, self).__init__(None, None)
......@@ -826,6 +826,7 @@ def patch_signal():
def _check_repatching(**module_settings):
_warnings = []
key = '_gevent_saved_patch_all'
del module_settings['kwargs']
if saved.get(key, module_settings) != module_settings:
_queue_warning("Patching more than once will result in the union of all True"
" parameters being patched",
......@@ -848,7 +849,8 @@ def _subscribe_signal_os(will_patch_all):
def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=True, ssl=True,
httplib=False, # Deprecated, to be removed.
subprocess=True, sys=False, aggressive=True, Event=True,
builtins=True, signal=True):
builtins=True, signal=True,
**kwargs):
"""
Do all of the default monkey patching (calls every other applicable
function in this module).
......@@ -868,6 +870,10 @@ def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=Tru
``Event`` defaults to True.
.. versionchanged:: 1.3b1
Defined the return values.
.. versionchanged:: 1.3b1
Add ``**kwargs`` for the benefit of event subscribers. CAUTION: gevent may add
and interpret additional arguments in the future, so it is suggested to use prefixes
for kwarg values to be interpreted by plugins, for example, `patch_all(mylib_futures=True)`.
"""
# pylint:disable=too-many-locals,too-many-branches
......@@ -880,7 +886,7 @@ def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=Tru
from gevent import events
try:
_notify_patch(events.GeventWillPatchAllEvent(modules_to_patch), _warnings)
_notify_patch(events.GeventWillPatchAllEvent(modules_to_patch, kwargs), _warnings)
except events.DoNotPatch:
return False
......@@ -910,8 +916,8 @@ def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=Tru
if signal:
patch_signal()
_notify_patch(events.GeventDidPatchBuiltinModulesEvent(modules_to_patch), _warnings)
_notify_patch(events.GeventDidPatchAllEvent(), _warnings)
_notify_patch(events.GeventDidPatchBuiltinModulesEvent(modules_to_patch, kwargs), _warnings)
_notify_patch(events.GeventDidPatchAllEvent(modules_to_patch, kwargs), _warnings)
_process_warnings(_warnings)
return True
......
......@@ -70,6 +70,7 @@ class TestMonkey(SubscriberCleanupMixin, unittest.TestCase):
def test_patch_twice_warnings_events(self):
import warnings
from zope.interface import verify
orig_saved = {}
for k, v in monkey.saved.items():
......@@ -88,7 +89,7 @@ class TestMonkey(SubscriberCleanupMixin, unittest.TestCase):
with warnings.catch_warnings(record=True) as issued_warnings:
# Patch again, triggering three warnings, one for os=False/signal=True,
# one for repeated monkey-patching, one for patching after ssl (on python >= 2.7.9)
monkey.patch_all(os=False)
monkey.patch_all(os=False, extra_kwarg=42)
self.assertGreaterEqual(len(issued_warnings), 2)
self.assertIn('SIGCHLD', str(issued_warnings[-1].message))
self.assertIn('more than once', str(issued_warnings[0].message))
......@@ -114,10 +115,20 @@ class TestMonkey(SubscriberCleanupMixin, unittest.TestCase):
self.assertNotIn('gevent', str(v))
self.assertIsInstance(all_events[0], events.GeventWillPatchAllEvent)
self.assertEqual({'extra_kwarg': 42}, all_events[0].patch_all_kwargs)
verify.verifyObject(events.IGeventWillPatchAllEvent, all_events[0])
self.assertIsInstance(all_events[1], events.GeventWillPatchModuleEvent)
verify.verifyObject(events.IGeventWillPatchModuleEvent, all_events[1])
self.assertIsInstance(all_events[2], events.GeventDidPatchModuleEvent)
verify.verifyObject(events.IGeventWillPatchModuleEvent, all_events[1])
self.assertIsInstance(all_events[-2], events.GeventDidPatchBuiltinModulesEvent)
verify.verifyObject(events.IGeventDidPatchBuiltinModulesEvent, all_events[-2])
self.assertIsInstance(all_events[-1], events.GeventDidPatchAllEvent)
verify.verifyObject(events.IGeventDidPatchAllEvent, all_events[-1])
for e in all_events:
self.assertFalse(isinstance(e, events.GeventDidPatchModuleEvent)
......
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