From d81fe590db7cb420ff6b2d59ea24cc5fc47769da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 2 Jan 2019 12:47:16 +0100 Subject: [PATCH] BlendPath: don't use os.path.join() for slash notations The BlendPath should just use forward slashes, and not be depending on the current platform. --- blender_asset_tracer/bpathlib.py | 4 ++-- tests/test_bpathlib.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/blender_asset_tracer/bpathlib.py b/blender_asset_tracer/bpathlib.py index de1abd7..4bd0c22 100644 --- a/blender_asset_tracer/bpathlib.py +++ b/blender_asset_tracer/bpathlib.py @@ -79,13 +79,13 @@ class BlendPath(bytes): sub = BlendPath(subpath) if sub.is_absolute(): raise ValueError("'a / b' only works when 'b' is a relative path") - return BlendPath(os.path.join(self, sub)) + return BlendPath(self.rstrip(b'/') + b'/' + sub) def __rtruediv__(self, parentpath: bytes): """Slash notation like pathlib.Path.""" if self.is_absolute(): raise ValueError("'a / b' only works when 'b' is a relative path") - return BlendPath(os.path.join(parentpath, self)) + return BlendPath(parentpath.rstrip(b'/') + b'/' + self) def to_path(self) -> pathlib.PurePath: """Convert this path to a pathlib.PurePath. diff --git a/tests/test_bpathlib.py b/tests/test_bpathlib.py index 43bfe99..154bbfd 100644 --- a/tests/test_bpathlib.py +++ b/tests/test_bpathlib.py @@ -45,6 +45,11 @@ class BlendPathTest(unittest.TestCase): with self.assertRaises(ValueError): b'/root/and' / BlendPath(b'/parent.blend') + # On Windows+Python 3.5.4 this resulted in b'//root//parent.blend', + # but only if the root is a single term (so not b'//root/and/'). + self.assertEqual(BlendPath(b'//root/parent.blend'), + BlendPath(b'//root/') / b'parent.blend') + def test_mkrelative(self): self.assertEqual(b'//asset.png', BlendPath.mkrelative( Path('/path/to/asset.png'),