Skip to content

Commit

Permalink
disable monkey patch on Py2
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenkov committed Mar 22, 2020
1 parent 4b2a904 commit 7b2ec25
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions smart_open/smart_open_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,8 @@ def _patch_pathlib(func):
"""Replace `Path.open` with `func`"""
if not PATHLIB_SUPPORT:
raise RuntimeError('install pathlib (or pathlib2) before using this function')
if six.PY2:
raise RuntimeError('this monkey patch does not work on Py2')
old_impl = pathlib.Path.open
pathlib.Path.open = func
return old_impl
9 changes: 7 additions & 2 deletions smart_open/tests/test_smart_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_gs_uri_contains_slash(self):
self.assertEqual(parsed_uri.bucket_id, "mybucket")
self.assertEqual(parsed_uri.blob_id, "mydir/myblob")

@unittest.skipUnless(smart_open_lib.PATHLIB_SUPPORT, "this test requires pathlib")
@unittest.skipUnless(smart_open_lib.six.PY3, "our monkey patch only works on Py3")
def test_pathlib_monkeypatch(self):
from smart_open.smart_open_lib import pathlib

Expand All @@ -305,7 +305,7 @@ def test_pathlib_monkeypatch(self):
_patch_pathlib(obj.old_impl)
assert pathlib.Path.open != smart_open.open

@unittest.skipUnless(smart_open_lib.PATHLIB_SUPPORT, "this test requires pathlib")
@unittest.skipUnless(smart_open_lib.six.PY3, "our monkey patch only works on Py3")
def test_pathlib_monkeypath_read_gz(self):
from smart_open.smart_open_lib import pathlib

Expand All @@ -325,6 +325,11 @@ def test_pathlib_monkeypath_read_gz(self):
finally:
_patch_pathlib(obj.old_impl)

@unittest.skipUnless(smart_open_lib.six.PY2, 'this test is for Py2 only')
def test_monkey_patch_raises_exception_py2(self):
with self.assertRaises(RuntimeError):
patch_pathlib()


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

0 comments on commit 7b2ec25

Please sign in to comment.