Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb49b7c5

Browse files
committed
Multiple fixes and enhancements for monkey pluggability
1. `_check_repatching` now merges kwargs into state2. `_get_patch_all_state` allows to retrieve the patch_all configuration state3.a `_patch_module` now allows to specific `_package_prefix` parameter that defaults to 'gevent.'3.b `import_module` is now used in `_patch_module` to allow handling of package names of any depth.4. `patch_item` and both `patch_modules` now accept argument `_patch_module` for item's `__module__` override.fixes#1447
1 parent9fbb971 commitb49b7c5

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

‎src/gevent/monkey.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def patch_psycopg(event):
119119
from __future__importabsolute_import
120120
from __future__importprint_function
121121
importsys
122+
fromimportlibimportimport_module
122123

123124
__all__= [
124125
'patch_all',
@@ -255,11 +256,18 @@ def get_original(mod_name, item_name):
255256
_NONE=object()
256257

257258

258-
defpatch_item(module,attr,newitem):
259+
defpatch_item(module,attr,newitem,_patch_module=False):
259260
olditem=getattr(module,attr,_NONE)
260261
ifolditemisnot_NONE:
261262
saved.setdefault(module.__name__, {}).setdefault(attr,olditem)
262263
setattr(module,attr,newitem)
264+
if_patch_module:
265+
ifolditemisnotNoneandnewitemisnotNone:
266+
try:
267+
newitem.__module__=olditem.__module__
268+
except (TypeError,AttributeError):
269+
# We will let this fail quietly
270+
pass
263271

264272

265273
defremove_item(module,attr):
@@ -290,7 +298,8 @@ def patch_module(target_module, source_module, items=None,
290298
_warnings=None,
291299
_notify_will_subscribers=True,
292300
_notify_did_subscribers=True,
293-
_call_hooks=True):
301+
_call_hooks=True,
302+
_patch_module=False):
294303
"""
295304
patch_module(target_module, source_module, items=None)
296305
@@ -336,7 +345,8 @@ def patch_module(target_module, source_module, items=None,
336345
returnFalse
337346

338347
forattrinitems:
339-
patch_item(target_module,attr,getattr(source_module,attr))
348+
patch_item(target_module,attr,getattr(source_module,attr),
349+
_patch_module=_patch_module)
340350

341351
if_call_hooks:
342352
__call_module_hook(source_module,'did',target_module,items,_warnings)
@@ -357,11 +367,13 @@ def _patch_module(name,
357367
_warnings=None,
358368
_notify_will_subscribers=True,
359369
_notify_did_subscribers=True,
360-
_call_hooks=True):
370+
_call_hooks=True,
371+
_patch_module=False,
372+
_package_prefix='gevent.'):
361373

362-
gevent_module=getattr(__import__('gevent.'+name),name)
374+
gevent_module=import_module(_package_prefix+name)
363375
module_name=getattr(gevent_module,'__target__',name)
364-
target_module=__import__(module_name)
376+
target_module=import_module(module_name)
365377

366378
patch_module(target_module,gevent_module,items=items,
367379
_warnings=_warnings,
@@ -386,7 +398,8 @@ def _patch_module(name,
386398
_warnings=_warnings,
387399
_notify_will_subscribers=False,
388400
_notify_did_subscribers=False,
389-
_call_hooks=False)
401+
_call_hooks=False,
402+
_patch_module=_patch_module)
390403
saved[alternate_name]=saved[module_name]
391404

392405
returngevent_module,target_module
@@ -951,9 +964,15 @@ def patch_signal():
951964
_patch_module("signal")
952965

953966

967+
def_get_patch_all_state():
968+
key='_gevent_saved_patch_all'
969+
returnsaved.get(key)
970+
971+
954972
def_check_repatching(**module_settings):
955973
_warnings= []
956974
key='_gevent_saved_patch_all'
975+
module_settings.update(module_settings['kwargs'])
957976
delmodule_settings['kwargs']
958977
ifsaved.get(key,module_settings)!=module_settings:
959978
_queue_warning("Patching more than once will result in the union of all True"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp