Skip to content

[Experiment] std::type_index & un/named namespace #4316

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 3 commits 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
5 changes: 5 additions & 0 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ struct internals {
PYBIND11_TLS_FREE(tstate);
}
#endif

std::unordered_map<std::type_index, std::vector<std::string>>
std_type_index_registry_unnamed_namespace;
std::unordered_map<std::type_index, std::vector<std::string>>
std_type_index_registry_named_namespace;
};

/// Additional type information which does not fit into the PyTypeObject.
Expand Down
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ set(PYBIND11_TEST_FILES
test_eigen_tensor_avoid_stl_array.cpp
test_enum
test_eval
test_exc_named_namespace_a.py
test_exc_named_namespace_b.py
test_exceptions
test_factory_constructors
test_gil_scoped
Expand All @@ -156,6 +158,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 Expand Up @@ -219,6 +223,8 @@ tests_extra_targets("test_exceptions.py;test_local_bindings.py;test_stl.py;test_
# And add additional targets for other tests.
tests_extra_targets("test_exceptions.py" "cross_module_interleaved_error_already_set")
tests_extra_targets("test_gil_scoped.py" "cross_module_gil_utils")
tests_extra_targets("test_exc_named_namespace_a.py" "named_namespace_a")
tests_extra_targets("test_exc_named_namespace_b.py" "named_namespace_b")

set(PYBIND11_EIGEN_REPO
"https://gitlab.com/libeigen/eigen.git"
Expand Down
25 changes: 25 additions & 0 deletions tests/named_namespace_a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <pybind11/stl.h>

#include "pybind11_tests.h"

namespace test_named_namespace {
struct any_struct {};
} // namespace test_named_namespace

PYBIND11_MODULE(named_namespace_a, m) {
m.attr("name") = "NA";

py::detail::get_internals()
.std_type_index_registry_named_namespace[std::type_index(
typeid(test_named_namespace::any_struct))]
.push_back("NA");

m.def("std_type_index_registry_dump", []() {
py::list items;
for (const auto &it :
py::detail::get_internals().std_type_index_registry_named_namespace) {
items.append(py::make_tuple(it.first.name(), it.second));
}
return items;
});
}
14 changes: 14 additions & 0 deletions tests/named_namespace_b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "pybind11_tests.h"

namespace test_named_namespace {
struct any_struct {};
} // namespace test_named_namespace

PYBIND11_MODULE(named_namespace_b, m) {
m.attr("name") = "NB";

py::detail::get_internals()
.std_type_index_registry_named_namespace[std::type_index(
typeid(test_named_namespace::any_struct))]
.push_back("NB");
}
15 changes: 15 additions & 0 deletions tests/test_exc_named_namespace_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import named_namespace_a as m
import pytest


def test_inspect():
assert m.name == "NA"
reg = m.std_type_index_registry_dump()
if len(reg) == 1:
assert tuple(sorted(reg[0][1])) == ("NA", "NB")
pytest.skip("std::type_index-EQ-GOOD")
if len(reg) == 2:
assert reg[0][0] == reg[1][0]
assert tuple(sorted(reg[0][1] + reg[1][1])) == ("NA", "NB")
pytest.skip("std::type_index-NE-BAD")
assert reg is None # Sure to fail.
5 changes: 5 additions & 0 deletions tests/test_exc_named_namespace_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import named_namespace_b as m


def test_inspect():
assert m.name == "NB"
24 changes: 24 additions & 0 deletions tests/test_unnamed_namespace_a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <pybind11/stl.h>

#include "pybind11_tests.h"

namespace {
struct any_struct {};
} // namespace

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

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

m.def("std_type_index_registry_dump", []() {
py::list items;
for (const auto &it :
py::detail::get_internals().std_type_index_registry_unnamed_namespace) {
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 == "UA"
reg = m.std_type_index_registry_dump()
if len(reg) == 1:
assert tuple(sorted(reg[0][1])) == ("UA", "UB")
pytest.skip("std::type_index-EQ-BAD")
if len(reg) == 2:
assert tuple(sorted([reg[0][1][0], reg[1][1][0]])) == ("UA", "UB")
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") = "UB";

py::detail::get_internals()
.std_type_index_registry_unnamed_namespace[std::type_index(typeid(any_struct))]
.push_back("UB");
}
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 == "UB"