Non-ascii 8-bit string handling #494
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an alternative solution to pull request #492.
The unicode() call in is_math_text() is strictly unnecessary to get things done, but it provides an important early warning to the user that they are probably doing something wrong (and is_math_text is called on all "displayed" text in maplotlib, so it's a convenient place to do this check).
If the user passes in any non-ascii 8-bit string, the encoding is "unknown" (one can assume for particular locales and platforms, but not in the general sense), and therefore since the encoding is very likely to not match the font selected, we may get the wrong or missing characters in the output, therefore raising an exception is the right thing to do. Think of this as shorthand for:
if not is_unicode(s) and [s contains codepoints > 127]: throw
A lot of this confusion goes away in the Python 3 world.
This pull request improves the error message when the user passes in an 8-bit string containing non-ascii characters.
It also fixes a bug in font_table_ttf where it is passing non-ascii characters in an 8-bit string rather than using Unicode.