python / cpython

The Python programming language
https://www.python.org
Other
63.49k stars 30.41k forks source link

Why does codecs.StreamWriter accept text data stream? #108428

Open infoagee opened 1 year ago

infoagee commented 1 year ago

Bug report

Checklist

CPython versions tested on:

3.8

Operating systems tested on:

Windows

Output from running 'python -VV' on the command line:

No response

A clear and concise description of the bug:

import sys, codecs
a = codecs.getwriter('cp850')
b = a(sys.stdout.buffer)
c = a(sys.stdout)
b.write('A')
c.write('A')  # error!

Now that b is enough to write text in cp850, why can one construct a werid and buggy c? Then, why does codecs.StreamWriter accept text data stream such as sys.stdout?

zougloub commented 7 months ago

Not a bug: cp850 is a codec that encodes from str to bytes, and sys.stdout is a str stream.