Skip to content

Commit

Permalink
workaround for pathlib on py 3.7
Browse files Browse the repository at this point in the history
fixes eventlet#534

pathlib._NormalAccessor wraps `open` in `staticmethod` for py < 3.7 but
not 3.7. That means we `Path.open` calls `green.os.open` with `file`
being a pathlib._NormalAccessor object, and the other arguments shifted.
Fortunately pathlib doesn't use the `dir_fd` argument, so we have space
in the parameter list. We use some heuristics to detect this and adjust
the parameters (without importing pathlib)
  • Loading branch information
davidszotten committed Aug 19, 2019
1 parent 7f8d478 commit 124c7eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions eventlet/green/os.py
Expand Up @@ -103,6 +103,15 @@ def open(file, flags, mode=0o777, dir_fd=None):
This behaves identically, but collaborates with
the hub's notify_opened protocol.
"""
# pathlib workaround #534 pathlib._NormalAccessor wraps `open` in
# `staticmethod` for py < 3.7 but not 3.7. That means we get here with
# `file` being a pathlib._NormalAccessor object, and the other arguments
# shifted. Fortunately pathlib doesn't use the `dir_fd` argument, so we
# have space in the parameter list. We use some heuristics to detect this
# and adjust the parameters (without importing pathlib)
if type(file).__name__ == '_NormalAccessor':
file, flags, mode, dir_fd = flags, mode, dir_fd, None

if dir_fd is not None:
fd = __original_open__(file, flags, mode, dir_fd=dir_fd)
else:
Expand Down
9 changes: 9 additions & 0 deletions tests/os_test.py
@@ -0,0 +1,9 @@
import eventlet
pathlib = eventlet.import_patched('pathlib')


def test_pathlib_open_issue_534():
path = pathlib.Path(__file__)
with path.open():
# should not raise
pass

0 comments on commit 124c7eb

Please sign in to comment.