Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when sorting warning messages if a warning has no line number #10498

Closed
kovidgoyal opened this issue May 31, 2022 · 12 comments · Fixed by #10503
Closed

Exception when sorting warning messages if a warning has no line number #10498

kovidgoyal opened this issue May 31, 2022 · 12 comments · Fixed by #10503

Comments

@kovidgoyal
Copy link

Describe the bug

In sphinx 5.0 there is a new warning about extlinks. This warning has no associated linenumber. This breaks Catalog.iter with the following traceback:

# Sphinx version: 5.0.0
# Python version: 3.10.4 (CPython)
# Docutils version: 0.18.1 release
# Jinja2 version: 3.1.2
# Last messages:
#   writing output... [ 88%] template_lang
#   writing output... [ 90%] tutorials
#   writing output... [ 92%] typesetting_math
#   writing output... [ 94%] url_scheme
#   writing output... [ 96%] viewer
#   writing output... [ 98%] virtual_libraries
#   writing output... [100%] xpath
#   
#   writing message catalogs... [  2%] sphinx
#   writing message catalogs... [  5%] catalogs
# Loaded extensions:
#   sphinx.ext.mathjax (5.0.0) from /usr/lib/python3.10/site-packages/sphinx/ext/mathjax.py
#   sphinxcontrib.applehelp (1.0.2) from /usr/lib/python3.10/site-packages/sphinxcontrib/applehelp/__init__.py
#   sphinxcontrib.devhelp (1.0.2) from /usr/lib/python3.10/site-packages/sphinxcontrib/devhelp/__init__.py
#   sphinxcontrib.htmlhelp (2.0.0) from /usr/lib/python3.10/site-packages/sphinxcontrib/htmlhelp/__init__.py
#   sphinxcontrib.serializinghtml (1.1.5) from /usr/lib/python3.10/site-packages/sphinxcontrib/serializinghtml/__init__.py
#   sphinxcontrib.qthelp (1.0.3) from /usr/lib/python3.10/site-packages/sphinxcontrib/qthelp/__init__.py
#   alabaster (0.7.12) from /usr/lib/python3.10/site-packages/alabaster/__init__.py
#   sphinx.ext.autodoc.preserve_defaults (1.0) from /usr/lib/python3.10/site-packages/sphinx/ext/autodoc/preserve_defaults.py
#   sphinx.ext.autodoc.type_comment (5.0.0) from /usr/lib/python3.10/site-packages/sphinx/ext/autodoc/type_comment.py
#   sphinx.ext.autodoc (5.0.0) from /usr/lib/python3.10/site-packages/sphinx/ext/autodoc/__init__.py
#   custom (unknown version) from /home/kovid/work/calibre/manual/custom.py
#   sidebar_toc (unknown version) from /home/kovid/work/calibre/manual/sidebar_toc.py
#   sphinx.ext.viewcode (5.0.0) from /usr/lib/python3.10/site-packages/sphinx/ext/viewcode.py
#   sphinx.ext.extlinks (5.0.0) from /usr/lib/python3.10/site-packages/sphinx/ext/extlinks.py
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/sphinx/cmd/build.py", line 276, in build_main
    app.build(args.force_all, filenames)
  File "/usr/lib/python3.10/site-packages/sphinx/application.py", line 329, in build
    self.builder.build_update()
  File "/usr/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 288, in build_update
    self.build(to_build,
  File "/usr/lib/python3.10/site-packages/sphinx/builders/gettext.py", line 251, in build
    super().build(docnames, summary, method)
  File "/usr/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 355, in build
    self.finish()
  File "/usr/lib/python3.10/site-packages/sphinx/builders/gettext.py", line 273, in finish
    context['messages'] = list(catalog)
  File "/usr/lib/python3.10/site-packages/sphinx/builders/gettext.py", line 60, in __iter__
    positions = sorted(set((source, line) for source, line, uuid
TypeError: '<' not supported between instances of 'int' and 'NoneType'

The fix is simply to change the sorting of positions to:

            positions = sorted(set((source, line or 0) for source, line, uuid
                                   in self.metadata[message]))

How to Reproduce

Use extlinks with sphinx 5

Expected behavior

No response

Your project

calibre

Screenshots

No response

OS

Linux

Python version

3.10.4

Sphinx version

5.0.0

Sphinx extensions

No response

Extra tools

No response

Additional context

No response

@kovidgoyal
Copy link
Author

Actually it wasnt the extlinks warning. Instead if you have an image with an alt field that has exactly the same text as a preceding heading, you get the message with None as a linenumber.

kovidgoyal added a commit to kovidgoyal/calibre that referenced this issue May 31, 2022
@kovidgoyal
Copy link
Author

Here is the commit I made to workaround it in my project: kovidgoyal/calibre@bece999

@AA-Turner
Copy link
Member

@kovidgoyal do you have a single file reproducer please?

A

@AA-Turner
Copy link
Member

Also, your commit message says "Workaround yet another regression in Sphinx" -- what are the other regressions? Can we fix them? We'd like for upgrading Sphinx versions to be as painless as possible, so any help would be really useful here.

Thanks,
Adam

@kovidgoyal
Copy link
Author

kovidgoyal commented May 31, 2022 via email

@AA-Turner
Copy link
Member

AA-Turner commented May 31, 2022

Edit: the gettext builder needs to be used.

import shutil
from pathlib import Path

from sphinx.cmd.make_mode import run_make_mode

def write(filename, text): Path(filename).write_text(text, encoding="utf-8")


write("conf.py", '''\
extensions = ['sphinx.ext.extlinks']
''')

write("index.rst", '''\
lorem ipsum dolar sit amet
--------------------------

.. image:: placeholder_image.jpg
   :alt: lorem ipsum dolar sit amet
''')

shutil.rmtree("_build", ignore_errors=True)
run_make_mode(["gettext", ".", "_build", "-T"])

run as python reproducer_10498.py

A

@kovidgoyal
Copy link
Author

touch conf.py
sphinx-build -b gettext -t online -t gettext . /tmp/x

@AA-Turner
Copy link
Member

Docutils doesn't set a line number for image nodes with alt text for some reason, reported as bug#449

We could patch Docutils, or add the workaround you suggested -- I'm inclined to go with the latter as it is more general (although might hide similar problems in the future)

A

@kovidgoyal
Copy link
Author

kovidgoyal commented May 31, 2022 via email

@AA-Turner
Copy link
Member

>>> print(core.publish_doctree(".. image:: placeholder_image.jpg")[0].line)
1
>>> print(core.publish_doctree(".. image:: placeholder_image.jpg\n   :alt: blah")[0].line)
None

This example has no title, yet still fails.

A

@AA-Turner
Copy link
Member

A proper workaround is to check for no line number and issue a warning and then use the sort fix I suggested.

The PR has been updated to issue a warning, thanks for the suggestion.

A

@tk0miya tk0miya added this to the 5.0.1 milestone Jun 1, 2022
@tk0miya tk0miya closed this as completed Jun 1, 2022
@tk0miya
Copy link
Member

tk0miya commented Jun 1, 2022

Fixed by #10503. It will be released as v5.0.1 soon (within a few days). Please wait for a while.
Thank you for your report.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants