pybind / pybind/pybind11

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

Open
#4,088 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
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`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.