-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[smart_holder] Add test_namespace_visibility #4050
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
rwgk
merged 11 commits into
pybind:smart_holder
from
rwgk:probe_namespace_visibility_sh
Jul 13, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a85bac8
Add test_namespace_visibility
rwgk c285357
Disable flake8 in ubench/holder_comparison_*.py, to suppress new & us…
rwgk bbc1f9b
Disable namespace_visibility_1s.cpp (tosee if that resolves the MSVC …
rwgk c4d3e4a
Turn off flake8 completely for ubench (the Strip unnecessary `# noqa`…
rwgk c805bdb
Disable test_namespace_visibility completely. Just keep the two .cpp …
rwgk 1e6e220
Rename test_namespace_visibility.py to test_exc_namespace_visibility.…
rwgk cfffd7d
Add `set_property(SOURCE namespace_visibility_1s.cpp PROPERTY LANGUAG…
rwgk e3f4aa6
Add reference to PR #4054
rwgk dd4170d
Complete the documentation (comments in test_exc_namespace_visibility…
rwgk 35ee7c8
Merge branch 'smart_holder' into probe_namespace_visibility_sh
rwgk e2f0056
Rename namespace_visibility.h to namespace_visibility.inl, as suggest…
rwgk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2022 The Pybind Development Team. | ||
// All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
#pragma once | ||
|
||
#include <cstddef> | ||
|
||
#ifdef __GNUG__ | ||
# define PYBIND11_NS_VIS_U /* unspecified */ | ||
# define PYBIND11_NS_VIS_H __attribute__((visibility("hidden"))) | ||
#else | ||
# define PYBIND11_NS_VIS_U | ||
# define PYBIND11_NS_VIS_H | ||
#endif | ||
|
||
#define PYBIND11_NS_VIS_FUNC \ | ||
inline std::ptrdiff_t func() { \ | ||
static std::ptrdiff_t value = 0; \ | ||
return reinterpret_cast<std::ptrdiff_t>(&value); \ | ||
} | ||
|
||
#define PYBIND11_NS_VIS_DEFS \ | ||
m.def("ns_vis_uuu_func", pybind11_ns_vis_uuu::func); \ | ||
m.def("ns_vis_uuh_func", pybind11_ns_vis_uuh::func); \ | ||
m.def("ns_vis_uhu_func", pybind11_ns_vis_uhu::func); \ | ||
m.def("ns_vis_uhh_func", pybind11_ns_vis_uhh::func); \ | ||
m.def("ns_vis_huu_func", pybind11_ns_vis_huu::func); \ | ||
m.def("ns_vis_huh_func", pybind11_ns_vis_huh::func); \ | ||
m.def("ns_vis_hhu_func", pybind11_ns_vis_hhu::func); \ | ||
m.def("ns_vis_hhh_func", pybind11_ns_vis_hhh::func); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "pybind11/pybind11.h" | ||
#include "namespace_visibility.inl" | ||
|
||
// clang-format off | ||
namespace pybind11_ns_vis_uuu PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_uuh PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_uhu PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_uhh PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_huu PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_huh PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_hhu PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_hhh PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
// ^ ^ | ||
// bit used .............. here | ||
// clang-format on | ||
|
||
void namespace_visibility_1s(pybind11::module_ &m); | ||
|
||
PYBIND11_MODULE(namespace_visibility_1, m) { | ||
PYBIND11_NS_VIS_DEFS | ||
|
||
auto sm = m.def_submodule("submodule"); | ||
namespace_visibility_1s(sm); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "pybind11/pybind11.h" | ||
#include "namespace_visibility.inl" | ||
|
||
// clang-format off | ||
namespace pybind11_ns_vis_uuu PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_uuh PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_uhu PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_uhh PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_huu PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_huh PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_hhu PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_hhh PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
// ^ ^ | ||
// bit used ............. here | ||
// clang-format on | ||
|
||
void namespace_visibility_1s(pybind11::module_ &m) { PYBIND11_NS_VIS_DEFS } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "pybind11/pybind11.h" | ||
#include "namespace_visibility.inl" | ||
|
||
// clang-format off | ||
namespace pybind11_ns_vis_uuu PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_uuh PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_uhu PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_uhh PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_huu PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_huh PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_hhu PYBIND11_NS_VIS_U { PYBIND11_NS_VIS_FUNC } | ||
namespace pybind11_ns_vis_hhh PYBIND11_NS_VIS_H { PYBIND11_NS_VIS_FUNC } | ||
// ^ ^ | ||
// bit used ............ here | ||
// clang-format on | ||
|
||
PYBIND11_MODULE(namespace_visibility_2, m) { PYBIND11_NS_VIS_DEFS } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# This is not really a unit test, but probing environment/toolchain/platform-specific | ||
# behavior under the exact same conditions as normal tests. | ||
# The results are useful to understanding the effects of, e.g., removing | ||
# `-fvisibility=hidden` or `__attribute__((visibility("hidden")))`, or linking | ||
# extensions statically with the core Python interpreter. | ||
|
||
# NOTE | ||
# ==== | ||
# The "exc_" in "test_exc_namespace_visibility.py" is a workaround, to avoid a | ||
# test_cross_module_exception_translator (test_exceptions.py) failure. This | ||
# test has to be imported (by pytest) before test_exceptions.py; pytest sorts | ||
# lexically. See https://github.com/pybind/pybind11/pull/4054 for more information. | ||
|
||
import itertools | ||
|
||
import namespace_visibility_1 | ||
import namespace_visibility_2 | ||
import pytest | ||
|
||
# Please take a quick look at namespace_visibility.h first, to see what is being probed. | ||
# | ||
# EXPECTED is for -fvisibility=hidden or equivalent, as recommended in the docs. | ||
EXPECTED_ALL_UNIQUE_POINTERS_OBSERVED = "AAC:AAc:AaC:Aac:aAC:aAc:aaC:aac" | ||
# ^^^ | ||
# namespace_visibility_1 pointer||| | ||
# namespace_visibility_1.submodule pointer|| (identical letters means same pointer) | ||
# namespace_visibility_2 pointer| | ||
# Upper-case: namespace visibility unspecified | ||
# Lower-case: namespace visibility hidden | ||
# This test probes all 2**3 combinations of u/h ** number-of-sub/modules. | ||
# | ||
# Also observed: | ||
# AAA:AAc:AaC:Aac:aAC:aAc:aaC:aac -fvisibility=default Linux | ||
# AAA:AAc:AaA:Aac:aAC:aAc:aaC:aac -fvisibility=default macOS | ||
# AAA:AAa:AaA:Aaa:aAA:aAa:aaA:aaa everything linked statically | ||
|
||
|
||
def test_namespace_visibility(): | ||
modules = ( | ||
namespace_visibility_1, | ||
namespace_visibility_1.submodule, | ||
namespace_visibility_2, | ||
) | ||
unique_pointer_labels = "ABC" | ||
unique_pointers_observed = [] | ||
# u = visibility unspecified | ||
# h = visibility hidden | ||
for visibility in itertools.product(*([("u", "h")] * len(modules))): | ||
# See functions in namespace_visibility_*.cpp | ||
func = "ns_vis_" + "".join(visibility) + "_func" | ||
ptrs = [] | ||
uq_ptrs_obs = "" | ||
for vis, m in zip(visibility, modules): | ||
ptr = getattr(m, func)() | ||
ptrs.append(ptr) | ||
lbl = unique_pointer_labels[ptrs.index(ptr)] | ||
if vis == "h": | ||
# Encode u/h info as upper/lower case to make the final result | ||
# as compact as possible. | ||
lbl = lbl.lower() | ||
uq_ptrs_obs += lbl | ||
unique_pointers_observed.append(uq_ptrs_obs) | ||
all_unique_pointers_observed = ":".join(unique_pointers_observed) | ||
if all_unique_pointers_observed != EXPECTED_ALL_UNIQUE_POINTERS_OBSERVED: | ||
pytest.skip( | ||
f"UNUSUAL all_unique_pointers_observed: {all_unique_pointers_observed}" | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.