Skip to content

Commit 282eb72

Browse files
committed
[Experiment] std::type_index & unnamed namespace
Related to PRs pybind#4313, pybind#4315
1 parent ee2b522 commit 282eb72

6 files changed

+60
-0
lines changed

include/pybind11/detail/internals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ struct internals {
209209
PYBIND11_TLS_FREE(tstate);
210210
}
211211
#endif
212+
213+
std::unordered_map<std::type_index, std::vector<std::string>> std_type_index_registry;
212214
};
213215

214216
/// Additional type information which does not fit into the PyTypeObject.

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ set(PYBIND11_TEST_FILES
156156
test_tagbased_polymorphic
157157
test_thread
158158
test_union
159+
test_unnamed_namespace_a
160+
test_unnamed_namespace_b
159161
test_virtual_functions)
160162

161163
# Invoking cmake with something like:

tests/test_unnamed_namespace_a.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <pybind11/stl.h>
2+
3+
#include "pybind11_tests.h"
4+
5+
namespace {
6+
struct any_struct {};
7+
} // namespace
8+
9+
TEST_SUBMODULE(unnamed_namespace_a, m) {
10+
m.attr("name") = "A";
11+
12+
py::detail::get_internals()
13+
.std_type_index_registry[std::type_index(typeid(any_struct))]
14+
.push_back("A");
15+
16+
m.def("std_type_index_registry_dump", []() {
17+
py::list items;
18+
for (const auto &it : py::detail::get_internals().std_type_index_registry) {
19+
items.append(py::make_tuple(it.first.name(), it.second));
20+
}
21+
return items;
22+
});
23+
}

tests/test_unnamed_namespace_a.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
from pybind11_tests import unnamed_namespace_a as m
4+
5+
6+
def test_inspect():
7+
assert m.name == "A"
8+
reg = m.std_type_index_registry_dump()
9+
if len(reg) == 1:
10+
assert tuple(sorted(reg[0][1])) == ("A", "B")
11+
pytest.skip("std::type_index-EQ-BAD")
12+
if len(reg) == 2:
13+
assert tuple(sorted([reg[0][1][0], reg[1][1][0]])) == ("A", "B")
14+
pytest.skip("std::type_index-NE-GOOD")
15+
assert reg is None # Sure to fail.

tests/test_unnamed_namespace_b.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "pybind11_tests.h"
2+
3+
namespace {
4+
struct any_struct {};
5+
} // namespace
6+
7+
TEST_SUBMODULE(unnamed_namespace_b, m) {
8+
m.attr("name") = "B";
9+
10+
py::detail::get_internals()
11+
.std_type_index_registry[std::type_index(typeid(any_struct))]
12+
.push_back("B");
13+
}

tests/test_unnamed_namespace_b.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from pybind11_tests import unnamed_namespace_b as m
2+
3+
4+
def test_inspect():
5+
assert m.name == "B"

0 commit comments

Comments
 (0)