Skip to content
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

[Experiment] boost::typeindex::type_index & unnamed namespace #4315

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"