mirror of
https://github.com/pybind/pybind11.git
synced 2025-03-04 05:23:21 +00:00
* Insert type_caster_odr_guard<> (an empty struct to start with). * Add odr_guard_registry() used in type_caster_odr_guard() default constructor. * Add minimal_real_caster (from PR #3862) to test_async, test_buffers * VERY MESSY SNAPSHOT of WIP, this was the starting point for cl/454658864, which has more changes on top. * Restore original test_async, test_buffers from current smart_holder HEAD * Copy from cl/454991845 snapshot Jun 14, 5:08 PM * Cleanup of tests. Systematically insert `if (make_caster<T>::translation_unit_local) {` * Small simplification of odr_guard_impl() * WIP * Add PYBIND11_SOURCE_FILE_LINE macro. * Replace PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER with PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE, baked into PYBIND11_TYPE_CASTER macro. * Add more PYBIND11_DETAIL_TYPE_CASTER_ACCESS_TRANSLATION_UNIT_LOCAL; resolves "unused" warning when compiling test_custom_type_casters.cpp * load_type fixes & follow-on cleanup * Strip ./ from source_file_line * Add new tests to CMakeLists.txt, disable PYBIND11_WERROR * Replace C++17 syntax. Compiles with Debian clang 13 C++11 mode, but fails to link. Trying GitHub Actions anyway to see if there are any platforms that support https://en.cppreference.com/w/cpp/language/tu_local before C++20. Note that Debian clang 13 C++17 works locally. * Show C++ version along with ODR VIOLATION DETECTED message. * Add source_file_line_basename() * Introduce PYBIND11_TYPE_CASTER_ODR_GUARD_ON (but not set automatically). * Minor cleanup. * Set PYBIND11_TYPE_CASTER_ODR_GUARD_ON automatically. * Resolve clang-tidy error. * Compatibility with old compilers. * Fix off-by-one in source_file_line_basename() * Report PYBIND11_INTERNALS_ID & C++ Version from pytest_configure() * Restore use of PYBIND11_WERROR * Move cpp_version_in_use() from cast.h to pybind11_tests.cpp * define PYBIND11_DETAIL_ODR_GUARD_IMPL_THROW_DISABLED true in test_odr_guard_1,2.cpp * IWYU cleanup of detail/type_caster_odr_guard.h * Replace `throw err;` to resolve clang-tidy error. * Add new header filename to CMakeLists.txt, test_files.py * Experiment: Try any C++17 compiler. * Fix ifdef for pragma GCC diagnostic. * type_caster_odr_guard_impl() cleanup * Move type_caster_odr_guard to type_caster_odr_guard.h * Rename test_odr_guard* to test_type_caster_odr_guard* * Remove comments that are (now) more distracting than helpful. * Mark tu_local_no_data_always_false operator bool as explicit (clang-tidy). See also: https://stackoverflow.com/questions/39995573/when-can-i-use-explicit-operator-bool-without-a-cast * New PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT option (current on by default). * Add test_type_caster_odr_registry_values(), test_type_caster_odr_violation_detected_counter() * Report UNEXPECTED: test_type_caster_odr_guard_2.cpp prevailed (but do not fail). * Apply clang-tidy suggestion. * Attempt to handle valgrind behavior. * Another attempt to handle valgrind behavior. * Yet another attempt to handle valgrind behavior. * Trying a new direction: show compiler info & std for UNEXPECTED: type_caster_odr_violation_detected_count() == 0 * compiler_info MSVC fix. num_violations == 0 condition. * assert pybind11_tests.compiler_info is not None * Introduce `make_caster_intrinsic<T>`, to be able to undo the 2 changes from `load_type` to `load_type<T>`. This is to avoid breaking 2 `pybind11::detail::load_type()` calls found in the wild (Google global testing). One of the breakages in the wild was:0f0f600767/python/tensorstore/subscript_method.h (L61)
* Add test for stl.h / stl_bind.h mix. Manually verified that the ODR guard detects the ODR violation: ``` C++ Info: Debian Clang 13.0.1 C++17 __pybind11_internals_v4_clang_libstdcpp_cxxabi1002_sh_def__ =========================================================== test session starts ============================================================ platform linux -- Python 3.9.12, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- /usr/bin/python3 ... ================================================================= FAILURES ================================================================= _____________________________________________ test_type_caster_odr_violation_detected_counter ______________________________________________ def test_type_caster_odr_violation_detected_counter(): ... else: > assert num_violations == 1 E assert 2 == 1 E +2 E -1 num_violations = 2 test_type_caster_odr_guard_1.py:51: AssertionError ========================================================= short test summary info ========================================================== FAILED test_type_caster_odr_guard_1.py::test_type_caster_odr_violation_detected_counter - assert 2 == 1 ======================================================= 1 failed, 5 passed in 0.08s ======================================================== ``` * Eliminate need for `PYBIND11_DETAIL_TYPE_CASTER_ACCESS_TRANSLATION_UNIT_LOCAL` macro. Copying code first developed by @amauryfa. I tried this at an earlier stage, but by itself this was insufficient. In the meantime I added in the TU-local mechanisms: trying again. Passes local testing: ``` DISABLED std::system_error: ODR VIOLATION DETECTED: pybind11::detail::type_caster<mrc_ns::type_mrc>: SourceLocation1="/usr/local/google/home/rwgk/forked/pybind11/tests/test_type_caster_odr_guard_1.cpp:18", SourceLocation2="/usr/local/google/home/rwgk/forked/pybind11/tests/test_type_caster_odr_guard_2.cpp:19" C++ Info: Debian Clang 13.0.1 C++17 __pybind11_internals_v4_clang_libstdcpp_cxxabi1002_sh_def__ =========================================================== test session starts ============================================================ platform linux -- Python 3.9.12, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- /usr/bin/python3 cachedir: .pytest_cache rootdir: /usr/local/google/home/rwgk/forked/pybind11/tests, configfile: pytest.ini collected 6 items test_type_caster_odr_guard_1.py::test_type_mrc_to_python PASSED test_type_caster_odr_guard_1.py::test_type_mrc_from_python PASSED test_type_caster_odr_guard_1.py::test_type_caster_odr_registry_values PASSED test_type_caster_odr_guard_1.py::test_type_caster_odr_violation_detected_counter PASSED test_type_caster_odr_guard_2.py::test_type_mrc_to_python PASSED test_type_caster_odr_guard_2.py::test_type_mrc_from_python PASSED ============================================================ 6 passed in 0.01s ============================================================= ``` * tu_local_descr with src_loc experiment * clang-tidy suggested fixes * Use source_file_line_from_sloc in type_caster_odr_guard_registry * Disable type_caster ODR guard for __INTEL_COMPILER (see comment). Also turn off printf. * Add missing include (discovered via google-internal testing). * Work `scr_loc` into `descr` * Use `TypeCasterType::name.sloc` instead of `source_file_line.sloc` Manual re-verification: ``` +++ b/tests/test_type_caster_odr_guard_2.cpp - // m.def("pass_vector_type_mrc", mrc_ns::pass_vector_type_mrc); + m.def("pass_vector_type_mrc", mrc_ns::pass_vector_type_mrc); ``` ``` > assert num_violations == 1 E assert 2 == 1 num_violations = 2 test_type_caster_odr_guard_1.py:51: AssertionError ``` * Fix small oversight (src_loc::here() -> src_loc{nullptr, 0}). * Remove PYBIND11_DETAIL_TYPE_CASTER_ACCESS_TRANSLATION_UNIT_LOCAL macro completely. * Remove PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE macro completely. Some small extra cleanup. * Minor tweaks looking at the PR with a fresh eye. * src_loc comments * Add new test_descr_src_loc & and fix descr.h `concat()` `src_loc` bug discovered while working on the test. * Some more work on source code comments. * Fully document the ODR violations in the ODR guard itself and introduce `PYBIND11_TYPE_CASTER_ODR_GUARD_ON_IF_AVAILABLE` * Update comment (incl. mention of deadsnakes known to not work as intended). * Use no-destructor idiom for type_caster_odr_guard_registry, as suggested by @laramiel * Fix clang-tidy error: 'auto reg' can be declared as 'auto *reg' [readability-qualified-auto,-warnings-as-errors] * WIP * Revert "WIP" (tu_local_no_data_always_false_base experiment). This reverts commit31e8ac562f
. * Change `PYBIND11_TYPE_CASTER_ODR_GUARD_ON` to `PYBIND11_ENABLE_TYPE_CASTER_ODR_GUARD`, based on a suggestion by @rainwoodman * Improved `#if` determining `PYBIND11_ENABLE_TYPE_CASTER_ODR_GUARD`, based on suggestion by @laramiel * Make `descr::sloc` `const`, as suggested by @rainwoodman * Rename macro to `PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_DEBUG`, as suggested by @laramiel * Tweak comments some more (add "white hat hacker" analogy). * Bring back `PYBIND11_CPP17` in determining `PYBIND11_ENABLE_TYPE_CASTER_ODR_GUARD`, to hopefully resolve most if not all of the many CI failures (89 failing, 32 successful: https://github.com/pybind/pybind11/runs/7430295771). * Try another workaround for `__has_builtin`-related breakages (https://github.com/pybind/pybind11/runs/7430720321). * Remove `defined(__has_builtin)` and subconditions. * Update "known to not work" expectation in test and comment. * `pytest.skip` `num_violations == 0` only `#ifdef __NO_INLINE__` (irrespective of the compiler) * Systematically change all new `#ifdef` to `#if defined` (review suggestion). * Bring back MSVC comment that got lost while experimenting.
283 lines
7.7 KiB
Python
283 lines
7.7 KiB
Python
import contextlib
|
|
import os
|
|
import string
|
|
import subprocess
|
|
import sys
|
|
import tarfile
|
|
import zipfile
|
|
|
|
# These tests must be run explicitly
|
|
# They require CMake 3.15+ (--install)
|
|
|
|
DIR = os.path.abspath(os.path.dirname(__file__))
|
|
MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
|
|
|
|
|
|
main_headers = {
|
|
"include/pybind11/attr.h",
|
|
"include/pybind11/buffer_info.h",
|
|
"include/pybind11/cast.h",
|
|
"include/pybind11/chrono.h",
|
|
"include/pybind11/common.h",
|
|
"include/pybind11/complex.h",
|
|
"include/pybind11/eigen.h",
|
|
"include/pybind11/embed.h",
|
|
"include/pybind11/eval.h",
|
|
"include/pybind11/functional.h",
|
|
"include/pybind11/gil.h",
|
|
"include/pybind11/iostream.h",
|
|
"include/pybind11/numpy.h",
|
|
"include/pybind11/operators.h",
|
|
"include/pybind11/options.h",
|
|
"include/pybind11/pybind11.h",
|
|
"include/pybind11/pytypes.h",
|
|
"include/pybind11/smart_holder.h",
|
|
"include/pybind11/stl.h",
|
|
"include/pybind11/stl_bind.h",
|
|
"include/pybind11/trampoline_self_life_support.h",
|
|
}
|
|
|
|
detail_headers = {
|
|
"include/pybind11/detail/class.h",
|
|
"include/pybind11/detail/common.h",
|
|
"include/pybind11/detail/descr.h",
|
|
"include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h",
|
|
"include/pybind11/detail/init.h",
|
|
"include/pybind11/detail/internals.h",
|
|
"include/pybind11/detail/smart_holder_poc.h",
|
|
"include/pybind11/detail/smart_holder_sfinae_hooks_only.h",
|
|
"include/pybind11/detail/smart_holder_type_casters.h",
|
|
"include/pybind11/detail/type_caster_base.h",
|
|
"include/pybind11/detail/type_caster_odr_guard.h",
|
|
"include/pybind11/detail/typeid.h",
|
|
}
|
|
|
|
stl_headers = {
|
|
"include/pybind11/stl/filesystem.h",
|
|
}
|
|
|
|
cmake_files = {
|
|
"share/cmake/pybind11/FindPythonLibsNew.cmake",
|
|
"share/cmake/pybind11/pybind11Common.cmake",
|
|
"share/cmake/pybind11/pybind11Config.cmake",
|
|
"share/cmake/pybind11/pybind11ConfigVersion.cmake",
|
|
"share/cmake/pybind11/pybind11NewTools.cmake",
|
|
"share/cmake/pybind11/pybind11Targets.cmake",
|
|
"share/cmake/pybind11/pybind11Tools.cmake",
|
|
}
|
|
|
|
py_files = {
|
|
"__init__.py",
|
|
"__main__.py",
|
|
"_version.py",
|
|
"commands.py",
|
|
"py.typed",
|
|
"setup_helpers.py",
|
|
}
|
|
|
|
headers = main_headers | detail_headers | stl_headers
|
|
src_files = headers | cmake_files
|
|
all_files = src_files | py_files
|
|
|
|
|
|
sdist_files = {
|
|
"pybind11",
|
|
"pybind11/include",
|
|
"pybind11/include/pybind11",
|
|
"pybind11/include/pybind11/detail",
|
|
"pybind11/include/pybind11/stl",
|
|
"pybind11/share",
|
|
"pybind11/share/cmake",
|
|
"pybind11/share/cmake/pybind11",
|
|
"pyproject.toml",
|
|
"setup.cfg",
|
|
"setup.py",
|
|
"LICENSE",
|
|
"MANIFEST.in",
|
|
"README.rst",
|
|
"README_smart_holder.rst",
|
|
"PKG-INFO",
|
|
}
|
|
|
|
local_sdist_files = {
|
|
".egg-info",
|
|
".egg-info/PKG-INFO",
|
|
".egg-info/SOURCES.txt",
|
|
".egg-info/dependency_links.txt",
|
|
".egg-info/not-zip-safe",
|
|
".egg-info/top_level.txt",
|
|
}
|
|
|
|
|
|
def test_build_sdist(monkeypatch, tmpdir):
|
|
|
|
monkeypatch.chdir(MAIN_DIR)
|
|
|
|
out = subprocess.check_output(
|
|
[
|
|
sys.executable,
|
|
"-m",
|
|
"build",
|
|
"--sdist",
|
|
"--outdir",
|
|
str(tmpdir),
|
|
]
|
|
)
|
|
if hasattr(out, "decode"):
|
|
out = out.decode()
|
|
|
|
(sdist,) = tmpdir.visit("*.tar.gz")
|
|
|
|
with tarfile.open(str(sdist), "r:gz") as tar:
|
|
start = tar.getnames()[0] + "/"
|
|
version = start[9:-1]
|
|
simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
|
|
|
|
with contextlib.closing(
|
|
tar.extractfile(tar.getmember(start + "setup.py"))
|
|
) as f:
|
|
setup_py = f.read()
|
|
|
|
with contextlib.closing(
|
|
tar.extractfile(tar.getmember(start + "pyproject.toml"))
|
|
) as f:
|
|
pyproject_toml = f.read()
|
|
|
|
with contextlib.closing(
|
|
tar.extractfile(
|
|
tar.getmember(
|
|
start + "pybind11/share/cmake/pybind11/pybind11Config.cmake"
|
|
)
|
|
)
|
|
) as f:
|
|
contents = f.read().decode("utf8")
|
|
assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in contents
|
|
|
|
files = {f"pybind11/{n}" for n in all_files}
|
|
files |= sdist_files
|
|
files |= {f"pybind11{n}" for n in local_sdist_files}
|
|
files.add("pybind11.egg-info/entry_points.txt")
|
|
files.add("pybind11.egg-info/requires.txt")
|
|
assert simpler == files
|
|
|
|
with open(os.path.join(MAIN_DIR, "tools", "setup_main.py.in"), "rb") as f:
|
|
contents = (
|
|
string.Template(f.read().decode())
|
|
.substitute(version=version, extra_cmd="")
|
|
.encode()
|
|
)
|
|
assert setup_py == contents
|
|
|
|
with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
|
|
contents = f.read()
|
|
assert pyproject_toml == contents
|
|
|
|
|
|
def test_build_global_dist(monkeypatch, tmpdir):
|
|
|
|
monkeypatch.chdir(MAIN_DIR)
|
|
monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
|
|
out = subprocess.check_output(
|
|
[
|
|
sys.executable,
|
|
"-m",
|
|
"build",
|
|
"--sdist",
|
|
"--outdir",
|
|
str(tmpdir),
|
|
]
|
|
)
|
|
|
|
if hasattr(out, "decode"):
|
|
out = out.decode()
|
|
|
|
(sdist,) = tmpdir.visit("*.tar.gz")
|
|
|
|
with tarfile.open(str(sdist), "r:gz") as tar:
|
|
start = tar.getnames()[0] + "/"
|
|
version = start[16:-1]
|
|
simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
|
|
|
|
with contextlib.closing(
|
|
tar.extractfile(tar.getmember(start + "setup.py"))
|
|
) as f:
|
|
setup_py = f.read()
|
|
|
|
with contextlib.closing(
|
|
tar.extractfile(tar.getmember(start + "pyproject.toml"))
|
|
) as f:
|
|
pyproject_toml = f.read()
|
|
|
|
files = {f"pybind11/{n}" for n in all_files}
|
|
files |= sdist_files
|
|
files |= {f"pybind11_global{n}" for n in local_sdist_files}
|
|
assert simpler == files
|
|
|
|
with open(os.path.join(MAIN_DIR, "tools", "setup_global.py.in"), "rb") as f:
|
|
contents = (
|
|
string.Template(f.read().decode())
|
|
.substitute(version=version, extra_cmd="")
|
|
.encode()
|
|
)
|
|
assert setup_py == contents
|
|
|
|
with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
|
|
contents = f.read()
|
|
assert pyproject_toml == contents
|
|
|
|
|
|
def tests_build_wheel(monkeypatch, tmpdir):
|
|
monkeypatch.chdir(MAIN_DIR)
|
|
|
|
subprocess.check_output(
|
|
[sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)]
|
|
)
|
|
|
|
(wheel,) = tmpdir.visit("*.whl")
|
|
|
|
files = {f"pybind11/{n}" for n in all_files}
|
|
files |= {
|
|
"dist-info/LICENSE",
|
|
"dist-info/METADATA",
|
|
"dist-info/RECORD",
|
|
"dist-info/WHEEL",
|
|
"dist-info/entry_points.txt",
|
|
"dist-info/top_level.txt",
|
|
}
|
|
|
|
with zipfile.ZipFile(str(wheel)) as z:
|
|
names = z.namelist()
|
|
|
|
trimmed = {n for n in names if "dist-info" not in n}
|
|
trimmed |= {f"dist-info/{n.split('/', 1)[-1]}" for n in names if "dist-info" in n}
|
|
assert files == trimmed
|
|
|
|
|
|
def tests_build_global_wheel(monkeypatch, tmpdir):
|
|
monkeypatch.chdir(MAIN_DIR)
|
|
monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
|
|
|
|
subprocess.check_output(
|
|
[sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)]
|
|
)
|
|
|
|
(wheel,) = tmpdir.visit("*.whl")
|
|
|
|
files = {f"data/data/{n}" for n in src_files}
|
|
files |= {f"data/headers/{n[8:]}" for n in headers}
|
|
files |= {
|
|
"dist-info/LICENSE",
|
|
"dist-info/METADATA",
|
|
"dist-info/WHEEL",
|
|
"dist-info/top_level.txt",
|
|
"dist-info/RECORD",
|
|
}
|
|
|
|
with zipfile.ZipFile(str(wheel)) as z:
|
|
names = z.namelist()
|
|
|
|
beginning = names[0].split("/", 1)[0].rsplit(".", 1)[0]
|
|
trimmed = {n[len(beginning) + 1 :] for n in names}
|
|
|
|
assert files == trimmed
|