def patch_select_module(testcase, *keep, **replace):
""" Helper function that removes all selectors from the select module
except those listed in *keep and **replace. Those in keep will be kept
if they exist in the select module and those in replace will be patched
with the value that is given regardless if they exist or not. Cleanup
will restore previous state. This helper also resets the selectors module
so that a call to DefaultSelector() will do feature detection again. """
selectors2._DEFAULT_SELECTOR = None
for s in ['select', 'poll', 'epoll', 'kqueue']:
if s in replace:
if hasattr(select, s):
old_selector = getattr(select, s)
testcase.addCleanup(setattr, select, s, old_selector)
else:
testcase.addCleanup(delattr, select, s)
setattr(select, s, replace[s])
elif s not in keep and hasattr(select, s):
old_selector = getattr(select, s)
testcase.addCleanup(setattr, select, s, old_selector)
delattr(select, s)
评论列表
文章目录