# -*- coding: utf-8 -*-"""State management and query functions for tracking and discovering whathas been patched."""importlogginglogger=logging.getLogger(__name__)# maps module name -> {attribute name: original item}# e.g. "time" -> {"sleep": built-in function sleep}# NOT A PUBLIC API. However, third-party monkey-patchers may be using# it? TODO: Provide better API for them.saved:dict={}
[docs]defis_module_patched(mod_name):""" Check if a module has been replaced with a cooperative version. :param str mod_name: The name of the standard library module, e.g., ``'socket'``. """returnmod_nameinsaved
[docs]defis_object_patched(mod_name,item_name):""" Check if an object in a module has been replaced with a cooperative version. :param str mod_name: The name of the standard library module, e.g., ``'socket'``. :param str item_name: The name of the attribute in the module, e.g., ``'create_connection'``. """returnis_module_patched(mod_name)anditem_nameinsaved[mod_name]
defis_anything_patched():""" Check if this module has done any patching in the current process. This is currently only used in gevent tests. Not currently a documented, public API, because I'm not convinced it is 100% reliable in the event of third-party patch functions that don't use ``saved``. .. versionadded:: 21.1.0 """returnbool(saved)def_get_original(name,items):d=saved.get(name,{})values=[]module=Noneforiteminitems:ifitemind:values.append(d[item])else:ifmoduleisNone:module=__import__(name)values.append(getattr(module,item))returnvaluesdef_save(module,attr_name,item):saved.setdefault(module.__name__,{}).setdefault(attr_name,item)