From 19216cb12e3f054d1ede0d21f5f917a183101b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 20 Dec 2018 15:35:06 +0100 Subject: [PATCH] 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). --- blender_asset_tracer/bpathlib.py | 8 +++++--- blender_asset_tracer/trace/file2blocks.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/blender_asset_tracer/bpathlib.py b/blender_asset_tracer/bpathlib.py index 3e3f592..170aae6 100644 --- a/blender_asset_tracer/bpathlib.py +++ b/blender_asset_tracer/bpathlib.py @@ -87,17 +87,19 @@ class BlendPath(bytes): def to_path(self) -> 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 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__(). try: decoded = self.decode('utf8') except UnicodeDecodeError: 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) def is_blendfile_relative(self) -> bool: diff --git a/blender_asset_tracer/trace/file2blocks.py b/blender_asset_tracer/trace/file2blocks.py index 97fb506..b45d7b7 100644 --- a/blender_asset_tracer/trace/file2blocks.py +++ b/blender_asset_tracer/trace/file2blocks.py @@ -110,7 +110,7 @@ class BlockIterator: # We've gone through all the blocks in this file, now open the libraries # and iterate over the blocks referred there. for lib_bpath, idblocks in blocks_per_lib.items(): - lib_path = lib_bpath.to_path() + lib_path = pathlib.Path(lib_bpath.to_path()) try: lib_path = lib_path.resolve() except FileNotFoundError: