#1035578 fails to write image with TypeError: write() argument must be str, not bytes

Package:
python3-matplotlib
Source:
python3-matplotlib
Description:
Python based plotting system in a style similar to Matlab (Python 3)
Submitter:
Antoine Beaupre
Date:
2023-05-15 06:42:03 UTC
Severity:
normal
#1035578#5
Date:
2023-05-05 19:26:17 UTC
From:
To:
I have this tiny script which generates a silly graph of cryptographic
primitives here:

https://gitlab.com/anarcat/crypto-bench/-/blob/master/benchdiceware.py

The details are rather irrelevant here, but it eventually does this:

        plt.savefig(args.output,
                    format=args.output.name[-3:])

... which is basically:

    plt.savefig("foo.png", "png")

This used to work fine in Python 2. Now that we switched to Python 3,
this explodes with:

anarcat@angela:crypto-bench$ ./benchpasswords.py -o benchdiceware.png
Traceback (most recent call last):
  File "/home/anarcat/src/crypto-bench/./benchpasswords.py", line 102, in <module>
    render_graph(args.output)
  File "/home/anarcat/src/crypto-bench/./benchpasswords.py", line 90, in render_graph
    plt.savefig(args.output,
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 959, in savefig
    res = fig.savefig(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 3285, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 2338, in print_figure
    result = print_method(
             ^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 2204, in <lambda>
    print_method = functools.wraps(meth)(lambda *args, **kwargs: meth(
                                                                 ^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/_api/deprecation.py", line 410, in wrapper
    return func(*inner_args, **inner_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 517, in print_png
    self._print_pil(filename_or_obj, "png", pil_kwargs, metadata)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 464, in _print_pil
    mpl.image.imsave(
  File "/usr/lib/python3/dist-packages/matplotlib/image.py", line 1667, in imsave
    image.save(fname, **pil_kwargs)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2431, in save
    save_handler(self, fp, filename)
  File "/usr/lib/python3/dist-packages/PIL/PngImagePlugin.py", line 1307, in _save
    fp.write(_MAGIC)
TypeError: write() argument must be str, not bytes
anarcat@angela:crypto-bench[1]$

It looks like matplotlib is calling PIL wrong. Or PIL is wrong itself,
I'm not sure where to lay the blame here...

a.

#1035578#10
Date:
2023-05-15 06:38:06 UTC
From:
To:
control: reassign -1 python3-pil

the crash is in PIL, so reassigning, and it also seems to be
potentially limited to the PNG format: i've tried with jpg and eps and
they both worked