From: Vladimír Vondruš Date: Tue, 21 May 2019 22:08:19 +0000 (+0200) Subject: documentation/python: don't ignore pybind submodules inside packages. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=52ae12e97fc10d461ff14aa08eff2d85cb80a5b5;p=blog.git documentation/python: don't ignore pybind submodules inside packages. This is extra brittle. Hahah why this has to be so damn complex. --- diff --git a/documentation/python.py b/documentation/python.py index 5c3a2556..905d57e3 100755 --- a/documentation/python.py +++ b/documentation/python.py @@ -165,10 +165,11 @@ def is_internal_or_imported_module_member(state: State, parent, path: str, name: # handle modules and packages differently. See also for more info: # https://stackoverflow.com/a/7948672 else: - # pybind11 submodules have __package__ set to None for nested modules, - # the top-level __package__ is '' though. Allow these if parent - # __package__ is empty (either '' or None). - if state.config['PYBIND11_COMPATIBILITY'] and object.__package__ is None and not parent.__package__: return False + # pybind11 submodules have __package__ set to None (instead of '') for + # nested modules. Allow these. The parent's __package__ can be None (if + # it's a nested submodule), '' (if it's a top-level module) or a string + # (if the parent is a Python package), can't really check further. + if state.config['PYBIND11_COMPATIBILITY'] and object.__package__ is None: return False # The parent is a single-file module (not a package), these don't have # submodules so this is most definitely an imported module. Source: diff --git a/documentation/test_python/CMakeLists.txt b/documentation/test_python/CMakeLists.txt index be599c14..e38d63f7 100644 --- a/documentation/test_python/CMakeLists.txt +++ b/documentation/test_python/CMakeLists.txt @@ -37,3 +37,9 @@ pybind11_add_module(pybind_name_mapping pybind_name_mapping/sub.cpp) set_target_properties(pybind_name_mapping PROPERTIES OUTPUT_NAME _sub LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/pybind_name_mapping/pybind_name_mapping) + +# Need a special name for this one +pybind11_add_module(pybind_submodules_package pybind_submodules_package/sub.cpp) +set_target_properties(pybind_submodules_package PROPERTIES + OUTPUT_NAME sub + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/pybind_submodules_package/pybind_submodules_package) diff --git a/documentation/test_python/pybind_submodules_package/pybind_submodules_package.sub.html b/documentation/test_python/pybind_submodules_package/pybind_submodules_package.sub.html new file mode 100644 index 00000000..2065a267 --- /dev/null +++ b/documentation/test_python/pybind_submodules_package/pybind_submodules_package.sub.html @@ -0,0 +1,51 @@ + + + + + pybind_submodules_package.sub | My Python Project + + + + + +
+
+ + diff --git a/documentation/test_python/pybind_submodules_package/pybind_submodules_package/__init__.py b/documentation/test_python/pybind_submodules_package/pybind_submodules_package/__init__.py new file mode 100644 index 00000000..cde8adcf --- /dev/null +++ b/documentation/test_python/pybind_submodules_package/pybind_submodules_package/__init__.py @@ -0,0 +1,3 @@ +import pybind_submodules_package.sub + +__all__ = ['sub'] diff --git a/documentation/test_python/pybind_submodules_package/sub.cpp b/documentation/test_python/pybind_submodules_package/sub.cpp new file mode 100644 index 00000000..b30985be --- /dev/null +++ b/documentation/test_python/pybind_submodules_package/sub.cpp @@ -0,0 +1,8 @@ +#include + +PYBIND11_MODULE(sub, m) { + m.doc() = "pybind11 submodule of a Python package"; + + m.def_submodule("subsub", "Yay a submodule"); + m.def_submodule("another", "Yay another"); +} diff --git a/documentation/test_python/test_pybind.py b/documentation/test_python/test_pybind.py index b5679d44..fb29c0d8 100644 --- a/documentation/test_python/test_pybind.py +++ b/documentation/test_python/test_pybind.py @@ -202,6 +202,16 @@ class Submodules(BaseInspectTestCase): }) self.assertEqual(*self.actual_expected_contents('pybind_submodules.html')) +class SubmodulesPackage(BaseInspectTestCase): + def __init__(self, *args, **kwargs): + super().__init__(__file__, 'submodules_package', *args, **kwargs) + + def test(self): + self.run_python({ + 'PYBIND11_COMPATIBILITY': True + }) + self.assertEqual(*self.actual_expected_contents('pybind_submodules_package.sub.html')) + class NameMapping(BaseInspectTestCase): def __init__(self, *args, **kwargs): super().__init__(__file__, 'name_mapping', *args, **kwargs)