Documenting a pybind11 embedded module from the application

205 views
Skip to first unread message

Gonzalo Garramuño

unread,
Mar 26, 2023, 11:25:25 AM3/26/23
to sphinx...@googlegroups.com
I have a pybind11 module in an application with an embedded executable
that is created with:

PYBIND11_EMBEDDED_MODULE(mrv2, m) { ... }

Thus, it becomes a built-in module.  This works fine, the module is
imported, its help() can be seen, etc.

However, as there's no .so I cannot easily document it with Sphinx (or
the tutorials don't show me how).

The application with the embedded python has a console and the ability
to run arbitrary scripts.
I was hoping I could call Sphinx from a script running in the embedded
python to create the documentation.  This seems to be almost working,
but due to my unfamiliarity with Sphinx, I am unable to have it
recognize the mrv2 embedded module as the one to document.

ChatGPT offered some help, but not enough.  I have the following script:

import os
import sys
import site

# My module to document
import mrv2

# Import pip path
pip_dir = site.USER_SITE
sys.path.append(pip_dir)

# Load Sphinx modules
from sphinx.application import Sphinx
from sphinx.ext import autodoc

# Define a custom autodoc documenter for Pybind11 modules
class Pybind11ModuleDocumenter(autodoc.ModuleDocumenter):
    objtype = "pybind11 module"

    def can_document_member(self, member, membername, isattr, parent):
        return False

    # This never gets called, which is probably the issue.
    def import_object(self):
        print("Importing module:", self.modname)
        self.object = mrv2  # module to document
        return True

# root dir
root_dir = "/media/gga/datos/code/applications/mrv2/"
sphinx_dir = root_dir + "/sphinx"

# Set up the Sphinx application
source_dir = os.path.abspath( sphinx_dir )
build_dir = os.path.abspath( sphinx_dir + '/build')
doctree_dir = os.path.abspath( sphinx_dir + '/doctrees')
conf_dir = os.path.abspath( sphinx_dir )
app = Sphinx(source_dir, conf_dir, build_dir, doctree_dir, freshenv=True,
             warningiserror=True, buildername='html')

# Add the custom documenter
# This seems not to do anything
app.registry.documenters["pybind11 module"] = Pybind11ModuleDocumenter

# Build the documentation
app.builder.build_all()

# Build the html web pages
app.build()

# Remove the custom documenter
del app.registry.documenters["pybind11 module"]

Any ideas on what might be wrong?  Do I have to do some additional work
on the index.rst or conf.py file?


Reply all
Reply to author
Forward
0 new messages