WhyNotHugo / sphinx-autorun

Sphinx extension to execute the code output it into a document.
https://pypi.python.org/pypi/sphinx-autorun
BSD 2-Clause "Simplified" License
15 stars 3 forks source link

Problems with code that outputs unicode #18

Closed petercorke closed 4 years ago

petercorke commented 4 years ago

Firstly, this is a really useful tool, and a timely discovery for me. Thanks.

For a function that outputs the unicode symbol for degrees an error is raised

  File ".../opt/miniconda3/envs/dev/lib/python3.8/site-packages/docutils/parsers/rst/states.py", line 2146, in run_directive
    result = directive_instance.run()
  File ".../opt/miniconda3/envs/dev/lib/python3.8/site-packages/sphinx_autorun/__init__.py", line 73, in run
    out = stdout.decode(output_encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 186: ordinal not in range(128)

I hacked the offending line, changing 'ascii' to 'UTF-8' and that works fine.

Looking at doing this nicely in conf.py I would have thought that adding

autorun_languages['pycon_output_encoding'] = 'UTF-8'

would do the trick, but I get

NameError: name 'autorun_languages' is not defined

It looks like setup() makes this available, so now I'm confused. All other sphinx extensions and their configuration are working just fine.

It would be good to document this encoding option.

petercorke commented 4 years ago

Solved!

autorun_languages = {}
autorun_languages['pycon_output_encoding'] = 'UTF-8'

does the trick. I thought the dict was already created, just need to initialise it.