path: Disallow unknown substitutions in expand_path

This commit is contained in:
Thomas Adamcik 2013-04-25 22:27:45 +02:00
parent ac7edad86d
commit a7b6ff7b18
2 changed files with 9 additions and 4 deletions

View File

@ -22,6 +22,9 @@ XDG_DIRS = {
'XDG_MUSIC_DIR': glib.get_user_special_dir(glib.USER_DIRECTORY_MUSIC),
}
# XDG_MUSIC_DIR can be none, so filter out any bad data.
XDG_DIRS = dict((k, v) for k, v in XDG_DIRS.items() if v is not None)
def get_or_create_dir(dir_path):
if not isinstance(dir_path, bytes):
@ -97,10 +100,13 @@ def split_path(path):
def expand_path(path):
# TODO: document as we want people to use this.
if not isinstance(path, bytes):
raise ValueError('Path is not a bytestring.')
# TODO: expandvars as well?
path = string.Template(path).safe_substitute(XDG_DIRS)
try:
path = string.Template(path).substitute(XDG_DIRS)
except KeyError:
return None
path = os.path.expanduser(path)
path = os.path.abspath(path)
return path

View File

@ -237,8 +237,7 @@ class ExpandPathTest(unittest.TestCase):
path.expand_path(b'$XDG_DATA_DIR/foo'))
def test_xdg_subsititution_unknown(self):
self.assertEqual(
b'/tmp/$XDG_INVALID_DIR/foo',
self.assertIsNone(
path.expand_path(b'/tmp/$XDG_INVALID_DIR/foo'))