Skip to content

"import matplotlib.pyplot" fails on NetBSD with version 2.2.2 #11635

Closed
@gson1703

Description

@gson1703

Bug report

As originally reported in http://gnats.netbsd.org/53425 , with matplotlib version 2.2.2, attempting to import matplotlib.pyplot on a NetBSD system fails with:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/pyplot.py", line 31, in <module>
    import matplotlib.colorbar
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
    import matplotlib.contour as contour
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/contour.py", line 20, in <module>
    import matplotlib.font_manager as font_manager
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1469, in <module>
    _rebuild()
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1450, in _rebuild
    fontManager = FontManager()
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1086, in __init__
    self.ttflist = createFontList(self.ttffiles)
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 587, in createFontList
    font = ft2font.FT2Font(fpath)
TypeError: First argument must be a path or file object reading bytes

I tracked down the cause of this to mpl_PyFile_Dup() attempting to call the "flush" method of a file object that is open only for reading. Python implements this by calling fflush(), and calling fflush() on a file descriptor not open for writing is treated as an error on BSD systems (but silently ignored on Linux). The fflush() call returns -1 with errno=EBADF, and this ultimately gets translated into the rather unhelpful error message "TypeError: First argument must be a path or file object reading bytes" show above.

The call chain leading to the error is PyFT2Font_init() -> convert_open_args() -> mpl_PyFile_Dup().

Code for reproduction

On a BSD system, run

python2.7 -c 'import matplotlib.pyplot'

Actual outcome

The stack trace shown above is printed.

Expected outcome

The command silently exits with a success status.

Matplotlib version

  • Operating system: NetBSD 7.1_STABLE
  • Matplotlib version: 2.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): GTKAgg
  • Python version: 2.7.15
  • Jupyter version (if applicable): N/A
  • Other libraries: N/A

matplotlib was installed via pkgsrc.

I have a patch which I will attempt to submit as a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions