Disallow BlendPath(str)

This commit is contained in:
Sybren A. Stüvel 2018-03-08 14:18:07 +01:00
parent f2f824ad85
commit e9ee5b69ec
2 changed files with 6 additions and 16 deletions

View File

@ -15,10 +15,10 @@ class BlendPath(bytes):
def __new__(cls, path):
if isinstance(path, pathlib.Path):
path = str(path) # handle as string, which is encoded to bytes below.
if isinstance(path, str):
# As a convenience, when a string is given, interpret as UTF-8.
return bytes.__new__(cls, path.encode('utf-8'))
path = str(path).encode('utf-8')
if not isinstance(path, bytes):
raise TypeError('path must be bytes or pathlib.Path, but is %r' % path)
return bytes.__new__(cls, path)
@classmethod

View File

@ -6,18 +6,13 @@ from blender_asset_tracer.bpathlib import BlendPath
class BlendPathTest(unittest.TestCase):
def test_string_path(self):
p = BlendPath('//some/file.blend')
p = BlendPath(Path('//some/file.blend'))
self.assertEqual(b'//some/file.blend', p)
p = BlendPath(r'C:\some\file.blend')
p = BlendPath(Path(r'C:\some\file.blend'))
self.assertEqual(b'C:\\some\\file.blend', p)
def test_is_absolute(self):
self.assertFalse(BlendPath('//some/file.blend').is_absolute())
self.assertTrue(BlendPath('/some/file.blend').is_absolute())
self.assertTrue(BlendPath('C:/some/file.blend').is_absolute())
self.assertFalse(BlendPath('some/file.blend').is_absolute())
self.assertFalse(BlendPath(b'//some/file.blend').is_absolute())
self.assertTrue(BlendPath(b'/some/file.blend').is_absolute())
self.assertTrue(BlendPath(b'C:/some/file.blend').is_absolute())
@ -25,11 +20,6 @@ class BlendPathTest(unittest.TestCase):
self.assertFalse(BlendPath(b'some/file.blend').is_absolute())
def test_is_blendfile_relative(self):
self.assertTrue(BlendPath('//some/file.blend').is_blendfile_relative())
self.assertFalse(BlendPath('/some/file.blend').is_blendfile_relative())
self.assertFalse(BlendPath('C:/some/file.blend').is_blendfile_relative())
self.assertFalse(BlendPath('some/file.blend').is_blendfile_relative())
self.assertTrue(BlendPath(b'//some/file.blend').is_blendfile_relative())
self.assertFalse(BlendPath(b'/some/file.blend').is_blendfile_relative())
self.assertFalse(BlendPath(b'C:/some/file.blend').is_blendfile_relative())