[Python-Dev] Draft PEP: Deprecate codecs.StreamReader and codecs.StreamWriter

Victor Stinner victor.stinner at haypocalc.com
Thu Jul 7 12:22:51 CEST 2011


Le 07/07/2011 03:16, Benjamin Peterson a écrit :
> 2011/7/6 Victor Stinner<victor.stinner at haypocalc.com>:
>> codecs.open() will be changed to reuse the builtin open() function
>> (TextIOWrapper).
> This doesn't strike me as particularly backwards compatible, since
> you've just enumerated the differences between StreamWriter/Reader and
> TextIOWrapper.
Which kind of differences are you thinking about? I only listed two 
attributes specific to StreamReaderWriter (.reader and .writer). You 
mean that these attributes are used? There are maybe other subtle 
differences between Stream* and TextIOWrapper, but I don't think that 
anyone relies on them. Should I try to list all differences in the PEP?

If I understood correctly the previous discussion, an important point is 
to be able to write code Python 2 which "just works" on Python 3 without 
using 2to3. If you don't rely on the subtle implementation details of 
Stream*, it's possible (to use codecs.open in Python 3, even if 
codecs.open is implemented to reuse TextIOWrapper via open). If you rely 
on the differences, I bet that it is easy to not use these differences 
(and continue to be compatible with Python 2). For example, you can 
replace f.reader.read() by f.read(), it's just the same.

The two classical usages of codecs.open() (of text files) are:

- read the whole file content
- read the file line by line

For this two usecases, the API is exactly the same. Using 
f=codecs.open() or f=open() (in Python 3, or f=io.open() in Python 2), 
you can use:

- for line in f: ...
- while True: line = f.readline(); if not line: break; ...
- f.read()

I'm not saying that my PEP doesn't break the compatibility, it *does* 
break the backward compatibility. That's why we need a PEP. That's why 
there is a section called "Backwards Compatibility" in the PEP. I'm 
trying to say that I bet that nobody will notice.

The most impacting change will be (at the end) the removal of the 
StreamReader/StreamWriter API. If a program uses directly these classes, 
it will have to be updated to use TextIOWrapper (or codecs.open() or 
open() maybe).

I wrote in a previous email:

"I did search for usage of these classes on the Internet, and except 
projects
implementing their own codecs (and so implement their
StreamReader/StreamWriter classes, even if they don't use it), I only found
one project using directly StreamReader: pygment (*). I searched 
quickly, so
don't trust these results :-) StreamReader & friends are used indirectly
through codecs.open(). My patch changes codecs.open() to make it reuse open
(io.TextIOWrapper), so the deprecation of StreamReader would not be 
noticed by
most users.

(*) I also found Sphinx, but I was wrong: it doesn't use StreamReader, 
it just
has a full copy of the UTF-8-SIG codec which has a StreamReader class. I 
don't
think that the class is used."

Victor


More information about the Python-Dev mailing list