Merge branch 'master' into sh_merge_master3

This commit is contained in:
Ralf W. Grosse-Kunstleve
2023-02-07 23:54:04 -08:00
18 changed files with 77 additions and 49 deletions

View File

@@ -11,11 +11,17 @@ import multiprocessing
import os
import re
import textwrap
import traceback
import pytest
# Early diagnostic for failed imports
import pybind11_tests
try:
import pybind11_tests
except Exception:
# pytest does not show the traceback without this.
traceback.print_exc()
raise
@pytest.fixture(scope="session", autouse=True)

View File

@@ -143,7 +143,6 @@ def normalize_line_endings(value: bytes) -> bytes:
def test_build_sdist(monkeypatch, tmpdir):
monkeypatch.chdir(MAIN_DIR)
subprocess.run(
@@ -194,7 +193,6 @@ def test_build_sdist(monkeypatch, tmpdir):
def test_build_global_dist(monkeypatch, tmpdir):
monkeypatch.chdir(MAIN_DIR)
monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
subprocess.run(

View File

@@ -7,7 +7,6 @@ from pybind11_tests import chrono as m
def test_chrono_system_clock():
# Get the time from both c++ and datetime
date0 = datetime.datetime.today()
date1 = m.test_chrono1()
@@ -122,7 +121,6 @@ def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch):
def test_chrono_duration_roundtrip():
# Get the difference between two times (a timedelta)
date1 = datetime.datetime.today()
date2 = datetime.datetime.today()
@@ -143,7 +141,6 @@ def test_chrono_duration_roundtrip():
def test_chrono_duration_subtraction_equivalence():
date1 = datetime.datetime.today()
date2 = datetime.datetime.today()
@@ -154,7 +151,6 @@ def test_chrono_duration_subtraction_equivalence():
def test_chrono_duration_subtraction_equivalence_date():
date1 = datetime.date.today()
date2 = datetime.date.today()

View File

@@ -185,7 +185,6 @@ def test_inheritance(msg):
def test_inheritance_init(msg):
# Single base
class Python(m.Pet):
def __init__(self):

View File

@@ -94,7 +94,8 @@ def test_noconvert_args(msg):
def test_custom_caster_destruction():
"""Tests that returning a pointer to a type that gets converted with a custom type caster gets
destroyed when the function has py::return_value_policy::take_ownership policy applied."""
destroyed when the function has py::return_value_policy::take_ownership policy applied.
"""
cstats = m.destruction_tester_cstats()
# This one *doesn't* have take_ownership: the pointer should be used but not destroyed:

View File

@@ -59,7 +59,6 @@ def assert_equal_tensor_ref(mat, writeable=True, modified=None):
@pytest.mark.parametrize("m", submodules)
@pytest.mark.parametrize("member_name", ["member", "member_view"])
def test_reference_internal(m, member_name):
if not hasattr(sys, "getrefcount"):
pytest.skip("No reference counting")
foo = m.CustomExample()
@@ -108,7 +107,6 @@ def test_convert_tensor_to_py(m, func_name):
@pytest.mark.parametrize("m", submodules)
def test_bad_cpp_to_python_casts(m):
with pytest.raises(
RuntimeError, match="Cannot use reference internal when there is no parent"
):
@@ -131,7 +129,6 @@ def test_bad_cpp_to_python_casts(m):
@pytest.mark.parametrize("m", submodules)
def test_bad_python_to_cpp_casts(m):
with pytest.raises(
TypeError, match=r"^round_trip_tensor\(\): incompatible function arguments"
):
@@ -194,7 +191,6 @@ def test_bad_python_to_cpp_casts(m):
@pytest.mark.parametrize("m", submodules)
def test_references_actually_refer(m):
a = m.reference_tensor()
temp = a[indices]
a[indices] = 100
@@ -211,7 +207,6 @@ def test_references_actually_refer(m):
@pytest.mark.parametrize("m", submodules)
def test_round_trip(m):
assert_equal_tensor_ref(m.round_trip_tensor(tensor_ref))
with pytest.raises(TypeError, match="^Cannot cast array data from"):
@@ -260,7 +255,6 @@ def test_round_trip(m):
@pytest.mark.parametrize("m", submodules)
def test_round_trip_references_actually_refer(m):
# Need to create a copy that matches the type on the C side
copy = np.array(tensor_ref, dtype=np.float64, order=m.needed_options)
a = m.round_trip_view_tensor(copy)

View File

@@ -184,7 +184,7 @@ TEST_CASE("Custom PyConfig") {
py::initialize_interpreter();
}
TEST_CASE("Custom PyConfig with argv") {
TEST_CASE("scoped_interpreter with PyConfig_InitIsolatedConfig and argv") {
py::finalize_interpreter();
{
PyConfig config;
@@ -199,6 +199,26 @@ TEST_CASE("Custom PyConfig with argv") {
}
py::initialize_interpreter();
}
TEST_CASE("scoped_interpreter with PyConfig_InitPythonConfig and argv") {
py::finalize_interpreter();
{
PyConfig config;
PyConfig_InitPythonConfig(&config);
// `initialize_interpreter() overrides the default value for config.parse_argv (`1`) by
// changing it to `0`. This test exercises `scoped_interpreter` with the default config.
char *argv[] = {strdup("a.out"), strdup("arg1")};
py::scoped_interpreter argv_scope(&config, 2, argv);
std::free(argv[0]);
std::free(argv[1]);
auto module = py::module::import("test_interpreter");
auto py_widget = module.attr("DerivedWidget")("The question");
const auto &cpp_widget = py_widget.cast<const Widget &>();
REQUIRE(cpp_widget.argv0() == "arg1");
}
py::initialize_interpreter();
}
#endif
TEST_CASE("Add program dir to path pre-PyConfig") {

View File

@@ -130,7 +130,8 @@ def test_stl_bind_global():
def test_mixed_local_global():
"""Local types take precedence over globally registered types: a module with a `module_local`
type can be registered even if the type is already registered globally. With the module,
casting will go to the local type; outside the module casting goes to the global type."""
casting will go to the local type; outside the module casting goes to the global type.
"""
import pybind11_cross_module_tests as cm
m.register_mixed_global()

View File

@@ -107,11 +107,10 @@ def test_def_submodule_failures():
sm_name_orig = sm.__name__
sm.__name__ = malformed_utf8
try:
with pytest.raises(Exception):
# Seen with Python 3.9: SystemError: nameless module
# But we do not want to exercise the internals of PyModule_GetName(), which could
# change in future versions of Python, but a bad __name__ is very likely to cause
# some kind of failure indefinitely.
# We want to assert that a bad __name__ causes some kind of failure, although we do not want to exercise
# the internals of PyModule_GetName(). Currently all supported Python versions raise SystemError. If that
# changes in future Python versions, simply add the new expected exception types here.
with pytest.raises(SystemError):
m.def_submodule(sm, b"SubSubModuleName")
finally:
# Clean up to ensure nothing gets upset by a module with an invalid __name__.

View File

@@ -130,7 +130,6 @@ def test_nested():
def test_overriding_eq_reset_hash():
assert m.Comparable(15) is not m.Comparable(15)
assert m.Comparable(15) == m.Comparable(15)