chiark / gitweb /
documentation/python: don't ignore pybind submodules inside packages.
authorVladimír Vondruš <mosra@centrum.cz>
Tue, 21 May 2019 22:08:19 +0000 (00:08 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Tue, 28 May 2019 18:05:07 +0000 (20:05 +0200)
This is extra brittle. Hahah why this has to be so damn complex.

documentation/python.py
documentation/test_python/CMakeLists.txt
documentation/test_python/pybind_submodules_package/pybind_submodules_package.sub.html [new file with mode: 0644]
documentation/test_python/pybind_submodules_package/pybind_submodules_package/__init__.py [new file with mode: 0644]
documentation/test_python/pybind_submodules_package/sub.cpp [new file with mode: 0644]
documentation/test_python/test_pybind.py

index 5c3a25561acbb0d5d53edea2a063ee8347e81b92..905d57e3afbb3bb19631779fadcb96d65aa95788 100755 (executable)
@@ -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:
index be599c14eff5fd4167792374cfa56f0f3eb98774..e38d63f78e636346070ef24fa155f6b865bb985b 100644 (file)
@@ -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 (file)
index 0000000..2065a26
--- /dev/null
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8" />
+  <title>pybind_submodules_package.sub | My Python Project</title>
+  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,400i,600,600i%7CSource+Code+Pro:400,400i,600" />
+  <link rel="stylesheet" href="m-dark+documentation.compiled.css" />
+  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+</head>
+<body>
+<header><nav id="navigation">
+  <div class="m-container">
+    <div class="m-row">
+      <a href="index.html" id="m-navbar-brand" class="m-col-t-8 m-col-m-none m-left-m">My Python Project</a>
+    </div>
+  </div>
+</nav></header>
+<main><article>
+  <div class="m-container m-container-inflatable">
+    <div class="m-row">
+      <div class="m-col-l-10 m-push-l-1">
+        <h1>
+          <span class="m-breadcrumb"><a href="pybind_submodules_package.html">pybind_submodules_package</a>.<wbr/></span>sub <span class="m-thin">module</span>
+        </h1>
+        <p>pybind11 submodule of a Python package</p>
+        <div class="m-block m-default">
+          <h3>Contents</h3>
+          <ul>
+            <li>
+              Reference
+              <ul>
+                <li><a href="#packages">Modules</a></li>
+              </ul>
+            </li>
+          </ul>
+        </div>
+        <section id="namespaces">
+          <h2><a href="#namespaces">Modules</a></h2>
+          <dl class="m-doc">
+            <dt>module <a href="pybind_submodules_package.sub.another.html" class="m-doc">another</a></dt>
+            <dd>Yay another</dd>
+            <dt>module <a href="pybind_submodules_package.sub.subsub.html" class="m-doc">subsub</a></dt>
+            <dd>Yay a submodule</dd>
+          </dl>
+        </section>
+      </div>
+    </div>
+  </div>
+</article></main>
+</body>
+</html>
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 (file)
index 0000000..cde8adc
--- /dev/null
@@ -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 (file)
index 0000000..b30985b
--- /dev/null
@@ -0,0 +1,8 @@
+#include <pybind11/pybind11.h>
+
+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");
+}
index b5679d447ee8d7c07c036999e5576c0273d43aed..fb29c0d820ee0837e55a1896bf7fcc167f82e72d 100644 (file)
@@ -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)