Skip to content
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

Handle leading slash in local path edge case #238

Merged
merged 1 commit into from
Oct 2, 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
14 changes: 7 additions & 7 deletions smart_open/smart_open_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,10 @@ def _parse_uri(uri_as_string):
return _parse_uri_webhdfs(parsed_uri)
elif parsed_uri.scheme in smart_open_s3.SUPPORTED_SCHEMES:
return _parse_uri_s3x(parsed_uri)
elif parsed_uri.scheme in ('file', '', None):
return _parse_uri_file(parsed_uri)
elif parsed_uri.scheme == 'file':
return _parse_uri_file(parsed_uri.netloc + parsed_uri.path)
elif parsed_uri.scheme in ('', None):
return _parse_uri_file(uri_as_string)
elif parsed_uri.scheme.startswith('http'):
return Uri(scheme=parsed_uri.scheme, uri_path=uri_as_string)
else:
Expand Down Expand Up @@ -531,14 +533,12 @@ def _parse_uri_s3x(parsed_uri):
)


def _parse_uri_file(parsed_uri):
assert parsed_uri.scheme in (None, '', 'file')
uri_path = parsed_uri.netloc + parsed_uri.path
def _parse_uri_file(input_path):
# '~/tmp' may be expanded to '/Users/username/tmp'
uri_path = os.path.expanduser(uri_path)
uri_path = os.path.expanduser(input_path)

if not uri_path:
raise RuntimeError("invalid file URI: %s" % str(parsed_uri))
raise RuntimeError("invalid file URI: %s" % input_path)

return Uri(scheme='file', uri_path=uri_path)

Expand Down
10 changes: 10 additions & 0 deletions smart_open/tests/test_smart_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ def test_invalid_port2(self):
as_string = 's3u://user:secret@host:port:foo@mybucket/mykey.txt'
self.assertRaises(ValueError, smart_open_lib._parse_uri, as_string)

def test_leading_slash_local_file(self):
path = "/home/misha/hello.txt"
uri = smart_open_lib._parse_uri(path)
self.assertEqual(uri.scheme, "file")
self.assertEqual(uri.uri_path, path)

uri = smart_open_lib._parse_uri('//' + path)
self.assertEqual(uri.scheme, "file")
self.assertEqual(uri.uri_path, '//' + path)


class SmartOpenHttpTest(unittest.TestCase):
"""
Expand Down