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

UnicodeDecodeError on importing pyplot in python2 #11955

Closed
AndreySV opened this issue Aug 28, 2018 · 4 comments
Closed

UnicodeDecodeError on importing pyplot in python2 #11955

AndreySV opened this issue Aug 28, 2018 · 4 comments
Milestone

Comments

@AndreySV
Copy link

AndreySV commented Aug 28, 2018

Bug report

Bug summary

UnicodeDecodeError on importing pyplot If some module in call stack is located in directory with non-ascii character in path. The problem is that traceback.format_stack() returns list of 'str' objects in python2. This problem happens only on python2.

Code for reproduction
Create two modules in a directory with a non-ascii character in the path.

#!/usr/bin/env python
# first_module.py
import second_module
# second_module.py
import matplotlib.pyplot

Run first module.

$ python2 first_module.py

Actual outcome

Following traceback happens

Traceback (most recent call last):
  File "./a.py", line 3, in <module>
    import second_module
  File "/home/developer/WorkData/PLC/beremiz/tmp/xыв/beremiz/second_module.py", line 3, in <module>
    import matplotlib.pyplot
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 71, in <module>
    from matplotlib.backends import pylab_setup
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/__init__.py", line 16, in <module>
    line for line in traceback.format_stack()
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/__init__.py", line 18, in <genexpr>
    if not line.startswith('  File "<frozen importlib._bootstrap'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 50: ordinal not in range(128)

Matplotlib version

  • Operating system: Debian Buster
  • Matplotlib version: 2.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): 'WX"
  • Python version: 2.7.15
@AndreySV
Copy link
Author

Suggested in comment change:

_backend_loading_tb = b"".join(
    line for line in traceback.format_stack()
    # Filter out line noise from importlib line.
    if not line.startswith(b'  File "<frozen importlib._bootstrap'))

seems to fix the problem. In python2 crash doesn't happen anymore and the code looks to work with python 3.6 either.

@tacaswell tacaswell added this to the v2.2.4 milestone Aug 28, 2018
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Aug 28, 2018
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Aug 28, 2018
The issue is that the lines coming out of `traceback.format_stack()`
are bytes (aka python2 str).  This file uses `unicode_literals` so
the string literals are unicode.  If any of the paths in the stack
have non-ascii we get UnicodeDecode exceptions when trying to convert
the byte strings to unicode with ascii.

The `str` calls will have no effect on python3 and down-cast the
unicode to bytes so the operations will work.

A better fix would be to sort out what encoding the bytes from
`format_stack` are in and convert them to unicode, but this is simpler
and is unlikely to make things worse than they were.

closes matplotlib#11955
@tacaswell
Copy link
Member

@AndreySV Can you try the fix in #11957 ? It is slightly different, but should have the same effect.

@AndreySV
Copy link
Author

all good like originally suggested changed.

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Aug 28, 2018
The issue is that the lines coming out of `traceback.format_stack()`
are bytes (aka python2 str).  This file uses `unicode_literals` so
the string literals are unicode.  If any of the paths in the stack
have non-ascii we get UnicodeDecode exceptions when trying to convert
the byte strings to unicode with ascii.

The `str` calls will have no effect on python3 and down-cast the
unicode to bytes so the operations will work.

A better fix would be to sort out what encoding the bytes from
`format_stack` are in and convert them to unicode, but this is simpler
and is unlikely to make things worse than they were.

closes matplotlib#11955
@Zac-HD
Copy link
Contributor

Zac-HD commented Sep 26, 2018

@tacaswell looks like merging #11957 didn't automatically close this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants