This commit is contained in:
Tom de Geus 2024-09-20 23:44:18 -07:00 committed by GitHub
commit e1dd502ed0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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"