pybind11
pybind11 copied to clipboard
[BUG]: pybind11::print raises an exception, when started from pythonw
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
pythonwis used,sys.stdouthas the valueNone. - The pybind11 print implementation retrieves the
sys.stdout. From that object thewriteattribute is retrieved and used. Retrieving thatwriteattribute will fail ifsys.stdoutisNone.
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`