Make print() use a single write() call

Ouverte
#161,934 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
22/100
Type d'issue
Fonctionnalité
Clarté
Clairement spécifiée
Activité
À l'abandon
Stack technique
python
Domaine
backend

Piste de recherche

The builtin print() lives in Python/bltinmodule.c (builtin_print_impl), which currently issues separate writes for each argument, the separator and end. Read that function first, then the existing tests in Lib/test/test_builtin.py covering print to see the expected partial-output and flush behaviour. Done means one buffered write per call, partial output preserved when str() raises, plus tests for the atomicity and error paths.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

interpreter-core topic-IO type-feature
Feature or enhancement

Currently, print("a", "b") performs four separate writes:

file.write("a")
file.write(" ")
file.write("b")
file.write("\n")

The proposed behavior is equivalent to:

file.write("a b\n")

The main motivation is to reduce interleaving between concurrent print() calls. For example, two threads printing separate messages can currently produce msg1msg2\n\n. Atomicity would still depend on the underlying stream.

Reducing the number of calls can also improve performance, particularly for streams with expensive write() methods, such as IDLE’s output stream.

The implementation should preserve partial output when converting an argument with str() raises: write the parts collected so far, without appending end, and propagate the exception. If flush=True, flush that partial output as well.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/make-print-use-a-single-write-call/109886

Linked PRs
  • gh-161936
Langage dominant
Python
Étoiles
78.2k
Forks
36.6k
Merge moyen
19 h 57 min
PR mergées (30 j)
630

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de python/cpython

Toutes les issues de python/cpython

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.