自定义 Sphinx 指令给出酸洗错误

Custom Sphinx directive gives a pickling error

我正在尝试在我的 conf.py 上注册自定义 Sphinx 指令,如下所示:

diff --git a/docs/conf.py b/docs/conf.py
index fda031a72..2d829602d 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -2,6 +2,10 @@ import os
 import sys
 from configparser import RawConfigParser
 
+from docutils import nodes
+from docutils.parsers.rst import directives
+from docutils.parsers.rst.directives.admonitions import BaseAdmonition
+
 import sphinx_rtd_theme
 
 sys.path.insert(0, os.path.abspath('..'))
@@ -153,5 +157,18 @@ linkcheck_ignore = [
 ]
 
 
+class commercial(nodes.Admonition, nodes.Element):
+    pass
+
+
+class Commercial(BaseAdmonition):
+
+    node_class = commercial
+
+
 def setup(app):
     app.add_css_file('css/sphinx_prompt_css.css')
+
+    app.add_node(commercial)
+
+    directives.register_directive('commercial', Commercial)

但我的 make html 失败并出现此错误:

Exception occurred:
  File "/home/juanlu/Projects/RTD/readthedocs.org/.venv/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 501, in write_doctree
    pickle.dump(doctree, f, pickle.HIGHEST_PROTOCOL)
_pickle.PicklingError: Can't pickle <class 'commercial'>: attribute lookup commercial on builtins failed

完整追溯:https://pastebin.com/fgHdDpJK

有人知道这里发生了什么吗?

这是不可能的,请参阅 this issue, and this one, and this one, and these docs:

It is important to know that while you can extend Sphinx without leaving your conf.py, if you declare an inherited node right there, you’ll hit an unobvious PickleError. So if something goes wrong, please make sure that you put inherited nodes into a separate Python module.