diff --git a/news/74.bugfix.rst b/news/74.bugfix.rst new file mode 100644 index 0000000..9a12bde --- /dev/null +++ b/news/74.bugfix.rst @@ -0,0 +1 @@ +Fixed an issue where paths could sometimes fail to be fs-encoded properly when using backported ``NamedTemporaryFile`` instances. diff --git a/src/vistir/backports/tempfile.py b/src/vistir/backports/tempfile.py index 2b0a0be..f5594a2 100644 --- a/src/vistir/backports/tempfile.py +++ b/src/vistir/backports/tempfile.py @@ -15,6 +15,24 @@ from backports.weakref import finalize +def fs_encode(path): + try: + return os.fsencode(path) + except AttributeError: + from ..compat import fs_encode + + return fs_encode(path) + + +def fs_decode(path): + try: + return os.fsdecode(path) + except AttributeError: + from ..compat import fs_decode + + return fs_decode(path) + + __all__ = ["finalize", "NamedTemporaryFile"] @@ -48,7 +66,7 @@ def _sanitize_params(prefix, suffix, dir): if output_type is str: dir = gettempdir() else: - dir = os.fsencode(gettempdir()) + dir = fs_encode(gettempdir()) return prefix, suffix, dir, output_type diff --git a/test_compat.py b/test_compat.py new file mode 100644 index 0000000..7d969dd --- /dev/null +++ b/test_compat.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- + +from vistir.compat import fs_decode, fs_encode + + +def test_fs_encode(): + # This fails in the normal backports library: + # see https://github.com/PiDelport/backports.os/issues/13 + assert fs_decode(fs_encode(u"unicode\u0141")) == u"unicode\u0141"