| 1 | [Bug]: Text label with empty line causes a "TypeError: cannot unpack non-iterable NoneType object" in PostScript backend |
| 2 | ### Bug summary |
| 3 | |
| 4 | When saving a figure with the PostScript backend, a |
| 5 | > TypeError: cannot unpack non-iterable NoneType object |
| 6 | |
| 7 | happens if the figure contains a multi-line text label with an empty line (see example). |
| 8 | |
| 9 | ### Code for reproduction |
| 10 | |
| 11 | ```python |
| 12 | from matplotlib.figure import Figure |
| 13 | |
| 14 | figure = Figure() |
| 15 | ax = figure.add_subplot(111) |
| 16 | # ax.set_title('\nLower title') # this would cause an error as well |
| 17 | ax.annotate(text='\nLower label', xy=(0, 0)) |
| 18 | figure.savefig('figure.eps') |
| 19 | ``` |
| 20 | |
| 21 | |
| 22 | ### Actual outcome |
| 23 | |
| 24 | $ ./venv/Scripts/python save_ps.py |
| 25 | Traceback (most recent call last): |
| 26 | File "C:\temp\matplotlib_save_ps\save_ps.py", line 7, in <module> |
| 27 | figure.savefig('figure.eps') |
| 28 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\figure.py", line 3272, in savefig |
| 29 | self.canvas.print_figure(fname, **kwargs) |
| 30 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\backend_bases.py", line 2338, in print_figure |
| 31 | result = print_method( |
| 32 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\backend_bases.py", line 2204, in <lambda> |
| 33 | print_method = functools.wraps(meth)(lambda *args, **kwargs: meth( |
| 34 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\_api\deprecation.py", line 410, in wrapper |
| 35 | return func(*inner_args, **inner_kwargs) |
| 36 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\backends\backend_ps.py", line 869, in _print_ps |
| 37 | printer(fmt, outfile, dpi=dpi, dsc_comments=dsc_comments, |
| 38 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\backends\backend_ps.py", line 927, in _print_figure |
| 39 | self.figure.draw(renderer) |
| 40 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\artist.py", line 74, in draw_wrapper |
| 41 | result = draw(artist, renderer, *args, **kwargs) |
| 42 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper |
| 43 | return draw(artist, renderer) |
| 44 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\figure.py", line 3069, in draw |
| 45 | mimage._draw_list_compositing_images( |
| 46 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\image.py", line 131, in _draw_list_compositing_images |
| 47 | a.draw(renderer) |
| 48 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper |
| 49 | return draw(artist, renderer) |
| 50 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\axes\_base.py", line 3106, in draw |
| 51 | mimage._draw_list_compositing_images( |
| 52 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\image.py", line 131, in _draw_list_compositing_images |
| 53 | a.draw(renderer) |
| 54 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper |
| 55 | return draw(artist, renderer) |
| 56 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\text.py", line 1995, in draw |
| 57 | Text.draw(self, renderer) |
| 58 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper |
| 59 | return draw(artist, renderer) |
| 60 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\text.py", line 736, in draw |
| 61 | textrenderer.draw_text(gc, x, y, clean_line, |
| 62 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\backends\backend_ps.py", line 248, in wrapper |
| 63 | return meth(self, *args, **kwargs) |
| 64 | File "C:\temp\matplotlib_save_ps\venv\lib\site-packages\matplotlib\backends\backend_ps.py", line 673, in draw_text |
| 65 | for ps_name, xs_names in stream: |
| 66 | TypeError: cannot unpack non-iterable NoneType object |
| 67 | |
| 68 | |
| 69 | ### Expected outcome |
| 70 | |
| 71 | The figure can be saved as `figure.eps` without error. |
| 72 | |
| 73 | ### Additional information |
| 74 | |
| 75 | - seems to happen if a text label or title contains a linebreak with an empty line |
| 76 | - works without error for other backends such as PNG, PDF, SVG, Qt |
| 77 | - works with matplotlib<=3.5.3 |
| 78 | - adding `if curr_stream:` before line 669 of `backend_ps.py` seems to fix the bug |
| 79 | |
| 80 | ### Operating system |
| 81 | |
| 82 | Windows |
| 83 | |
| 84 | ### Matplotlib Version |
| 85 | |
| 86 | 3.6.0 |
| 87 | |
| 88 | ### Matplotlib Backend |
| 89 | |
| 90 | _No response_ |
| 91 | |
| 92 | ### Python version |
| 93 | |
| 94 | 3.9.13 |
| 95 | |
| 96 | ### Jupyter version |
| 97 | |
| 98 | _No response_ |
| 99 | |
| 100 | ### Installation |
| 101 | |
| 102 | pip |