Skip to content

Commit

Permalink
tests: Consolidate version (2 vs. 3) and platform (CPython vs. PyPy) …
Browse files Browse the repository at this point in the history
…checks
  • Loading branch information
EricCousineau-TRI committed Aug 9, 2020
1 parent 0af7fe6 commit 2c81360
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
24 changes: 18 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,19 @@ 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.PY3 = (sys.version_info.major == 3)
pytest.CPYTHON = (platform.python_implementation == "CPython")
pytest.PYPY = (platform.python_implementation() == "PyPy")

if not pytest.PY2 and not pytest.PY3:
print("pybind11 is only supported on Python 2.x or 3.x")
sys.exit(1)
if not pytest.CPYTHON and not pytest.PYPY:
print("pybind11 is only supported for CPython or PyPy")
sys.exit(1)

skipif = pytest.mark.skipif
pytest.suppress = suppress
Expand All @@ -215,13 +227,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 pytest.PY3,
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] == 0x64 if pytest.PY3 else b'd'
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] = 0x64 if pytest.PY3 else b'd'
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] = 0 if pytest.PY3 else b'\0'
with pytest.raises(TypeError):
io.BytesIO(b'1').readinto(buf)
16 changes: 7 additions & 9 deletions tests/test_builtin_casters.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,12 @@ 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
bytes_ = bytes if pytest.PY2 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
assert m.strlen(bytes_("hi")) == 2
assert m.string_length(bytes_("world")) == 5
assert m.string_length(bytes_("a\x00b")) == 3
assert m.strlen(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 +186,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 +212,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"
"bytes" if pytest.PY3 else "str"
)


Expand Down Expand Up @@ -292,7 +292,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 pytest.PY3:
view_as_list = list(view)
else:
# Using max to pick non-zero byte (big-endian vs little-endian).
Expand Down Expand Up @@ -320,7 +320,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 @@ -334,14 +334,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
3 changes: 1 addition & 2 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,7 +76,7 @@ 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 pytest.PY3:
assert mv[2] == 5
mv[2] = 6
else:
Expand Down

0 comments on commit 2c81360

Please sign in to comment.