diff options
author | 秋葉 <ambiguous404@gmail.com> | 2020-10-22 09:56:12 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-22 04:56:12 +0300 |
commit | 089a1a73e6af6ca8f7f3627f04843add023b86ce (patch) | |
tree | 7265f5bc7d5a89609cf47a5a05a7b2f75b287799 | |
parent | 2f5052671d5365323dc5e0b1f2b5a20686741829 (diff) | |
download | eventlet-089a1a73e6af6ca8f7f3627f04843add023b86ce.tar.gz |
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
-rw-r--r-- | eventlet/green/builtin.py | 16 | ||||
-rw-r--r-- | tests/isolated/patcher_builtin.py | 14 | ||||
-rw-r--r-- | tests/patcher_test.py | 4 |
3 files changed, 28 insertions, 6 deletions
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') |