Compare commits

...

8 Commits

Author SHA1 Message Date
Tom de Geus 7ec4eec983
Merge a14c936562 into a7910be630 2024-09-16 15:51:57 -06:00
Tom de Geus a14c936562
Update tests/test_make_alias.py
Co-authored-by: Sebastian Koslowski <sebastian.koslowski@gmail.com>
2022-02-26 22:37:44 +01:00
Tom de Geus eb2ecf1033
Update tests/test_make_alias.cpp
Co-authored-by: Sebastian Koslowski <sebastian.koslowski@gmail.com>
2022-02-26 22:37:31 +01:00
Tom de Geus 549f3b7e54
Update include/pybind11/pybind11.h
Co-authored-by: Sebastian Koslowski <sebastian.koslowski@gmail.com>
2022-02-26 22:37:05 +01:00
Tom de Geus f4ee39c229
Addressing compiler errors 2022-02-26 16:57:15 +01:00
Tom de Geus 726918d74a
Fixing compiler errors 2022-02-26 16:52:00 +01:00
pre-commit-ci[bot] ae3cfa82cc [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2022-02-26 15:45:53 +00:00
Tom de Geus 9c8df99e40
Adding "make_alias" to use a different `__name__` as shared-object 2022-02-26 16:43:38 +01:00
3 changed files with 46 additions and 0 deletions

View File

@ -1301,6 +1301,21 @@ public:
// For Python 2, reinterpret_borrow was correct.
return reinterpret_borrow<module_>(m);
}
struct strip_leading_underscore_from_name {};
module_ make_alias(const char *name) {
module_ ret = *this;
ret.attr("__name__") = name;
return ret;
}
module_ make_alias(strip_leading_underscore_from_name) {
const char *name = PyModule_GetName(*this);
if (name && name[0] == '_')
name += 1;
return make_alias(name);
}
};
PYBIND11_NAMESPACE_BEGIN(detail)

22
tests/test_make_alias.cpp Normal file
View File

@ -0,0 +1,22 @@
/*
tests/make_alias -- make alias
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/
#include <pybind11/pybind11.h>
#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", []() -> {});
}

9
tests/test_make_alias.py Normal file
View File

@ -0,0 +1,9 @@
import pytest
from pybind11_tests import ConstructorStats
m = pytest.importorskip("pybind11_tests._make_alias")
def assert_name(mat):
assert m.__name__ == "make_alias"