[BUG]: pybind11::print raises an exception, when started from pythonw
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- 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`
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in include/pybind11/pybind11.h around lines 2597-2612 and reproduce the failure with a C++ call to pybind11::print launched through pythonw on Windows. Trace how sys.stdout and its write attribute are retrieved; done means the call no longer raises the reported AttributeError when sys.stdout is None.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- backend-api-design, operating-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100