pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

[BUG]: pybind11::print raises an exception, when started from pythonw

Open Wiskerke opened this issue 3 years ago • 0 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [ ] Consider asking first in the Gitter chat room or in a Discussion.

Problem description

When python is started with pythonw calling pybind11::print raises the AttributeError exception: 'NoneType' object has no attribute 'write'.

The problem is:

  • When pythonw is used, sys.stdout has the value None.
  • The pybind11 print implementation retrieves the sys.stdout. From that object the write attribute is retrieved and used. Retrieving that write attribute will fail if sys.stdout is None.

The implementation: https://github.com/pybind/pybind11/blob/b07975f492c2eed0409a18353fa23c9969e83e42/include/pybind11/pybind11.h#L2597-L2612

Note: We found the problem on MS windows, and as far as I know pythonw is only available on MS windows.

Reproducible example code

In any c++ code that is called from python, include a call to `pybind11::print`. 

Then call that code via `pythonw`

Wiskerke avatar Jul 25 '22 14:07 Wiskerke