Skip to content

Commit

Permalink
[Experiment] boost::typeindex::type_index & unnamed namespace
Browse files Browse the repository at this point in the history
Related to PR pybind#4313
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Nov 6, 2022
1 parent ee2b522 commit c4f6602
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

#include "../pytypes.h"

#if defined(PYBIND11_TEST_BOOST)
# include <boost/type_index.hpp>
# include <boost/unordered_map.hpp>
#endif

#include <exception>

/// Tracks the `internals` and `type_info` ABI version independent of the main library version.
Expand Down Expand Up @@ -209,6 +214,11 @@ struct internals {
PYBIND11_TLS_FREE(tstate);
}
#endif

#if defined(PYBIND11_TEST_BOOST)
boost::unordered_map<boost::typeindex::type_index, std::vector<std::string>>
boost_type_index_registry;
#endif
};

/// 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
30 changes: 30 additions & 0 deletions tests/test_unnamed_namespace_a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <pybind11/stl.h>

#include "pybind11_tests.h"

namespace {
struct any_struct {};
} // namespace

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

#if defined(PYBIND11_TEST_BOOST)
py::detail::get_internals()
.boost_type_index_registry[boost::typeindex::type_index(
boost::typeindex::type_id<any_struct>())]
.push_back("A");
#endif

m.def("boost_type_index_registry_dump", []() {
#if defined(PYBIND11_TEST_BOOST)
py::list items;
for (const auto &it : py::detail::get_internals().boost_type_index_registry) {
items.append(py::make_tuple(it.first.pretty_name(), it.second));
}
return items;
#else
return py::none();
#endif
});
}
17 changes: 17 additions & 0 deletions tests/test_unnamed_namespace_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from pybind11_tests import unnamed_namespace_a as m


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

namespace {
struct any_struct {};
} // namespace

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

#if defined(PYBIND11_TEST_BOOST)
py::detail::get_internals()
.boost_type_index_registry[boost::typeindex::type_index(
boost::typeindex::type_id<any_struct>())]
.push_back("B");
#endif
}
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 c4f6602

Please sign in to comment.