From 089a1a73e6af6ca8f7f3627f04843add023b86ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E8=91=89?= Date: Thu, 22 Oct 2020 09:56:12 +0800 Subject: patcher: monkey_patch(builtins=True) failed on py3 because `file` class is gone (#545) https://github.com/eventlet/eventlet/issues/541 https://github.com/eventlet/eventlet/pull/545 https://docs.python.org/release/3.0/whatsnew/3.0.html#builtins --- eventlet/green/builtin.py | 16 ++++++++++------ tests/isolated/patcher_builtin.py | 14 ++++++++++++++ tests/patcher_test.py | 4 ++++ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 tests/isolated/patcher_builtin.py diff --git a/eventlet/green/builtin.py b/eventlet/green/builtin.py index 3dd2c76..71de1dc 100644 --- a/eventlet/green/builtin.py +++ b/eventlet/green/builtin.py @@ -13,22 +13,26 @@ from eventlet import hubs from eventlet.hubs import hub from eventlet.patcher import slurp_properties import sys +import six __all__ = dir(builtins_orig) -__patched__ = ['file', 'open'] +__patched__ = ['open'] +if six.PY2: + __patched__ += ['file'] slurp_properties(builtins_orig, globals(), ignore=__patched__, srckeys=dir(builtins_orig)) hubs.get_hub() -__original_file = file +if six.PY2: + __original_file = file + class file(__original_file): + def __init__(self, *args, **kwargs): + super(file, self).__init__(*args, **kwargs) + hubs.notify_opened(self.fileno()) -class file(__original_file): - def __init__(self, *args, **kwargs): - super(file, self).__init__(*args, **kwargs) - hubs.notify_opened(self.fileno()) __original_open = open __opening = False diff --git a/tests/isolated/patcher_builtin.py b/tests/isolated/patcher_builtin.py new file mode 100644 index 0000000..3de73bf --- /dev/null +++ b/tests/isolated/patcher_builtin.py @@ -0,0 +1,14 @@ +if __name__ == '__main__': + from tests.mock import patch + + import sys + import eventlet + from eventlet import hubs + with patch.object(hubs, 'notify_opened') as mock_func: + eventlet.monkey_patch(builtins=True) + with open(__file__, 'r') as f: + mock_func.assert_called_with(f.fileno()) + if sys.version_info.major == 2: + with file(__file__, 'r') as f: + mock_func.assert_called_with(f.fileno()) + print('pass') diff --git a/tests/patcher_test.py b/tests/patcher_test.py index f1ef1f9..6ff6e5a 100644 --- a/tests/patcher_test.py +++ b/tests/patcher_test.py @@ -519,3 +519,7 @@ def test_threadpoolexecutor(): def test_fork_after_monkey_patch(): tests.run_isolated('patcher_fork_after_monkey_patch.py') + + +def test_builtin(): + tests.run_isolated('patcher_builtin.py') -- cgit v1.2.1