Skip to content

Eventlet broke pathlib on Python 3.7 when patches os #534

@orsinium

Description

@orsinium

Script to reproduce:

import eventlet
eventlet.monkey_patch(os=True)
from pathlib import Path
Path('.').open()

# Python 3.x: IsADirectoryError: [Errno 21] Is a directory: '.'
# ^ it's ok
# Python 3.7.1: TypeError: open: path should be string, bytes or os.PathLike, not _NormalAccessor
# ^ it's not ok

As you can see, Path.open doesn't work on Python 3.7. It's because pathlib uses _NormalAccessor instance, that stores functions from os as attributes:

class _NormalAccessor(_Accessor):
    open = os.open

Python <3.7 wraps this functions in staticmethod like this:

class _NormalAccessor(_Accessor):
    open = staticmethod(os.open)

However Python 3.7 doesn't. Python doesn't bound os.open to _NormalAccessor because os.open isn't a function. But when eventlet patches os.open by the custom python function then interpreter bound this function (eventlet.green.os.open) to _NormalAccessor.

I can't report this bug to Python because I can't register on bugs.python.org >.<

Possible solution: patch _NormalAccessor too. But don't forget: in Python 3.6 passed argument has to converted to string:

class _NormalAccessor(_Accessor):

    def _wrap_strfunc(strfunc):
        @functools.wraps(strfunc)
        def wrapped(pathobj, *args):
            return strfunc(str(pathobj), *args)
        return staticmethod(wrapped)

    def _wrap_binary_strfunc(strfunc):
        @functools.wraps(strfunc)
        def wrapped(pathobjA, pathobjB, *args):
            return strfunc(str(pathobjA), str(pathobjB), *args)
        return staticmethod(wrapped)

    open = _wrap_strfunc(os.open)

References:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions