sphinx-contrib / fulltoc

Extension for Sphinx to make the sidebar show a full table of contents instead of just the local headings
Apache License 2.0
38 stars 20 forks source link

make_toctree Not JSON serializable #10

Closed jambonrose closed 7 years ago

jambonrose commented 9 years ago

I'm running into problems when building documentation on Read the Docs. The command being used is:

sphinx-build -T -b json -d _build/doctrees-json -D language=en . _build/json

This results in an error that states that:

TypeError: <function html_page_context.<locals>.make_toctree at 0x10d954510> is not JSON serializable

The full output:

Running Sphinx v1.3.1
loading translations [en]... done
loading pickled environment... not yet created
building [mo]: targets for 0 po files that are out of date
building [json]: targets for 4 source files that are out of date
updating environment: 4 added, 0 changed, 0 removed
reading sources... [100%] usage                                                                                                                                                   
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [ 25%] index                                                                                                                                                    
Traceback (most recent call last):
  File "python3.4/site-packages/sphinx/cmdline.py", line 245, in main
    app.build(opts.force_all, filenames)
  File "python3.4/site-packages/sphinx/application.py", line 264, in build
    self.builder.build_update()
  File "python3.4/site-packages/sphinx/builders/__init__.py", line 245, in build_update
    'out of date' % len(to_build))
  File "python3.4/site-packages/sphinx/builders/__init__.py", line 316, in build
    self.write(docnames, list(updated_docnames), method)
  File "python3.4/site-packages/sphinx/builders/__init__.py", line 354, in write
    self._write_serial(sorted(docnames), warnings)
  File "python3.4/site-packages/sphinx/builders/__init__.py", line 362, in _write_serial
    self.write_doc(docname, doctree)
  File "python3.4/site-packages/sphinx/builders/html.py", line 445, in write_doc
    self.handle_page(docname, ctx, event_arg=doctree)
  File "python3.4/site-packages/sphinx/builders/html.py", line 1082, in handle_page
    self.dump_context(ctx, outfilename)
  File "python3.4/site-packages/sphinx/builders/html.py", line 1064, in dump_context
    self.implementation.dump(context, f, *self.additional_dump_args)
  File "python3.4/site-packages/sphinx/util/jsonimpl.py", line 28, in dump
    return json.dump(obj, fp, *args, **kwds)
  File "python3.4/json/__init__.py", line 178, in dump
    for chunk in iterable:
  File "python3.4/json/encoder.py", line 422, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "python3.4/json/encoder.py", line 396, in _iterencode_dict
    yield from chunks
  File "python3.4/json/encoder.py", line 429, in _iterencode
    o = _default(o)
  File "python3.4/site-packages/sphinx/util/jsonimpl.py", line 23, in default
    return json.JSONEncoder.default(self, obj)
  File "python3.4/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <function html_page_context.<locals>.make_toctree at 0x10d954510> is not JSON serializable

Exception occurred:
  File "python3.4/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <function html_page_context.<locals>.make_toctree at 0x10d954510> is not JSON serializable
The full traceback has been saved in /var/folders/44/znmnm9yd1xz8f98vyb4n3j7w0000gn/T/sphinx-err-d_55ezjk.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!

I am running Python 3.4 on Mac OS 10.10 (Yosemite). My current Sphinx extensions are:

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.doctest',
    'sphinx.ext.coverage',
    'sphinx.ext.viewcode',
    'sphinxcontrib.fulltoc',
]

Removing sphinxcontrib.fulltoc removes the problem.

sonofeft commented 9 years ago

I was having the same problem on ReadTheDocs and it seems to be solved by commenting out the "make_toctree" function inside the "html_page_context" function. In my build make_toctree was not being called, so I took a chance and tried commenting it out. That seems to work.

My "fix" is shown below

def html_page_context(app, pagename, templatename, context, doctree):
    """Event handler for the html-page-context signal.

    Modifies the context directly.

     - Replaces the 'toc' value created by the HTML builder with one
       that shows all document titles and the local table of contents.
     - Sets display_toc to True so the table of contents is always
       displayed, even on empty pages.
     - Replaces the 'toctree' function with one that uses the entire
       document structure, ignores the maxdepth argument, and uses
       only prune and collapse.
    """
    rendered_toc = get_rendered_toctree(app.builder, pagename)
    context['toc'] = rendered_toc
    context['display_toc'] = True  # force toctree to display

    #def make_toctree(collapse=True):
    #    result = get_rendered_toctree(app.builder,
    #                                  pagename,
    #                                  prune=False,
    #                                  collapse=collapse,
    #                                  )
    #    #sys.__stderr__.write( 'From make_toctree, result =' + repr(result) )
    #    return result
    #context['toctree'] =  make_toctree
reece commented 9 years ago

@dhellmann Do you have a recommendation for how to solve this problem? It appears to me that fulltoc is incompatible with sphinx json support, which is required for RTD. Furthermore, according to @ericholscher in https://github.com/rtfd/readthedocs.org/issues/1676, RTD now fails builds when json fails. Commenting out context['toctree'] = make_toctree and does not seem to affect my docs, which makes me wonder whether it's used at all.

jambonrose commented 9 years ago

@reece I believe the recommendation at this point is to use the RTD template, whichprovides the same behavior as the fulltoc project.

https://read-the-docs.readthedocs.org/en/latest/theme.html https://github.com/snide/sphinx_rtd_theme

reece commented 9 years ago

@jambonrose Thanks... and drat... I'd like to not have to change themes.

eli-collins commented 8 years ago

I've added a pull request which (I think) fixes this issue. Please let me know if anyone finds glitches in the approach I used. (PR #11)

electrofelix commented 7 years ago

This was fixed in PR #11, hope to release soon.