8000 FIX: always decode bytes as utf-8 in AFM headers by tacaswell · Pull Request #8021 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

FIX: always decode bytes as utf-8 in AFM headers #8021

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

Merged
merged 7 commits into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,7 @@ def tk_window_focus():

default_test_modules = [
'matplotlib.tests.test_agg',
'matplotlib.tests.test_afm',
'matplotlib.tests.test_animation',
'matplotlib.tests.test_arrow_patches',
'matplotlib.tests.test_artist',
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/afm.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
def _to_int(x):
return int(float(x))


_to_float = float
if six.PY3:
def _to_str(x):
return x.decode('utf8')
else:
_to_str = str


def _to_str(x):
return x.decode('utf8')


def _to_list_of_ints(s):
Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_afm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
10000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs an encoding comment; this fails on Python 2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

unicode_literals)

import matplotlib.afm as afm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is missing our standard __future__ imports.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the future import is useless for this file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, it might be harmful due to the 'unicode_literals'...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, if anyone adds new tests here, then they won't have to worry about adding in these imports if necessary. Easy consistency is important for tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the kind of practices that break static checkers such as pyflakes with very little benefit considering a forgotten import should break the test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never seen a static checker complain about __future__ imports; how would they even know if you used, e.g., division with types that were integers?

This is about consistent Python 2/3 behaviour, not some random import.



def test_nonascii_str():
# This tests that we also decode bytes as utf-8 properly.
# Else, font files with non ascii characters fail to load.
inp_str = "привет"
byte_str = inp_str.encode("utf8")

ret = afm._to_str(byte_str)
assert ret == inp_str
0