-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-29783: Replace codecs.open() with io.open() #599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@Haypo, thanks for your PR! By analyzing the history of the files in this pull request, we identified @loewis, @benjaminp, @asvetlov, @birkenfeld and @doerwalter to be potential reviewers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some changes LGTM, but others are not. Changes to codecs
and test_codecs
look unrelated to other changes and to the purpose of this PR.
@@ -254,11 +254,8 @@ def run(self): | |||
fpath = path.join(source_dir, fname) | |||
self.state.document.settings.record_dependencies.add(fpath) | |||
try: | |||
fp = codecs.open(fpath, encoding='utf-8') | |||
try: | |||
with io.open(fpath, encoding='utf-8') as fp: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not builtin open
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe some people or services still use Python 2 to build the Python documentation, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I missed that this is not a part of the stdlib.
@@ -107,22 +107,6 @@ def get_fixers_from_package(pkg_name): | |||
def _identity(obj): | |||
return obj | |||
|
|||
if sys.version_info < (3, 0): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks as a red flag. Should this file be compatible with Python 2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
io.open() is available since Python 2.6, so my change should work on Python 2.6 and newer.
I don't know why this file has support for Python 2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, we can drop the support of Python 2.5.
Lib/test/test_multibytecodec.py
Outdated
with open(TESTFN, 'wb') as fp: | ||
fp.write(b'\xa1') | ||
|
||
with open(TESTFN, encoding='cp949') as fp: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I afraid this invalidates the purpose of the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bug1728403 originally was about codecs.open()
. I don't know if there is a sense to use io.open()
here.
Lib/test/test_sax.py
Outdated
@@ -774,8 +774,8 @@ class StreamReaderWriterXmlgenTest(XmlgenTest, unittest.TestCase): | |||
fname = support.TESTFN + '-codecs' | |||
|
|||
def ioclass(self): | |||
writer = codecs.open(self.fname, 'w', encoding='ascii', | |||
errors='xmlcharrefreplace', buffering=0) | |||
writer = open(self.fname, 'w', encoding='ascii', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This invalidates the purpose of the test. This is the test for StreamReaderWriter
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why test_sax tests the StreamReaderWriter class.
It seems like io.TextIOWrapper is not tested, whereas it's more common than codecs.StreamReaderWriter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code of XMLGenerator
is complex, it contains a lot of special cases and should be tested for different kinds of writers.
It would be better to add a test for io.TextIOWrapper
(if testing io.StringIO
is not enough), but keep the test for StreamReaderWriter
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep the existing code. This test is purposed for testing StreamReaderWriter
.
I simpiified the PR to only keep the less controversal changes. @serhiy-storchaka: would you mind to review this shorted and rebased PR please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some tests should use codecs
streams.
Lib/test/test_multibytecodec.py
Outdated
with open(TESTFN, 'wb') as fp: | ||
fp.write(b'\xa1') | ||
|
||
with open(TESTFN, encoding='cp949') as fp: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bug1728403 originally was about codecs.open()
. I don't know if there is a sense to use io.open()
here.
Lib/test/test_sax.py
Outdated
@@ -774,8 +774,8 @@ class StreamReaderWriterXmlgenTest(XmlgenTest, unittest.TestCase): | |||
fname = support.TESTFN + '-codecs' | |||
|
|||
def ioclass(self): | |||
writer = codecs.open(self.fname, 'w', encoding='ascii', | |||
errors='xmlcharrefreplace', buffering=0) | |||
writer = open(self.fname, 'w', encoding='ascii', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep the existing code. This test is purposed for testing StreamReaderWriter
.
I reverted changes in Lib/test/test_multibytecodec.py and Lib/test/test_sax.py. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The remaining cosmetic changes LGTM.
In test_sax, remove buffering=0 since io.open() doesn't support
unbuffered text I/O.