Skip to content

tests: Consolidate version (2 vs. 3) and platform (CPython vs. PyPy) checks #2376

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ def pytest_configure():
from pybind11_tests.eigen import have_eigen
except ImportError:
have_eigen = False
pypy = platform.python_implementation() == "PyPy"

# Provide simple `six`-like aliases.
pytest.PY2 = (sys.version_info.major == 2)
pytest.CPYTHON = (platform.python_implementation() == "CPython")
pytest.PYPY = (platform.python_implementation() == "PyPy")

skipif = pytest.mark.skipif
pytest.suppress = suppress
Expand All @@ -215,13 +219,13 @@ def pytest_configure():
reason="eigen and/or numpy are not installed")
pytest.requires_eigen_and_scipy = skipif(
not have_eigen or not scipy, reason="eigen and/or scipy are not installed")
pytest.unsupported_on_pypy = skipif(pypy, reason="unsupported on PyPy")
pytest.bug_in_pypy = pytest.mark.xfail(pypy, reason="bug in PyPy")
pytest.unsupported_on_pypy3 = skipif(pypy and sys.version_info.major >= 3,
pytest.unsupported_on_pypy = skipif(pytest.PYPY, reason="unsupported on PyPy")
pytest.bug_in_pypy = pytest.mark.xfail(pytest.PYPY, reason="bug in PyPy")
pytest.unsupported_on_pypy3 = skipif(pytest.PYPY and not pytest.PY2,
reason="unsupported on PyPy3")
pytest.unsupported_on_pypy_lt_6 = skipif(pypy and sys.pypy_version_info[0] < 6,
pytest.unsupported_on_pypy_lt_6 = skipif(pytest.PYPY and sys.pypy_version_info[0] < 6,
reason="unsupported on PyPy<6")
pytest.unsupported_on_py2 = skipif(sys.version_info.major < 3,
pytest.unsupported_on_py2 = skipif(pytest.PY2,
reason="unsupported on Python 2.x")
pytest.gc_collect = gc_collect

Expand Down
9 changes: 3 additions & 6 deletions tests/test_buffers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# -*- coding: utf-8 -*-
import io
import struct
import sys

import pytest

from pybind11_tests import buffers as m
from pybind11_tests import ConstructorStats

PY3 = sys.version_info[0] >= 3

pytestmark = pytest.requires_numpy

with pytest.suppress(ImportError):
Expand Down Expand Up @@ -98,22 +95,22 @@ def test_pointer_to_member_fn():
def test_readonly_buffer():
buf = m.BufferReadOnly(0x64)
view = memoryview(buf)
assert view[0] == 0x64 if PY3 else b'd'
assert view[0] == b'd' if pytest.PY2 else 0x64
assert view.readonly


@pytest.unsupported_on_pypy
def test_selective_readonly_buffer():
buf = m.BufferReadOnlySelect()

memoryview(buf)[0] = 0x64 if PY3 else b'd'
memoryview(buf)[0] = b'd' if pytest.PY2 else 0x64
assert buf.value == 0x64

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

buf.readonly = True
with pytest.raises(TypeError):
memoryview(buf)[0] = 0 if PY3 else b'\0'
memoryview(buf)[0] = b'\0' if pytest.PY2 else 0
with pytest.raises(TypeError):
io.BytesIO(b'1').readinto(buf)
23 changes: 14 additions & 9 deletions tests/test_builtin_casters.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,19 @@ def test_bytes_to_string():
"""Tests the ability to pass bytes to C++ string-accepting functions. Note that this is
one-way: the only way to return bytes to Python is via the pybind11::bytes class."""
# Issue #816
import sys
byte = bytes if sys.version_info[0] < 3 else str

assert m.strlen(byte("hi")) == 2
assert m.string_length(byte("world")) == 5
assert m.string_length(byte("a\x00b")) == 3
assert m.strlen(byte("a\x00b")) == 1 # C-string limitation
def to_bytes(s):
if pytest.PY2:
b = s
else:
b = s.encode("utf8")
assert isinstance(b, bytes)
return b

assert m.strlen(to_bytes("hi")) == 2
assert m.string_length(to_bytes("world")) == 5
assert m.string_length(to_bytes("a\x00b")) == 3
assert m.strlen(to_bytes("a\x00b")) == 1 # C-string limitation

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

def test_integer_casting():
"""Issue #929 - out-of-range integer values shouldn't be accepted"""
import sys
assert m.i32_str(-1) == "-1"
assert m.i64_str(-1) == "-1"
assert m.i32_str(2000000000) == "2000000000"
assert m.u32_str(2000000000) == "2000000000"
if sys.version_info < (3,):
if pytest.PY2:
assert m.i32_str(long(-1)) == "-1" # noqa: F821 undefined name 'long'
assert m.i64_str(long(-1)) == "-1" # noqa: F821 undefined name 'long'
assert m.i64_str(long(-999999999999)) == "-999999999999" # noqa: F821 undefined name
Expand All @@ -214,7 +219,7 @@ def test_integer_casting():
m.i32_str(3000000000)
assert "incompatible function arguments" in str(excinfo.value)

if sys.version_info < (3,):
if pytest.PY2:
with pytest.raises(TypeError) as excinfo:
m.u32_str(long(-1)) # noqa: F821 undefined name 'long'
assert "incompatible function arguments" in str(excinfo.value)
Expand Down
7 changes: 1 addition & 6 deletions tests/test_kwargs_and_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
import pytest
from pybind11_tests import kwargs_and_defaults as m

import platform
import sys

pypy = platform.python_implementation() == "PyPy"


def test_function_signatures(doc):
assert doc(m.kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
Expand Down Expand Up @@ -151,7 +146,7 @@ def test_keyword_only_args(msg):
"""


@pytest.mark.xfail(pypy and sys.version_info < (3, 0),
@pytest.mark.xfail(pytest.PYPY and pytest.PY2,
reason="PyPy2 doesn't seem to double count")
def test_args_refcount():
"""Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
Expand Down
10 changes: 5 additions & 5 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_bytes(doc):
assert m.bytes_from_str().decode() == "bar"

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


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


def test_test_memoryview_from_buffer_nullptr():
if sys.version_info.major < 3:
if pytest.PY2:
m.test_memoryview_from_buffer_nullptr()
else:
with pytest.raises(ValueError):
m.test_memoryview_from_buffer_nullptr()


@pytest.mark.skipif(sys.version_info.major < 3, reason='API not available')
@pytest.unsupported_on_py2
def test_memoryview_from_memory():
view = m.test_memoryview_from_memory()
assert isinstance(view, memoryview)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_stl_binders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
import pytest
import sys
from pybind11_tests import stl_binders as m

with pytest.suppress(ImportError):
Expand Down Expand Up @@ -77,15 +76,15 @@ def test_vector_buffer():
assert v[1] == 2
v[2] = 5
mv = memoryview(v) # We expose the buffer interface
if sys.version_info.major > 2:
if not pytest.PY2:
assert mv[2] == 5
mv[2] = 6
else:
assert mv[2] == '\x05'
mv[2] = '\x06'
assert v[2] == 6

if sys.version_info.major > 2:
if not pytest.PY2:
mv = memoryview(b)
v = m.VectorUChar(mv[::2])
assert v[1] == 3
Expand Down