From 9c8df99e40c8531d15f22b4c636a54cfd539c335 Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Sat, 26 Feb 2022 16:43:38 +0100 Subject: [PATCH 1/7] Adding "make_alias" to use a different `__name__` as shared-object --- include/pybind11/pybind11.h | 22 ++++++++++++++++++++++ tests/test_make_alias.cpp | 22 ++++++++++++++++++++++ tests/test_make_alias.py | 8 ++++++++ 3 files changed, 52 insertions(+) create mode 100644 tests/test_make_alias.cpp create mode 100644 tests/test_make_alias.py diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 22b629f40..e5c97cbc3 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1245,6 +1245,28 @@ public: // For Python 2, reinterpret_borrow was correct. return reinterpret_borrow(m); } + + struct strip_leading_underscore_from_name; + + static module_ make_alias(const char *name) { + module_ ret = *this; + ret.attr("__name__") = this.attr(name); + return ret; + } + + static module_ make_alias(strip_leading_underscore_from_name) { + const char *name = PyModule_GetName(*this); + size_t n = strlen(name); + if (n == 0) { + return this->make_alias(name); + } + if (name[0] != "_") { + return this->make_alias(name); + } + char alias[n - 1]; + std::copy(&name[1], &name[n - 1], &alias[0]); + return this->make_alias(alias); + } }; // When inside a namespace (or anywhere as long as it's not the first item on a line), diff --git a/tests/test_make_alias.cpp b/tests/test_make_alias.cpp new file mode 100644 index 000000000..deab102d8 --- /dev/null +++ b/tests/test_make_alias.cpp @@ -0,0 +1,22 @@ +/* + tests/make_alias -- make alias + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +#if defined(_MSC_VER) +# pragma warning(disable : 4996) // C4996: std::unary_negation is deprecated +#endif + +TEST_SUBMODULE(_make_alias, m) { + ma = m.make_alias(pybind11::module::strip_leading_underscore_from_name); + ma.def("foo", []() -> { }); +} diff --git a/tests/test_make_alias.py b/tests/test_make_alias.py new file mode 100644 index 000000000..b43fcde4d --- /dev/null +++ b/tests/test_make_alias.py @@ -0,0 +1,8 @@ +import pytest + +from pybind11_tests import ConstructorStats + +m = pytest.importorskip("pybind11_tests.make_alias") + +def assert_name(mat): + assert m.__name__ == "make_alias" From ae3cfa82cc769d227e0733214fe8bd31be9e9cb4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 26 Feb 2022 15:45:52 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_make_alias.cpp | 2 +- tests/test_make_alias.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_make_alias.cpp b/tests/test_make_alias.cpp index deab102d8..0794d429e 100644 --- a/tests/test_make_alias.cpp +++ b/tests/test_make_alias.cpp @@ -18,5 +18,5 @@ TEST_SUBMODULE(_make_alias, m) { ma = m.make_alias(pybind11::module::strip_leading_underscore_from_name); - ma.def("foo", []() -> { }); + ma.def("foo", []() -> {}); } diff --git a/tests/test_make_alias.py b/tests/test_make_alias.py index b43fcde4d..41a0e3605 100644 --- a/tests/test_make_alias.py +++ b/tests/test_make_alias.py @@ -4,5 +4,6 @@ from pybind11_tests import ConstructorStats m = pytest.importorskip("pybind11_tests.make_alias") + def assert_name(mat): assert m.__name__ == "make_alias" From 726918d74abbbfd49c0d87e9fdcd58972b74f6aa Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Sat, 26 Feb 2022 16:50:55 +0100 Subject: [PATCH 3/7] Fixing compiler errors --- include/pybind11/pybind11.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index e5c97cbc3..4760c44b1 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1258,14 +1258,14 @@ public: const char *name = PyModule_GetName(*this); size_t n = strlen(name); if (n == 0) { - return this->make_alias(name); + return make_alias(name); } - if (name[0] != "_") { - return this->make_alias(name); + if (strcmp(name[0], "_") != 0) { + return make_alias(name); } char alias[n - 1]; std::copy(&name[1], &name[n - 1], &alias[0]); - return this->make_alias(alias); + return make_alias(alias); } }; From f4ee39c229c5a57a24cbbe5cca266db3ddda855d Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Sat, 26 Feb 2022 16:57:15 +0100 Subject: [PATCH 4/7] Addressing compiler errors --- include/pybind11/pybind11.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 4760c44b1..440e766cc 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1246,21 +1246,21 @@ public: return reinterpret_borrow(m); } - struct strip_leading_underscore_from_name; + struct strip_leading_underscore_from_name {}; - static module_ make_alias(const char *name) { + module_ make_alias(const char *name) { module_ ret = *this; - ret.attr("__name__") = this.attr(name); + ret.attr("__name__") = name; return ret; } - static module_ make_alias(strip_leading_underscore_from_name) { + module_ make_alias(strip_leading_underscore_from_name) { const char *name = PyModule_GetName(*this); size_t n = strlen(name); if (n == 0) { return make_alias(name); } - if (strcmp(name[0], "_") != 0) { + if (strcmp(&name[0], "_") != 0) { return make_alias(name); } char alias[n - 1]; From 549f3b7e5413b1d07a1aa225c79db5ecaa5d362f Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Sat, 26 Feb 2022 22:37:05 +0100 Subject: [PATCH 5/7] Update include/pybind11/pybind11.h Co-authored-by: Sebastian Koslowski --- include/pybind11/pybind11.h | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 440e766cc..10277f031 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1256,16 +1256,9 @@ public: module_ make_alias(strip_leading_underscore_from_name) { const char *name = PyModule_GetName(*this); - size_t n = strlen(name); - if (n == 0) { - return make_alias(name); - } - if (strcmp(&name[0], "_") != 0) { - return make_alias(name); - } - char alias[n - 1]; - std::copy(&name[1], &name[n - 1], &alias[0]); - return make_alias(alias); + if (name && name[0] == '_') + name += 1; + return make_alias(name); } }; From eb2ecf103328a682ff9ecd638cbb2b1f3a0da03d Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Sat, 26 Feb 2022 22:37:31 +0100 Subject: [PATCH 6/7] Update tests/test_make_alias.cpp Co-authored-by: Sebastian Koslowski --- tests/test_make_alias.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_make_alias.cpp b/tests/test_make_alias.cpp index 0794d429e..487df8292 100644 --- a/tests/test_make_alias.cpp +++ b/tests/test_make_alias.cpp @@ -17,6 +17,6 @@ #endif TEST_SUBMODULE(_make_alias, m) { - ma = m.make_alias(pybind11::module::strip_leading_underscore_from_name); + ma = m.make_alias(pybind11::module::strip_leading_underscore_from_name{}); ma.def("foo", []() -> {}); } From a14c936562d859f610c9718897d6afe330b8bf25 Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Sat, 26 Feb 2022 22:37:44 +0100 Subject: [PATCH 7/7] Update tests/test_make_alias.py Co-authored-by: Sebastian Koslowski --- tests/test_make_alias.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_make_alias.py b/tests/test_make_alias.py index 41a0e3605..f8d4f6862 100644 --- a/tests/test_make_alias.py +++ b/tests/test_make_alias.py @@ -2,7 +2,7 @@ import pytest from pybind11_tests import ConstructorStats -m = pytest.importorskip("pybind11_tests.make_alias") +m = pytest.importorskip("pybind11_tests._make_alias") def assert_name(mat):