Skip to content

Commit 134f3e7

Browse files
tests: Consolidate version (2 vs. 3) and platform (CPython vs. PyPy) checks
1 parent 0af7fe6 commit 134f3e7

6 files changed

+35
-34
lines changed

tests/conftest.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,19 @@ 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.PY3 = (sys.version_info.major == 3)
212+
pytest.CPYTHON = (platform.python_implementation() == "CPython")
213+
pytest.PYPY = (platform.python_implementation() == "PyPy")
214+
215+
if not pytest.PY2 and not pytest.PY3:
216+
print("pybind11 is only supported on Python 2.x or 3.x")
217+
sys.exit(1)
218+
if not pytest.CPYTHON and not pytest.PYPY:
219+
print("pybind11 is only supported for CPython or PyPy")
220+
sys.exit(1)
209221

210222
skipif = pytest.mark.skipif
211223
pytest.suppress = suppress
@@ -215,13 +227,13 @@ def pytest_configure():
215227
reason="eigen and/or numpy are not installed")
216228
pytest.requires_eigen_and_scipy = skipif(
217229
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,
230+
pytest.unsupported_on_pypy = skipif(pytest.PYPY, reason="unsupported on PyPy")
231+
pytest.bug_in_pypy = pytest.mark.xfail(pytest.PYPY, reason="bug in PyPy")
232+
pytest.unsupported_on_pypy3 = skipif(pytest.PYPY and pytest.PY3,
221233
reason="unsupported on PyPy3")
222-
pytest.unsupported_on_pypy_lt_6 = skipif(pypy and sys.pypy_version_info[0] < 6,
234+
pytest.unsupported_on_pypy_lt_6 = skipif(pytest.PYPY and sys.pypy_version_info[0] < 6,
223235
reason="unsupported on PyPy<6")
224-
pytest.unsupported_on_py2 = skipif(sys.version_info.major < 3,
236+
pytest.unsupported_on_py2 = skipif(pytest.PY2,
225237
reason="unsupported on Python 2.x")
226238
pytest.gc_collect = gc_collect
227239

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] == 0x64 if pytest.PY3 else b'd'
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] = 0x64 if pytest.PY3 else b'd'
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] = 0 if pytest.PY3 else b'\0'
118115
with pytest.raises(TypeError):
119116
io.BytesIO(b'1').readinto(buf)

tests/test_builtin_casters.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,12 @@ 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
118+
bytes_ = bytes if pytest.PY2 else str
120119

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
120+
assert m.strlen(bytes_("hi")) == 2
121+
assert m.string_length(bytes_("world")) == 5
122+
assert m.string_length(bytes_("a\x00b")) == 3
123+
assert m.strlen(bytes_("a\x00b")) == 1 # C-string limitation
125124

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

188187
def test_integer_casting():
189188
"""Issue #929 - out-of-range integer values shouldn't be accepted"""
190-
import sys
191189
assert m.i32_str(-1) == "-1"
192190
assert m.i64_str(-1) == "-1"
193191
assert m.i32_str(2000000000) == "2000000000"
194192
assert m.u32_str(2000000000) == "2000000000"
195-
if sys.version_info < (3,):
193+
if pytest.PY2:
196194
assert m.i32_str(long(-1)) == "-1" # noqa: F821 undefined name 'long'
197195
assert m.i64_str(long(-1)) == "-1" # noqa: F821 undefined name 'long'
198196
assert m.i64_str(long(-999999999999)) == "-999999999999" # noqa: F821 undefined name
@@ -214,7 +212,7 @@ def test_integer_casting():
214212
m.i32_str(3000000000)
215213
assert "incompatible function arguments" in str(excinfo.value)
216214

217-
if sys.version_info < (3,):
215+
if pytest.PY2:
218216
with pytest.raises(TypeError) as excinfo:
219217
m.u32_str(long(-1)) # noqa: F821 undefined name 'long'
220218
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+
"bytes" if pytest.PY3 else "str"
117117
)
118118

119119

@@ -292,7 +292,7 @@ def test_memoryview(method, args, fmt, expected_view):
292292
view = method(*args)
293293
assert isinstance(view, memoryview)
294294
assert view.format == fmt
295-
if isinstance(expected_view, bytes) or sys.version_info[0] >= 3:
295+
if isinstance(expected_view, bytes) or pytest.PY3:
296296
view_as_list = list(view)
297297
else:
298298
# Using max to pick non-zero byte (big-endian vs little-endian).
@@ -320,7 +320,7 @@ def test_memoryview_from_buffer_empty_shape():
320320
view = m.test_memoryview_from_buffer_empty_shape()
321321
assert isinstance(view, memoryview)
322322
assert view.format == 'B'
323-
if sys.version_info.major < 3:
323+
if pytest.PY2:
324324
# Python 2 behavior is weird, but Python 3 (the future) is fine.
325325
# PyPy3 has <memoryview, while CPython 2 has <memory
326326
assert bytes(view).startswith(b'<memory')
@@ -334,14 +334,14 @@ def test_test_memoryview_from_buffer_invalid_strides():
334334

335335

336336
def test_test_memoryview_from_buffer_nullptr():
337-
if sys.version_info.major < 3:
337+
if pytest.PY2:
338338
m.test_memoryview_from_buffer_nullptr()
339339
else:
340340
with pytest.raises(ValueError):
341341
m.test_memoryview_from_buffer_nullptr()
342342

343343

344-
@pytest.mark.skipif(sys.version_info.major < 3, reason='API not available')
344+
@pytest.unsupported_on_py2
345345
def test_memoryview_from_memory():
346346
view = m.test_memoryview_from_memory()
347347
assert isinstance(view, memoryview)

tests/test_stl_binders.py

Lines changed: 1 addition & 2 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,7 +76,7 @@ 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 pytest.PY3:
8180
assert mv[2] == 5
8281
mv[2] = 6
8382
else:

0 commit comments

Comments
 (0)