OpenNTI / sphinxcontrib-programoutput

Sphinx extension for capturing program output
BSD 3-Clause "New" or "Revised" License
37 stars 17 forks source link

Cannot handle Unicode characters with Python 2.7 #32

Closed ghost closed 5 years ago

ghost commented 5 years ago

The following snippet fails with an UnicodeEncodeError on Python 2.7:

.. command-output:: echo "U+2264 ≤ LESS-THAN OR EQUAL TO"

The error is

  File ".../sphinxcontrib-programoutput/src/sphinxcontrib/programoutput/__init__.py", line 245, in run_programs
    returncode=returncode)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2264' in position 13: ordinal not in range(128)

The internet(tm) suggested that when using pattern.format(str) both str and pattern have to have the same encoding. In the above example pattern is ASCII where as str is the above UTF-8 string. The solution is to make them both UTF-8.

Python 3 does not seem to have this problem (probably due to the unicode change of str in Python 3).

Workaround: Add

programoutput_prompt_template = u'$ {command}\n{output}'

to your Sphinx conf.py (Note the u infront of the string which marks it as UTF-8).

The commits Attached three commits

Feel free to use them as you wish.

ghost commented 5 years ago

To be clear: This pull request is not directly intended for merging (due to the Deprecation warnings introduced in the 3rd commit) but giving you the patches for easier inspection.

ghost commented 5 years ago

Any news on this?

jamadden commented 5 years ago

Thanks for this PR! I've updated the fixes in it and released it to PyPI as 0.14.

ghost commented 5 years ago

@jamadden Thanks for taking care of this.