-
-
Notifications
You must be signed in to change notification settings - Fork 382
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
Add support for pathlib.Path
. Fix #170
#175
Changes from 3 commits
60236b7
9c57e82
d9f60b5
7b88cb1
bfa729c
ae23fa4
279b4d7
9f0d342
94e903a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,12 @@ | |
# This code is distributed under the terms and conditions | ||
# from the MIT License (MIT). | ||
|
||
import importlib | ||
import unittest | ||
import logging | ||
import tempfile | ||
import os | ||
import pkgutil | ||
import sys | ||
import hashlib | ||
|
||
|
@@ -191,6 +193,36 @@ def test_open_with_keywords_explicit_r(self): | |
actual = fin.read() | ||
self.assertEqual(expected, actual) | ||
|
||
@unittest.skipIf( | ||
(pkgutil.find_loader('pathlib') is None and | ||
pkgutil.find_loader('pathlib2') is None), | ||
"do not test pathlib support if pathlib or backport are not available") | ||
def test_open_and_read_pathlib_path(self): | ||
"""If ``pathlib.Path`` is available we should be able to open and read.""" | ||
fpath = os.path.join(CURR_DIR, 'test_data/cp852.tsv.txt') | ||
|
||
# Import ``pathlib`` if the builtin ``pathlib`` or the backport | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we're duplicating unnecessary work here. We've already gone through the pain of importing the necessary pathlib module in smart_open_lib. So, we can just get rid of all this import stuff, and use: from smart_open.smart_open_lib import pathlib
path = pathlib.Path('/foo/bar') whenever you need access to the actual pathlib module. |
||
# ``pathlib2`` are available. The builtin ``pathlib`` will be imported | ||
# with higher precedence. | ||
for pathlib_module in ('pathlib', 'pathlib2'): | ||
try: | ||
pathlib = importlib.import_module(pathlib_module) | ||
break | ||
# Unit test will skip if either module is unavailable so it's safe | ||
# to assume we can import _at least_ one working ``pathlib``. | ||
except ImportError: | ||
pass | ||
|
||
# builtin open() supports pathlib.Path in python>=3.6 only | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to bother with this is in the tests. From the point of view of the test, it's an unnecessary detail. In this particular case, we just want to read the expected data in the quickest and simplest possible way. So in this case, I think it's better to just do: with open(fpath) as fin:
expected = fin.read().decode('cp852') |
||
path_open = pathlib.Path(fpath) if sys.version_info >= (3, 6) else fpath | ||
path_smart_open = pathlib.Path(fpath) | ||
|
||
with open(path_open, 'rb') as fin: | ||
expected = fin.read().decode('cp852') | ||
with smart_open.smart_open(path_smart_open, encoding='cp852') as fin: | ||
actual = fin.read() | ||
self.assertEqual(expected, actual) | ||
|
||
@mock_s3 | ||
def test_read_never_returns_none(self): | ||
"""read should never return None.""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a simpler way of writing this is:
@unittest.skipIf(smart_open_lib.PATHLIB_SUPPORT is False, "your reason here")