felix-hilden/sphinx-codeautolink

Crash on block containing invalid syntax

Closed this issue ยท 6 comments

Found by https://github.com/HypothesisWorks/hypothesis/actions/runs/4095444626/jobs/7062360717#step:6:483 and other builds on HypothesisWorks/hypothesis#3555.

Traceback (most recent call last):
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/block.py", line 220, in parse_source
    names = parse_names(modified_source, node)
  File "lib/python3.10/site-packages/sphinx_codeautolink/parse.py", line 20, in parse_names
    tree = ast.parse(source)
  File "/opt/hostedtoolcache/Python/3.10.9/x64/lib/python3.10/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 6
    Falsifying example: test_x_divided_by_y(
               ^^^^^^^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/__init__.py", line 43, in wrapper
    return func(*args, **kwargs)
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/__init__.py", line 137, in parse_blocks
    doctree.walkabout(visitor)
  File "lib/python3.10/site-packages/docutils/nodes.py", line 186, in walkabout
    if child.walkabout(visitor):
  File "lib/python3.10/site-packages/docutils/nodes.py", line 186, in walkabout
    if child.walkabout(visitor):
  File "lib/python3.10/site-packages/docutils/nodes.py", line 186, in walkabout
    if child.walkabout(visitor):
  [Previous line repeated 2 more times]
  File "lib/python3.10/site-packages/docutils/nodes.py", line 178, in walkabout
    visitor.dispatch_visit(self)
  File "lib/python3.10/site-packages/docutils/nodes.py", line 2005, in dispatch_visit
    return method(node)
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/block.py", line 173, in visit_literal_block
    return self.parse_source(node, node.get("language", None))
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/block.py", line 222, in parse_source
    show_source = self._format_source_for_error(source, prefaces)
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/block.py", line 253, in _format_source_for_error
    guides[ix] = "block source:"
IndexError: list assignment index out of range

Traceback (most recent call last):
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/block.py", line 220, in parse_source
    names = parse_names(modified_source, node)
  File "lib/python3.10/site-packages/sphinx_codeautolink/parse.py", line 20, in parse_names
    tree = ast.parse(source)
  File "/opt/hostedtoolcache/Python/3.10.9/x64/lib/python3.10/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 6
    Falsifying example: test_x_divided_by_y(
               ^^^^^^^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "lib/python3.10/site-packages/sphinx/events.py", line 96, in emit
    results.append(listener.handler(self.app, *args))
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/__init__.py", line 43, in wrapper
    return func(*args, **kwargs)
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/__init__.py", line 137, in parse_blocks
    doctree.walkabout(visitor)
  File "lib/python3.10/site-packages/docutils/nodes.py", line 186, in walkabout
    if child.walkabout(visitor):
  File "lib/python3.10/site-packages/docutils/nodes.py", line 186, in walkabout
    if child.walkabout(visitor):
  File "lib/python3.10/site-packages/docutils/nodes.py", line 186, in walkabout
    if child.walkabout(visitor):
  [Previous line repeated 2 more times]
  File "lib/python3.10/site-packages/docutils/nodes.py", line 178, in walkabout
    visitor.dispatch_visit(self)
  File "lib/python3.10/site-packages/docutils/nodes.py", line 2005, in dispatch_visit
    return method(node)
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/block.py", line 173, in visit_literal_block
    return self.parse_source(node, node.get("language", None))
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/block.py", line 222, in parse_source
    show_source = self._format_source_for_error(source, prefaces)
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/block.py", line 253, in _format_source_for_error
    guides[ix] = "block source:"
IndexError: list assignment index out of range (in document `hypothesis-python/docs/settings.rst`)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "lib/python3.10/site-packages/sphinx/cmd/build.py", line 284, in build_main
    app.build(args.force_all, args.filenames)
  File "lib/python3.10/site-packages/sphinx/application.py", line 347, in build
    self.builder.build_update()
  File "lib/python3.10/site-packages/sphinx/builders/__init__.py", line 311, in build_update
    self.build(to_build,
  File "lib/python3.10/site-packages/sphinx/builders/__init__.py", line 327, in build
    updated_docnames = set(self.read())
  File "lib/python3.10/site-packages/sphinx/builders/__init__.py", line 434, in read
    self._read_serial(docnames)
  File "lib/python3.10/site-packages/sphinx/builders/__init__.py", line 455, in _read_serial
    self.read_doc(docname)
  File "lib/python3.10/site-packages/sphinx/builders/__init__.py", line 511, in read_doc
    publisher.publish()
  File "lib/python3.10/site-packages/docutils/core.py", line 226, in publish
    self.apply_transforms()
  File "lib/python3.10/site-packages/docutils/core.py", line 206, in apply_transforms
    self.document.transformer.apply_transforms()
  File "lib/python3.10/site-packages/sphinx/transforms/__init__.py", line 80, in apply_transforms
    super().apply_transforms()
  File "lib/python3.10/site-packages/docutils/transforms/__init__.py", line 173, in apply_transforms
    transform.apply(**kwargs)
  File "lib/python3.10/site-packages/sphinx/transforms/__init__.py", line 359, in apply
    self.app.emit('doctree-read', self.document)
  File "lib/python3.10/site-packages/sphinx/application.py", line 458, in emit
    return self.events.emit(event, *args, allowed_exceptions=allowed_exceptions)
  File "lib/python3.10/site-packages/sphinx/events.py", line 107, in emit
    raise ExtensionError(__("Handler %r for event %r threw an exception") %
sphinx.errors.ExtensionError: Handler <bound method SphinxCodeAutoLink.parse_blocks of <sphinx_codeautolink.extension.SphinxCodeAutoLink object at 0x7fd[503](https://github.com/HypothesisWorks/hypothesis/actions/runs/4095444626/jobs/7062360717#step:6:504)ec1180>> for event 'doctree-read' threw an exception (exception: list assignment index out of range (in document `/home/runner/work/hypothesis/hypothesis/hypothesis-python/docs/settings.rst`))

of which the relevant portion seems to be:

  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/block.py", line 222, in parse_source
    show_source = self._format_source_for_error(source, prefaces)
  File "lib/python3.10/site-packages/sphinx_codeautolink/extension/block.py", line 253, in _format_source_for_error
    guides[ix] = "block source:"
IndexError: list assignment index out of range

I'm not entirely sure why this IndexError happens. I don't have time this evening to cut it down to a minimal repro but thought it better to report with a large reproducer than not report at all.

Thanks for reporting! I'll address it right away, it was fairly easy to reproduce ๐Ÿ‘Œ

Available now in sphinx-codeautolink==0.15.0

Legend, thanks again for maintaining such a great utility!

๐Ÿ˜„ you're most welcome!

Just ran into this again, it looks like the 0.15 release didn't actually make it to https://pypi.org/project/sphinx-codeautolink/ ?

Oh damn ๐Ÿ˜„ alright my bad! Definitely available now. Thanks for letting me know ๐Ÿ™