Skip to content

Fix TypeError: a bytes-like object is required, not 'str' - #9292

Merged
tacaswell merged 2 commits into
matplotlib:masterfrom
cgohlke:patch-1
Oct 23, 2017
Merged

tacaswell merged 2 commits into
matplotlib:masterfrom
cgohlke:patch-1

Conversation

@cgohlke

@cgohlke cgohlke commented Oct 6, 2017

Copy link
Copy Markdown
Contributor

Fix multipage_pdf.py failing on Python 3 (tested on Windows with matplotlib-2.1):

Traceback (most recent call last):
  File "multipage_pdf.py", line 33, in <module>
    pdf.savefig()
  File "X:\Python36\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2537, in savefig
    **kwargs)
  File "X:\Python36\lib\site-packages\matplotlib\figure.py", line 1814, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "X:\Python36\lib\site-packages\matplotlib\backend_bases.py", line 2259, in print_figure
    **kwargs)
  File "X:\Python36\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2592, in print_pdf
    self.figure.draw(renderer)
  File "X:\Python36\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "X:\Python36\lib\site-packages\matplotlib\figure.py", line 1295, in draw
    renderer, self, artists, self.suppressComposite)
  File "X:\Python36\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "X:\Python36\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "X:\Python36\lib\site-packages\matplotlib\axes\_base.py", line 2399, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "X:\Python36\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "X:\Python36\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "X:\Python36\lib\site-packages\matplotlib\axis.py", line 1138, in draw
    tick.draw(renderer)
  File "X:\Python36\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "X:\Python36\lib\site-packages\matplotlib\axis.py", line 282, in draw
    self.label1.draw(renderer)
  File "X:\Python36\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "X:\Python36\lib\site-packages\matplotlib\text.py", line 799, in draw
    mtext=mtext)
  File "X:\Python36\lib\site-packages\matplotlib\backends\backend_pdf.py", line 1944, in draw_tex
    pdfname = self.file.dviFontName(dvifont)
  File "X:\Python36\lib\site-packages\matplotlib\backends\backend_pdf.py", line 688, in dviFontName
    psfont = self.texFontMap[dvifont.texname]
  File "X:\Python36\lib\site-packages\matplotlib\dviread.py", line 866, in __getitem__
    fn = find_tex_file(fn)
  File "X:\Python36\lib\site-packages\matplotlib\dviread.py", line 1045, in find_tex_file
    stderr=subprocess.PIPE)
  File "X:\Python36\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "X:\Python36\lib\subprocess.py", line 971, in _execute_child
    args = list2cmdline(args)
  File "X:\Python36\lib\subprocess.py", line 461, in list2cmdline
    needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: a bytes-like object is required, not 'str'

Comment thread lib/matplotlib/dviread.py

if six.PY3:
if isinstance(filename, bytes):
filename = filename.decode('utf-8', errors='replace')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should probably use https://docs.python.org/3/library/os.html#os.fsdecode here?
For format I dunno what is the correct encoding; that probably is deep into the kpsewhich sources but I guess all relevant formats have ASCII compatible names so utf-8 should just be fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure the filesystem encoding is a better choice for a function searching for a file. Is this even relevant? MikTeX's kpsewhich does not find any non-ASCII file name.

@anntzer anntzer Oct 6, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

then perhaps add a comment that in practice the encoding is irrelevant -- other than being ASCII-compatible -- because you don't expect non-ASCII filenames to be found (to avoid later head-scratching as to why utf-8).

@tacaswell tacaswell added this to the 2.1.1 (next bug fix release) milestone Oct 6, 2017
@tacaswell

Copy link
Copy Markdown
Member

@cgohlke You mind if I push to your branch?

@anntzer anntzer added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Oct 14, 2017
@tacaswell

Copy link
Copy Markdown
Member

@anntzer I added a line comment about the encoding.

@anntzer anntzer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

waiting for tests

@tacaswell

Copy link
Copy Markdown
Member

The doc failure is un-related and fixed on all the active branches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants