BlendPath.as_path() now refuses to convert blendfile-relative paths

When a path starts with b'//' it will not be converted to a PurePath, as
the handling of such filenames is platform dependent (Windows handles those
weirdly, like appending a slash to any path).
This commit is contained in:
Sybren A. Stüvel 2018-12-20 15:35:06 +01:00
parent 5635895d0c
commit 19216cb12e
2 changed files with 6 additions and 4 deletions

View File

@ -87,17 +87,19 @@ class BlendPath(bytes):
def to_path(self) -> pathlib.PurePath: def to_path(self) -> pathlib.PurePath:
"""Convert this path to a pathlib.PurePath. """Convert this path to a pathlib.PurePath.
This path MUST NOT be a blendfile-relative path (e.g. it may not start
with `//`). For such paths, first use `.absolute()` to resolve the path.
Interprets the path as UTF-8, and if that fails falls back to the local Interprets the path as UTF-8, and if that fails falls back to the local
filesystem encoding. filesystem encoding.
Note that this does not handle blend-file-relative paths specially, so
the returned Path may still start with '//'.
""" """
# TODO(Sybren): once we target Python 3.6, implement __fspath__(). # TODO(Sybren): once we target Python 3.6, implement __fspath__().
try: try:
decoded = self.decode('utf8') decoded = self.decode('utf8')
except UnicodeDecodeError: except UnicodeDecodeError:
decoded = self.decode(sys.getfilesystemencoding()) decoded = self.decode(sys.getfilesystemencoding())
if self.is_blendfile_relative():
raise ValueError('to_path() cannot be used on blendfile-relative paths')
return pathlib.PurePath(decoded) return pathlib.PurePath(decoded)
def is_blendfile_relative(self) -> bool: def is_blendfile_relative(self) -> bool:

View File

@ -110,7 +110,7 @@ class BlockIterator:
# We've gone through all the blocks in this file, now open the libraries # We've gone through all the blocks in this file, now open the libraries
# and iterate over the blocks referred there. # and iterate over the blocks referred there.
for lib_bpath, idblocks in blocks_per_lib.items(): for lib_bpath, idblocks in blocks_per_lib.items():
lib_path = lib_bpath.to_path() lib_path = pathlib.Path(lib_bpath.to_path())
try: try:
lib_path = lib_path.resolve() lib_path = lib_path.resolve()
except FileNotFoundError: except FileNotFoundError: