python / python/cpython

pythonw should set `sys.stdout` and `sys.stdin` to valid objects.

Aperta
#122,633 43 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

OS-windows type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

Bug description:

So not exactly a bug -- but I wouldn't call it a feature request either, so I filed it as a bug.

The issue is that pythonw on Windows sets sys.stdout and sys.stdin to None.

This is problematic because while, e.g. print() will not fail, if not do anything useful, other things like warnings and logging, etc., may try to use stdout or stderr, and they will then fail.

See #107792 and #57791

So one might say: "don't do that" -- fair enough, except the user suffering from the problem may not be the one that wrote the code that's calling stderr -- it could be in a library, for instance. Library writers are often not thinking about windowed apps, and shouldn't have to do something special to accommodate them in any case.

See this note in the PyInstaller docs:

https://pyinstaller.org/en/stable/common-issues-and-pitfalls.html?highlight=os.devnull#sys-stdin-sys-stdout-and-sys-stderr-in-noconsole-windowed-applications-windows-only

In which the solution is:

import sys
import os

if sys.stdout is None:
    sys.stdout = open(os.devnull, "w")
if sys.stderr is None:
    sys.stderr = open(os.devnull, "w")

It's not unreasonable for library authors to expect that stdout and stderr to be valid.

So couldn't pythonw add that code by default? users could override it themselves, of course.

NOTE: tested recently on py3.12 -- but this has been an issue for quite some time.

NOTE2: this isn't an issue on the Mac, because its pythonw redirects to the system off-screen console.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo il comportamento con pythonw su Windows e consulta le discussioni collegate nell’issue e la documentazione di PyInstaller sui flussi standard mancanti. Determina come pythonw dovrebbe fornire oggetti sys.stdin e sys.stdout validi senza interrompere il comportamento esistente, quindi verifica che gli avvisi, il logging e i casi d’uso relativi a print funzionino come previsto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
operating-systems
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.