Skip to content

Commit

Permalink
Merge pull request #76 from sarugaku/bugfix/74
Browse files Browse the repository at this point in the history
Fix backport fsencode reference
  • Loading branch information
techalchemy committed Apr 3, 2019
2 parents 7a7567f + 2e5bf04 commit e2f2b25
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/74.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed an issue where paths could sometimes fail to be fs-encoded properly when using backported ``NamedTemporaryFile`` instances.
20 changes: 19 additions & 1 deletion src/vistir/backports/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down Expand Up @@ -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


Expand Down
9 changes: 9 additions & 0 deletions test_compat.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit e2f2b25

Please sign in to comment.