"include/vscode:/vscode.git/clone" did not exist on "44a69f78cfddbe99f47a1d2061f477c9b2d7ba23"
Unverified Commit 6493f496 authored by Ralf W. Grosse-Kunstleve's avatar Ralf W. Grosse-Kunstleve Committed by GitHub
Browse files

Python 2 removal part 1: tests (C++ code is intentionally ~untouched) (#3688)



* `#error BYE_BYE_GOLDEN_SNAKE`

* Removing everything related to 2.7 from ci.yml

* Commenting-out Centos7

* Removing `PYTHON: 27` from .appveyor.yml

* "PY2" removal, mainly from tests. C++ code is not touched.

* Systematic removal of `u` prefix from `u"..."` and `u'...'` literals. Collateral cleanup of a couple minor other things.

* Cleaning up around case-insensitive hits for `[^a-z]py.*2` in tests/.

* Removing obsolete Python 2 mention in compiling.rst

* Proper `#error` for Python 2.

* Using PY_VERSION_HEX to guard `#error "PYTHON 2 IS NO LONGER SUPPORTED.`

* chore: bump pre-commit

* style: run pre-commit for pyupgrade 3+

* tests: use sys.version_info, not PY

* chore: more Python 2 removal

* Uncommenting Centos7 block (PR #3691 showed that it is working again).

* Update pre-commit hooks

* Fix pre-commit hook

* refactor: remove Python 2 from CMake

* refactor: remove Python 2 from setup code

* refactor: simplify, better static typing

* feat: fail with nice messages

* refactor: drop Python 2 C++ code

* docs: cleanup for Python 3

* revert: intree

revert: intree

* docs: minor touchup to py2 statement
Co-authored-by: default avatarHenry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: default avatarAaron Gokaslan <skylion.aaron@gmail.com>
parent 46dcd9bc
# -*- coding: utf-8 -*-
import pytest
from pybind11_tests import ConstructorStats
......@@ -201,7 +200,7 @@ def test_negative_stride_from_python(msg):
double_threer(): incompatible function arguments. The following argument types are supported:
1. (arg0: numpy.ndarray[numpy.float32[1, 3], flags.writeable]) -> None
Invoked with: """ # noqa: E501 line too long
Invoked with: """
+ repr(np.array([5.0, 4.0, 3.0], dtype="float32"))
)
......@@ -213,7 +212,7 @@ def test_negative_stride_from_python(msg):
double_threec(): incompatible function arguments. The following argument types are supported:
1. (arg0: numpy.ndarray[numpy.float32[3, 1], flags.writeable]) -> None
Invoked with: """ # noqa: E501 line too long
Invoked with: """
+ repr(np.array([7.0, 4.0, 1.0], dtype="float32"))
)
......@@ -724,13 +723,13 @@ def test_sparse_signature(doc):
doc(m.sparse_copy_r)
== """
sparse_copy_r(arg0: scipy.sparse.csr_matrix[numpy.float32]) -> scipy.sparse.csr_matrix[numpy.float32]
""" # noqa: E501 line too long
"""
)
assert (
doc(m.sparse_copy_c)
== """
sparse_copy_c(arg0: scipy.sparse.csc_matrix[numpy.float32]) -> scipy.sparse.csc_matrix[numpy.float32]
""" # noqa: E501 line too long
"""
)
......
......@@ -126,7 +126,6 @@ TEST_CASE("Override cache") {
TEST_CASE("Import error handling") {
REQUIRE_NOTHROW(py::module_::import("widget_module"));
REQUIRE_THROWS_WITH(py::module_::import("throw_exception"), "ImportError: C++ Error");
#if PY_VERSION_HEX >= 0x03030000
REQUIRE_THROWS_WITH(py::module_::import("throw_error_already_set"),
Catch::Contains("ImportError: initialization failed"));
......@@ -142,10 +141,6 @@ TEST_CASE("Import error handling") {
locals);
REQUIRE(locals["is_keyerror"].cast<bool>() == true);
REQUIRE(locals["message"].cast<std::string>() == "'missing'");
#else
REQUIRE_THROWS_WITH(py::module_::import("throw_error_already_set"),
Catch::Contains("ImportError: KeyError"));
#endif
}
TEST_CASE("There can be only one interpreter") {
......
# -*- coding: utf-8 -*-
import sys
from widget_module import Widget
......@@ -6,7 +5,7 @@ from widget_module import Widget
class DerivedWidget(Widget):
def __init__(self, message):
super(DerivedWidget, self).__init__(message)
super().__init__(message)
def the_answer(self):
return 42
......
# -*- coding: utf-8 -*-
import trampoline_module
......
# -*- coding: utf-8 -*-
import pytest
import env
from pybind11_tests import enums as m
......@@ -241,10 +239,7 @@ def test_char_underlying_enum(): # Issue #1331/PR #1334:
assert type(m.ScopedCharEnum.Positive.__int__()) is int
assert int(m.ScopedChar16Enum.Zero) == 0
assert hash(m.ScopedChar32Enum.Positive) == 1
if env.PY2:
assert m.ScopedCharEnum.Positive.__getstate__() == 1 # long
else:
assert type(m.ScopedCharEnum.Positive.__getstate__()) is int
assert type(m.ScopedCharEnum.Positive.__getstate__()) is int
assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
with pytest.raises(TypeError):
# Even if the underlying type is char, only an int can be used to construct the enum:
......@@ -255,10 +250,7 @@ def test_bool_underlying_enum():
assert type(m.ScopedBoolEnum.TRUE.__int__()) is int
assert int(m.ScopedBoolEnum.FALSE) == 0
assert hash(m.ScopedBoolEnum.TRUE) == 1
if env.PY2:
assert m.ScopedBoolEnum.TRUE.__getstate__() == 1 # long
else:
assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int
assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int
assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
# Enum could construct with a bool
# (bool is a strict subclass of int, and False will be converted to 0)
......
# -*- coding: utf-8 -*-
import os
import pytest
......@@ -18,7 +17,7 @@ def test_evals(capture):
assert m.test_eval_failure()
@pytest.mark.xfail("env.PYPY and not env.PY2", raises=RuntimeError)
@pytest.mark.xfail("env.PYPY", raises=RuntimeError)
def test_eval_file():
filename = os.path.join(os.path.dirname(__file__), "test_eval_call.py")
assert m.test_eval_file(filename)
......
# -*- coding: utf-8 -*-
# This file is called from 'test_eval.py'
if "call_test2" in locals():
......
......@@ -276,8 +276,6 @@ TEST_SUBMODULE(exceptions, m) {
m.def("throw_should_be_translated_to_key_error", []() { throw shared_exception(); });
#if PY_VERSION_HEX >= 0x03030000
m.def("raise_from", []() {
PyErr_SetString(PyExc_ValueError, "inner");
py::raise_from(PyExc_ValueError, "outer");
......@@ -301,5 +299,4 @@ TEST_SUBMODULE(exceptions, m) {
std::throw_with_nested(std::runtime_error("Outer Exception"));
}
});
#endif
}
# -*- coding: utf-8 -*-
import sys
import pytest
......@@ -24,7 +23,6 @@ def test_error_already_set(msg):
assert msg(excinfo.value) == "foo"
@pytest.mark.skipif("env.PY2")
def test_raise_from(msg):
with pytest.raises(ValueError) as excinfo:
m.raise_from()
......@@ -32,7 +30,6 @@ def test_raise_from(msg):
assert msg(excinfo.value.__cause__) == "inner"
@pytest.mark.skipif("env.PY2")
def test_raise_from_already_set(msg):
with pytest.raises(ValueError) as excinfo:
m.raise_from_already_set()
......@@ -102,7 +99,7 @@ def ignore_pytest_unraisable_warning(f):
@ignore_pytest_unraisable_warning
def test_python_alreadyset_in_destructor(monkeypatch, capsys):
hooked = False
triggered = [False] # mutable, so Python 2.7 closure can modify it
triggered = False
if hasattr(sys, "unraisablehook"): # Python 3.8+
hooked = True
......@@ -112,7 +109,8 @@ def test_python_alreadyset_in_destructor(monkeypatch, capsys):
def hook(unraisable_hook_args):
exc_type, exc_value, exc_tb, err_msg, obj = unraisable_hook_args
if obj == "already_set demo":
triggered[0] = True
nonlocal triggered
triggered = True
default_hook(unraisable_hook_args)
return
......@@ -121,11 +119,11 @@ def test_python_alreadyset_in_destructor(monkeypatch, capsys):
assert m.python_alreadyset_in_destructor("already_set demo") is True
if hooked:
assert triggered[0] is True
assert triggered is True
_, captured_stderr = capsys.readouterr()
# Error message is different in Python 2 and 3, check for words that appear in both
assert "ignored" in captured_stderr and "already_set demo" in captured_stderr
assert captured_stderr.startswith("Exception ignored in: 'already_set demo'")
assert captured_stderr.rstrip().endswith("KeyError: 'bar'")
def test_exception_matches():
......@@ -239,7 +237,6 @@ def test_nested_throws(capture):
assert str(excinfo.value) == "this is a helper-defined translated exception"
@pytest.mark.skipif("env.PY2")
def test_throw_nested_exception():
with pytest.raises(RuntimeError) as excinfo:
m.throw_nested_exception()
......@@ -249,7 +246,7 @@ def test_throw_nested_exception():
# This can often happen if you wrap a pybind11 class in a Python wrapper
def test_invalid_repr():
class MyRepr(object):
class MyRepr:
def __repr__(self):
raise AttributeError("Example error")
......
# -*- coding: utf-8 -*-
import re
import pytest
import env # noqa: F401
from pybind11_tests import ConstructorStats
from pybind11_tests import factory_constructors as m
from pybind11_tests.factory_constructors import tag
......@@ -82,7 +80,7 @@ def test_init_factory_signature(msg):
4. m.factory_constructors.TestFactory1(arg0: handle, arg1: int, arg2: handle)
Invoked with: 'invalid', 'constructor', 'arguments'
""" # noqa: E501 line too long
"""
)
assert (
......@@ -465,12 +463,10 @@ def test_reallocation_g(capture, msg):
)
@pytest.mark.skipif("env.PY2")
def test_invalid_self():
"""Tests invocation of the pybind-registered base class with an invalid `self` argument. You
can only actually do this on Python 3: Python 2 raises an exception itself if you try."""
"""Tests invocation of the pybind-registered base class with an invalid `self` argument."""
class NotPybindDerived(object):
class NotPybindDerived:
pass
# Attempts to initialize with an invalid type passed as `self`:
......
# -*- coding: utf-8 -*-
import multiprocessing
import threading
......
# -*- coding: utf-8 -*-
import sys
from contextlib import contextmanager
from contextlib import redirect_stderr, redirect_stdout
from io import StringIO
from pybind11_tests import iostream as m
try:
# Python 3
from io import StringIO
except ImportError:
# Python 2
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
try:
# Python 3.4
from contextlib import redirect_stdout
except ImportError:
@contextmanager
def redirect_stdout(target):
original = sys.stdout
sys.stdout = target
yield
sys.stdout = original
try:
# Python 3.5
from contextlib import redirect_stderr
except ImportError:
@contextmanager
def redirect_stderr(target):
original = sys.stderr
sys.stderr = target
yield
sys.stderr = original
def test_captured(capsys):
msg = "I've been redirected to Python, I hope!"
......
# -*- coding: utf-8 -*-
import pytest
import env # noqa: F401
from pybind11_tests import kwargs_and_defaults as m
......@@ -82,7 +80,7 @@ def test_mixed_args_and_kwargs(msg):
1. (arg0: int, arg1: float, *args) -> tuple
Invoked with: 1
""" # noqa: E501 line too long
"""
)
with pytest.raises(TypeError) as excinfo:
assert mpa()
......@@ -93,7 +91,7 @@ def test_mixed_args_and_kwargs(msg):
1. (arg0: int, arg1: float, *args) -> tuple
Invoked with:
""" # noqa: E501 line too long
"""
)
assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (
......@@ -127,7 +125,7 @@ def test_mixed_args_and_kwargs(msg):
1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
Invoked with: 1; kwargs: i=1
""" # noqa: E501 line too long
"""
)
with pytest.raises(TypeError) as excinfo:
assert mpakd(1, 2, j=1)
......@@ -138,7 +136,7 @@ def test_mixed_args_and_kwargs(msg):
1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
Invoked with: 1, 2; kwargs: j=1
""" # noqa: E501 line too long
"""
)
# Arguments after a py::args are automatically keyword-only (pybind 2.9+)
......@@ -343,7 +341,6 @@ def test_signatures():
)
@pytest.mark.xfail("env.PYPY and env.PY2", reason="PyPy2 doesn't double count")
def test_args_refcount():
"""Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
arguments"""
......
# -*- coding: utf-8 -*-
import pytest
import env # noqa: F401
......@@ -200,7 +199,7 @@ def test_stl_caster_vs_stl_bind(msg):
1. (arg0: pybind11_cross_module_tests.VectorInt) -> int
Invoked with: [1, 2, 3]
""" # noqa: E501 line too long
"""
)
......
# -*- coding: utf-8 -*-
import pytest
import env # noqa: F401
......@@ -494,26 +493,17 @@ def test_overload_ordering():
assert m.overload_order("string") == 1
assert m.overload_order(0) == 4
# Different for Python 2 vs. 3
uni_name = type(u"").__name__
assert "1. overload_order(arg0: int) -> int" in m.overload_order.__doc__
assert (
"2. overload_order(arg0: {}) -> int".format(uni_name)
in m.overload_order.__doc__
)
assert (
"3. overload_order(arg0: {}) -> int".format(uni_name)
in m.overload_order.__doc__
)
assert "2. overload_order(arg0: str) -> int" in m.overload_order.__doc__
assert "3. overload_order(arg0: str) -> int" in m.overload_order.__doc__
assert "4. overload_order(arg0: int) -> int" in m.overload_order.__doc__
with pytest.raises(TypeError) as err:
m.overload_order(1.1)
assert "1. (arg0: int) -> int" in str(err.value)
assert "2. (arg0: {}) -> int".format(uni_name) in str(err.value)
assert "3. (arg0: {}) -> int".format(uni_name) in str(err.value)
assert "2. (arg0: str) -> int" in str(err.value)
assert "3. (arg0: str) -> int" in str(err.value)
assert "4. (arg0: int) -> int" in str(err.value)
......
# -*- coding: utf-8 -*-
from pybind11_tests import ConstructorStats
from pybind11_tests import modules as m
from pybind11_tests.modules import subsubmodule as ms
......
# -*- coding: utf-8 -*-
import pytest
import env # noqa: F401
......@@ -13,8 +12,7 @@ def test_multiple_inheritance_cpp():
assert mt.bar() == 4
@pytest.mark.skipif("env.PYPY and env.PY2")
@pytest.mark.xfail("env.PYPY and not env.PY2")
@pytest.mark.xfail("env.PYPY")
def test_multiple_inheritance_mix1():
class Base1:
def __init__(self, i):
......@@ -53,15 +51,14 @@ def test_multiple_inheritance_mix2():
assert mt.bar() == 4
@pytest.mark.skipif("env.PYPY and env.PY2")
@pytest.mark.xfail("env.PYPY and not env.PY2")
@pytest.mark.xfail("env.PYPY")
def test_multiple_inheritance_python():
class MI1(m.Base1, m.Base2):
def __init__(self, i, j):
m.Base1.__init__(self, i)
m.Base2.__init__(self, j)
class B1(object):
class B1:
def v(self):
return 1
......@@ -96,7 +93,7 @@ def test_multiple_inheritance_python():
def v(self):
return 2
class B3(object):
class B3:
def v(self):
return 3
......
# -*- coding: utf-8 -*-
import pytest
import env # noqa: F401
......
# -*- coding: utf-8 -*-
import re
import pytest
......
# -*- coding: utf-8 -*-
import pytest
from pybind11_tests import numpy_vectorize as m
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment