From 784943d0b74adc9cdfef10d27dfe8a8e6a3560f0 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 28 Jan 2020 20:29:59 +0100 Subject: [PATCH 1/5] tests: test behavior with py.path.local and bytes --- testing/path/test_local.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/testing/path/test_local.py b/testing/path/test_local.py index ae009362..5335eff1 100644 --- a/testing/path/test_local.py +++ b/testing/path/test_local.py @@ -1064,3 +1064,36 @@ def test_default_encoding(self, tmpdir): s = x.read_text("ascii") assert s == part assert type(s) == type(part) + + +def test_behavior_with_bytes(): + """Test py.path.local's behavior with regard to bytes instead of text.""" + bpath = local(b"/bytesname") + + with pytest.raises(TypeError, match=r"__str__ returned non-string \(type bytes\)"): + assert str(bpath) == "/bytesname" + + brelpath = local(b"bytesname") + with pytest.raises(TypeError, match="a bytes-like object is required, not 'str'"): + local() / brelpath + + with pytest.raises( + TypeError, match="endswith first arg must be bytes or a tuple of bytes, not str" + ): + local(b"/") / brelpath + + p1 = local(b"/") + p1.sep = b"/" + with pytest.raises(TypeError, match="can't concat str to bytes"): + p1 / brelpath + + p1 = local(b"") + p1.sep = b"/" + assert p1 / brelpath == p1 / brelpath + + assert p1 / brelpath != local("") / local("bytesname") + with pytest.raises(TypeError, match=r"__str__ returned non-string \(type bytes\)"): + str(p1 / brelpath) + + strpath = (p1 / brelpath).strpath.decode() + assert strpath == str(local("") / local("bytesname")) From 8784502f3883723a0f5d0839e40ce0938a0f1301 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 28 Jan 2020 20:41:02 +0100 Subject: [PATCH 2/5] typing, py2 --- testing/path/test_local.py | 54 +++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/testing/path/test_local.py b/testing/path/test_local.py index 5335eff1..4f6c91f4 100644 --- a/testing/path/test_local.py +++ b/testing/path/test_local.py @@ -1066,34 +1066,52 @@ def test_default_encoding(self, tmpdir): assert type(s) == type(part) -def test_behavior_with_bytes(): +def test_behavior_with_bytes(): # type: () -> None """Test py.path.local's behavior with regard to bytes instead of text.""" bpath = local(b"/bytesname") + brelpath = local(b"bytesname") - with pytest.raises(TypeError, match=r"__str__ returned non-string \(type bytes\)"): + if sys.version_info < (3,): assert str(bpath) == "/bytesname" - - brelpath = local(b"bytesname") - with pytest.raises(TypeError, match="a bytes-like object is required, not 'str'"): + assert str(brelpath) == local("bytesname") local() / brelpath - - with pytest.raises( - TypeError, match="endswith first arg must be bytes or a tuple of bytes, not str" - ): local(b"/") / brelpath + else: + with pytest.raises( + TypeError, match=r"__str__ returned non-string \(type bytes\)" + ): + str(bpath) + with pytest.raises( + TypeError, match="a bytes-like object is required, not 'str'" + ): + local() / brelpath + with pytest.raises( + TypeError, + match="endswith first arg must be bytes or a tuple of bytes, not str", + ): + local(b"/") / brelpath p1 = local(b"/") - p1.sep = b"/" - with pytest.raises(TypeError, match="can't concat str to bytes"): + p1.sep = b"/" # type: ignore[assignment,misc] # noqa: F821 + if sys.version_info < (3,): p1 / brelpath + else: + with pytest.raises(TypeError, match="can't concat str to bytes"): + p1 / brelpath p1 = local(b"") - p1.sep = b"/" + p1.sep = b"/" # type: ignore[assignment,misc] # noqa: F821 assert p1 / brelpath == p1 / brelpath - assert p1 / brelpath != local("") / local("bytesname") - with pytest.raises(TypeError, match=r"__str__ returned non-string \(type bytes\)"): - str(p1 / brelpath) - - strpath = (p1 / brelpath).strpath.decode() - assert strpath == str(local("") / local("bytesname")) + if sys.version_info < (3,): + assert p1 / brelpath == local("") / local("bytesname") + assert str(p1 / brelpath) == str(local("") / local("bytesname")) + else: + assert p1 / brelpath != local("") / local("bytesname") + with pytest.raises( + TypeError, match=r"__str__ returned non-string \(type bytes\)" + ): + str(p1 / brelpath) + + strpath = (p1 / brelpath).strpath.decode() + assert strpath == str(local("") / local("bytesname")) From e448e598e501eab4bdeea86b51570203721c562a Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 28 Jan 2020 21:59:26 +0100 Subject: [PATCH 3/5] win32 [skip travis] --- testing/path/test_local.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/testing/path/test_local.py b/testing/path/test_local.py index 4f6c91f4..695acc4f 100644 --- a/testing/path/test_local.py +++ b/testing/path/test_local.py @@ -1072,7 +1072,10 @@ def test_behavior_with_bytes(): # type: () -> None brelpath = local(b"bytesname") if sys.version_info < (3,): - assert str(bpath) == "/bytesname" + if sys.platform == "win32": + assert str(bpath) == r"C:\bytesname" + else: + assert str(bpath) == "/bytesname" assert str(brelpath) == local("bytesname") local() / brelpath local(b"/") / brelpath From 3efc0d8855922c9ba055dcacab09e706fb702264 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 28 Jan 2020 22:44:29 +0100 Subject: [PATCH 4/5] win32, py3 [skip travis] --- testing/path/test_local.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/testing/path/test_local.py b/testing/path/test_local.py index 695acc4f..cadf1ec4 100644 --- a/testing/path/test_local.py +++ b/testing/path/test_local.py @@ -1104,7 +1104,13 @@ def test_behavior_with_bytes(): # type: () -> None p1 = local(b"") p1.sep = b"/" # type: ignore[assignment,misc] # noqa: F821 - assert p1 / brelpath == p1 / brelpath + if sys.version_info > (3,) and sys.platform == "win32": + with pytest.raises( + TypeError, match="a bytes-like object is required, not 'str'" + ): + p1 / brelpath + else: + assert p1 / brelpath == p1 / brelpath if sys.version_info < (3,): assert p1 / brelpath == local("") / local("bytesname") From 6d0db5e9183a65dca4c0e737e07aac534635b46a Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 31 Jan 2020 01:15:16 +0100 Subject: [PATCH 5/5] more win32 shenanigans --- testing/path/test_local.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/testing/path/test_local.py b/testing/path/test_local.py index cadf1ec4..20797006 100644 --- a/testing/path/test_local.py +++ b/testing/path/test_local.py @@ -1116,11 +1116,15 @@ def test_behavior_with_bytes(): # type: () -> None assert p1 / brelpath == local("") / local("bytesname") assert str(p1 / brelpath) == str(local("") / local("bytesname")) else: - assert p1 / brelpath != local("") / local("bytesname") - with pytest.raises( - TypeError, match=r"__str__ returned non-string \(type bytes\)" - ): - str(p1 / brelpath) - - strpath = (p1 / brelpath).strpath.decode() - assert strpath == str(local("") / local("bytesname")) + if sys.platform == "win32": + with pytest.raises(TypeError, "a bytes-like object is required, not 'str'"): + p1 / brelpath + else: + assert p1 / brelpath != local("") / local("bytesname") + with pytest.raises( + TypeError, match=r"__str__ returned non-string \(type bytes\)" + ): + str(p1 / brelpath) + + strpath = (p1 / brelpath).strpath.decode() + assert strpath == str(local("") / local("bytesname"))