Skip to content

Commit ebdd0d3

Browse files
tests: Consolidate version (2 vs. 3) and platform (CPython vs. PyPy) checks (#2376)
Fix logic in test_bytes_to_string Co-authored-by: Henry Schreiner <[email protected]>
1 parent cba4a98 commit ebdd0d3

6 files changed

+35
-35
lines changed

tests/conftest.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ def pytest_configure():
205205
from pybind11_tests.eigen import have_eigen
206206
except ImportError:
207207
have_eigen = False
208-
pypy = platform.python_implementation() == "PyPy"
208+
209+
# Provide simple `six`-like aliases.
210+
pytest.PY2 = (sys.version_info.major == 2)
211+
pytest.CPYTHON = (platform.python_implementation() == "CPython")
212+
pytest.PYPY = (platform.python_implementation() == "PyPy")
209213

210214
skipif = pytest.mark.skipif
211215
pytest.suppress = suppress
@@ -215,13 +219,13 @@ def pytest_configure():
215219
reason="eigen and/or numpy are not installed")
216220
pytest.requires_eigen_and_scipy = skipif(
217221
not have_eigen or not scipy, reason="eigen and/or scipy are not installed")
218-
pytest.unsupported_on_pypy = skipif(pypy, reason="unsupported on PyPy")
219-
pytest.bug_in_pypy = pytest.mark.xfail(pypy, reason="bug in PyPy")
220-
pytest.unsupported_on_pypy3 = skipif(pypy and sys.version_info.major >= 3,
222+
pytest.unsupported_on_pypy = skipif(pytest.PYPY, reason="unsupported on PyPy")
223+
pytest.bug_in_pypy = pytest.mark.xfail(pytest.PYPY, reason="bug in PyPy")
224+
pytest.unsupported_on_pypy3 = skipif(pytest.PYPY and not pytest.PY2,
221225
reason="unsupported on PyPy3")
222-
pytest.unsupported_on_pypy_lt_6 = skipif(pypy and sys.pypy_version_info[0] < 6,
226+
pytest.unsupported_on_pypy_lt_6 = skipif(pytest.PYPY and sys.pypy_version_info[0] < 6,
223227
reason="unsupported on PyPy<6")
224-
pytest.unsupported_on_py2 = skipif(sys.version_info.major < 3,
228+
pytest.unsupported_on_py2 = skipif(pytest.PY2,
225229
reason="unsupported on Python 2.x")
226230
pytest.gc_collect = gc_collect
227231

tests/test_buffers.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# -*- coding: utf-8 -*-
22
import io
33
import struct
4-
import sys
54

65
import pytest
76

87
from pybind11_tests import buffers as m
98
from pybind11_tests import ConstructorStats
109

11-
PY3 = sys.version_info[0] >= 3
12-
1310
pytestmark = pytest.requires_numpy
1411

1512
with pytest.suppress(ImportError):
@@ -98,22 +95,22 @@ def test_pointer_to_member_fn():
9895
def test_readonly_buffer():
9996
buf = m.BufferReadOnly(0x64)
10097
view = memoryview(buf)
101-
assert view[0] == 0x64 if PY3 else b'd'
98+
assert view[0] == b'd' if pytest.PY2 else 0x64
10299
assert view.readonly
103100

104101

105102
@pytest.unsupported_on_pypy
106103
def test_selective_readonly_buffer():
107104
buf = m.BufferReadOnlySelect()
108105

109-
memoryview(buf)[0] = 0x64 if PY3 else b'd'
106+
memoryview(buf)[0] = b'd' if pytest.PY2 else 0x64
110107
assert buf.value == 0x64
111108

112109
io.BytesIO(b'A').readinto(buf)
113110
assert buf.value == ord(b'A')
114111

115112
buf.readonly = True
116113
with pytest.raises(TypeError):
117-
memoryview(buf)[0] = 0 if PY3 else b'\0'
114+
memoryview(buf)[0] = b'\0' if pytest.PY2 else 0
118115
with pytest.raises(TypeError):
119116
io.BytesIO(b'1').readinto(buf)

tests/test_builtin_casters.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,19 @@ def test_bytes_to_string():
115115
"""Tests the ability to pass bytes to C++ string-accepting functions. Note that this is
116116
one-way: the only way to return bytes to Python is via the pybind11::bytes class."""
117117
# Issue #816
118-
import sys
119-
byte = bytes if sys.version_info[0] < 3 else str
120118

121-
assert m.strlen(byte("hi")) == 2
122-
assert m.string_length(byte("world")) == 5
123-
assert m.string_length(byte("a\x00b")) == 3
124-
assert m.strlen(byte("a\x00b")) == 1 # C-string limitation
119+
def to_bytes(s):
120+
if pytest.PY2:
121+
b = s
122+
else:
123+
b = s.encode("utf8")
124+
assert isinstance(b, bytes)
125+
return b
126+
127+
assert m.strlen(to_bytes("hi")) == 2
128+
assert m.string_length(to_bytes("world")) == 5
129+
assert m.string_length(to_bytes("a\x00b")) == 3
130+
assert m.strlen(to_bytes("a\x00b")) == 1 # C-string limitation
125131

126132
# passing in a utf8 encoded string should work
127133
assert m.string_length(u'💩'.encode("utf8")) == 4
@@ -187,12 +193,11 @@ def test_string_view(capture):
187193

188194
def test_integer_casting():
189195
"""Issue #929 - out-of-range integer values shouldn't be accepted"""
190-
import sys
191196
assert m.i32_str(-1) == "-1"
192197
assert m.i64_str(-1) == "-1"
193198
assert m.i32_str(2000000000) == "2000000000"
194199
assert m.u32_str(2000000000) == "2000000000"
195-
if sys.version_info < (3,):
200+
if pytest.PY2:
196201
assert m.i32_str(long(-1)) == "-1" # noqa: F821 undefined name 'long'
197202
assert m.i64_str(long(-1)) == "-1" # noqa: F821 undefined name 'long'
198203
assert m.i64_str(long(-999999999999)) == "-999999999999" # noqa: F821 undefined name
@@ -214,7 +219,7 @@ def test_integer_casting():
214219
m.i32_str(3000000000)
215220
assert "incompatible function arguments" in str(excinfo.value)
216221

217-
if sys.version_info < (3,):
222+
if pytest.PY2:
218223
with pytest.raises(TypeError) as excinfo:
219224
m.u32_str(long(-1)) # noqa: F821 undefined name 'long'
220225
assert "incompatible function arguments" in str(excinfo.value)

tests/test_kwargs_and_defaults.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
import pytest
33
from pybind11_tests import kwargs_and_defaults as m
44

5-
import platform
6-
import sys
7-
8-
pypy = platform.python_implementation() == "PyPy"
9-
105

116
def test_function_signatures(doc):
127
assert doc(m.kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
@@ -151,7 +146,7 @@ def test_keyword_only_args(msg):
151146
"""
152147

153148

154-
@pytest.mark.xfail(pypy and sys.version_info < (3, 0),
149+
@pytest.mark.xfail(pytest.PYPY and pytest.PY2,
155150
reason="PyPy2 doesn't seem to double count")
156151
def test_args_refcount():
157152
"""Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular

tests/test_pytypes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_bytes(doc):
113113
assert m.bytes_from_str().decode() == "bar"
114114

115115
assert doc(m.bytes_from_str) == "bytes_from_str() -> {}".format(
116-
"bytes" if sys.version_info[0] == 3 else "str"
116+
"str" if pytest.PY2 else "bytes"
117117
)
118118

119119

@@ -324,7 +324,7 @@ def test_memoryview(method, args, fmt, expected_view):
324324
view = method(*args)
325325
assert isinstance(view, memoryview)
326326
assert view.format == fmt
327-
if isinstance(expected_view, bytes) or sys.version_info[0] >= 3:
327+
if isinstance(expected_view, bytes) or not pytest.PY2:
328328
view_as_list = list(view)
329329
else:
330330
# Using max to pick non-zero byte (big-endian vs little-endian).
@@ -352,7 +352,7 @@ def test_memoryview_from_buffer_empty_shape():
352352
view = m.test_memoryview_from_buffer_empty_shape()
353353
assert isinstance(view, memoryview)
354354
assert view.format == 'B'
355-
if sys.version_info.major < 3:
355+
if pytest.PY2:
356356
# Python 2 behavior is weird, but Python 3 (the future) is fine.
357357
# PyPy3 has <memoryview, while CPython 2 has <memory
358358
assert bytes(view).startswith(b'<memory')
@@ -366,14 +366,14 @@ def test_test_memoryview_from_buffer_invalid_strides():
366366

367367

368368
def test_test_memoryview_from_buffer_nullptr():
369-
if sys.version_info.major < 3:
369+
if pytest.PY2:
370370
m.test_memoryview_from_buffer_nullptr()
371371
else:
372372
with pytest.raises(ValueError):
373373
m.test_memoryview_from_buffer_nullptr()
374374

375375

376-
@pytest.mark.skipif(sys.version_info.major < 3, reason='API not available')
376+
@pytest.unsupported_on_py2
377377
def test_memoryview_from_memory():
378378
view = m.test_memoryview_from_memory()
379379
assert isinstance(view, memoryview)

tests/test_stl_binders.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
import pytest
3-
import sys
43
from pybind11_tests import stl_binders as m
54

65
with pytest.suppress(ImportError):
@@ -77,15 +76,15 @@ def test_vector_buffer():
7776
assert v[1] == 2
7877
v[2] = 5
7978
mv = memoryview(v) # We expose the buffer interface
80-
if sys.version_info.major > 2:
79+
if not pytest.PY2:
8180
assert mv[2] == 5
8281
mv[2] = 6
8382
else:
8483
assert mv[2] == '\x05'
8584
mv[2] = '\x06'
8685
assert v[2] == 6
8786

88-
if sys.version_info.major > 2:
87+
if not pytest.PY2:
8988
mv = memoryview(b)
9089
v = m.VectorUChar(mv[::2])
9190
assert v[1] == 3

0 commit comments

Comments
 (0)