Skip to content

Commit

Permalink
patcher: monkey_patch(builtins=True) failed on py3 because file cla…
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanaasagi committed Oct 22, 2020
1 parent 2f50526 commit 089a1a7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
16 changes: 10 additions & 6 deletions eventlet/green/builtin.py
Expand Up @@ -13,22 +13,26 @@
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
Expand Down
14 changes: 14 additions & 0 deletions 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')
4 changes: 4 additions & 0 deletions tests/patcher_test.py
Expand Up @@ -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')

0 comments on commit 089a1a7

Please sign in to comment.