Skip to content

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

Merged
merged 1 commit into from
Jun 16, 2017
Merged

bpo-29783: Replace codecs.open() with io.open() #599

merged 1 commit into from
Jun 16, 2017

Conversation

vstinner
Copy link
Member

In test_sax, remove buffering=0 since io.open() doesn't support
unbuffered text I/O.

@mention-bot
Copy link

@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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not builtin open?

Copy link
Member Author

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?

Copy link
Member

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):
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

with open(TESTFN, 'wb') as fp:
fp.write(b'\xa1')

with open(TESTFN, encoding='cp949') as fp:
Copy link
Member

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.

Copy link
Member

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.

@@ -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',
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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.

@vstinner
Copy link
Member Author

I simpiified the PR to only keep the less controversal changes.

@serhiy-storchaka: would you mind to review this shorted and rebased PR please?

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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.

with open(TESTFN, 'wb') as fp:
fp.write(b'\xa1')

with open(TESTFN, encoding='cp949') as fp:
Copy link
Member

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.

@@ -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',
Copy link
Member

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.

@vstinner
Copy link
Member Author

I reverted changes in Lib/test/test_multibytecodec.py and Lib/test/test_sax.py.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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.

@vstinner vstinner merged commit 272d888 into python:master Jun 16, 2017
@vstinner vstinner deleted the codecs_open2 branch June 16, 2017 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants