Skip to content

Commit

Permalink
[Experiment] std::type_index & unnamed namespace
Browse files Browse the repository at this point in the history
Related to PRs pybind#4313, pybind#4315
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Nov 6, 2022
1 parent ee2b522 commit 282eb72
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ struct internals {
PYBIND11_TLS_FREE(tstate);
}
#endif

std::unordered_map<std::type_index, std::vector<std::string>> std_type_index_registry;
};

/// Additional type information which does not fit into the PyTypeObject.
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ set(PYBIND11_TEST_FILES
test_tagbased_polymorphic
test_thread
test_union
test_unnamed_namespace_a
test_unnamed_namespace_b
test_virtual_functions)

# Invoking cmake with something like:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_unnamed_namespace_a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <pybind11/stl.h>

#include "pybind11_tests.h"

namespace {
struct any_struct {};
} // namespace

TEST_SUBMODULE(unnamed_namespace_a, m) {
m.attr("name") = "A";

py::detail::get_internals()
.std_type_index_registry[std::type_index(typeid(any_struct))]
.push_back("A");

m.def("std_type_index_registry_dump", []() {
py::list items;
for (const auto &it : py::detail::get_internals().std_type_index_registry) {
items.append(py::make_tuple(it.first.name(), it.second));
}
return items;
});
}
15 changes: 15 additions & 0 deletions tests/test_unnamed_namespace_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

from pybind11_tests import unnamed_namespace_a as m


def test_inspect():
assert m.name == "A"
reg = m.std_type_index_registry_dump()
if len(reg) == 1:
assert tuple(sorted(reg[0][1])) == ("A", "B")
pytest.skip("std::type_index-EQ-BAD")
if len(reg) == 2:
assert tuple(sorted([reg[0][1][0], reg[1][1][0]])) == ("A", "B")
pytest.skip("std::type_index-NE-GOOD")
assert reg is None # Sure to fail.
13 changes: 13 additions & 0 deletions tests/test_unnamed_namespace_b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "pybind11_tests.h"

namespace {
struct any_struct {};
} // namespace

TEST_SUBMODULE(unnamed_namespace_b, m) {
m.attr("name") = "B";

py::detail::get_internals()
.std_type_index_registry[std::type_index(typeid(any_struct))]
.push_back("B");
}
5 changes: 5 additions & 0 deletions tests/test_unnamed_namespace_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pybind11_tests import unnamed_namespace_b as m


def test_inspect():
assert m.name == "B"

0 comments on commit 282eb72

Please sign in to comment.