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