Skip to content

Better handle pathological filenames #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions nibabel/filename_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,14 @@ def splitext_addext(filename,
for ext in addexts:
if endswith(filename, ext):
extpos = -len(ext)
addext = filename[extpos:]
filename = filename[:extpos]
filename, addext = filename[:extpos], filename[extpos:]
break
else:
addext = ''
return os.path.splitext(filename) + (addext,)
# os.path.splitext() behaves unexpectedly when filename starts with '.'
extpos = filename.rfind('.')
if extpos < 0 or filename.strip('.') == '':
root, ext = filename, ''
else:
root, ext = filename[:extpos], filename[extpos:]
return (root, ext, addext)
11 changes: 11 additions & 0 deletions nibabel/tests/test_filename_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,14 @@ def test_splitext_addext():
# case sensitive
res = splitext_addext('fname.ext.FOO', ('.foo', '.bar'), True)
assert_equal(res, ('fname.ext', '.FOO', ''))
# edge cases
res = splitext_addext('.nii')
assert_equal(res, ('', '.nii', ''))
res = splitext_addext('...nii')
assert_equal(res, ('..', '.nii', ''))
res = splitext_addext('.')
assert_equal(res, ('.', '', ''))
res = splitext_addext('..')
assert_equal(res, ('..', '', ''))
res = splitext_addext('...')
assert_equal(res, ('...', '', ''))