diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index caca57b3e4..5f24dbde7a 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1336,6 +1336,19 @@ class module_ : public object { if (doc && options::show_user_defined_docstrings()) { result.attr("__doc__") = pybind11::str(doc); } + +#if defined(GRAALVM_PYTHON) && (!defined(GRAALPY_VERSION_NUM) || GRAALPY_VERSION_NUM < 0x190000) + // GraalPy doesn't support PyModule_GetFilenameObject, + // so getting by attribute (see PR #5584) + handle this_module = m_ptr; + result.attr("__file__") = this_module.attr("__file__"); +#else + handle this_file = PyModule_GetFilenameObject(m_ptr); + if (!this_file) { + throw error_already_set(); + } + result.attr("__file__") = this_file; +#endif attr(name) = result; return result; } diff --git a/tests/test_modules.py b/tests/test_modules.py index ad898be89a..f26c43da99 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -21,6 +21,7 @@ def test_nested_modules(): ) assert m.__name__ == "pybind11_tests.modules" assert ms.__name__ == "pybind11_tests.modules.subsubmodule" + assert m.__file__ == ms.__file__ assert ms.submodule_func() == "submodule_func()"