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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Doc/tools/extensions/pyspecific.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""

import re
import codecs
import io
from os import path
from time import asctime
from pprint import pformat
Expand Down Expand Up @@ -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.

content = fp.read()
finally:
fp.close()
except Exception:
text = 'The NEWS file is not available.'
node = nodes.strong(text, text)
Expand Down
7 changes: 4 additions & 3 deletions Lib/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.

"""#"
"""

import builtins, sys
import builtins
import sys

### Registry and builtin stateless codec functions

Expand Down Expand Up @@ -739,7 +740,7 @@ def __getattr__(self, name,
"""
return getattr(self.stream, name)

# these are needed to make "with codecs.open(...)" work properly
# these are needed to make "with StreamReaderWriter(...)" work properly

def __enter__(self):
return self
Expand Down
5 changes: 1 addition & 4 deletions Lib/lib2to3/pgen2/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,8 @@ def parse_stream(self, stream, debug=False):

def parse_file(self, filename, encoding=None, debug=False):
"""Parse a file and return the syntax tree."""
stream = codecs.open(filename, "r", encoding)
try:
with io.open(filename, "r", encoding=encoding) as stream:
return self.parse_stream(stream, debug)
finally:
stream.close()

def parse_string(self, text, debug=False):
"""Parse a string and return the syntax tree."""
Expand Down
36 changes: 10 additions & 26 deletions Lib/lib2to3/refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@


# Python imports
import io
import os
import sys
import logging
import operator
import collections
import io
from itertools import chain

# Local imports
Expand Down Expand Up @@ -107,22 +107,6 @@ def get_fixers_from_package(pkg_name):
def _identity(obj):
return obj

if sys.version_info < (3, 0):
import codecs
_open_with_encoding = codecs.open
# codecs.open doesn't translate newlines sadly.
def _from_system_newlines(input):
return input.replace("\r\n", "\n")
def _to_system_newlines(input):
if os.linesep != "\n":
return input.replace("\n", os.linesep)
else:
return input
else:
_open_with_encoding = open
_from_system_newlines = _identity
_to_system_newlines = _identity


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.

def _detect_future_features(source):
have_docstring = False
Expand Down Expand Up @@ -330,8 +314,8 @@ def _read_python_source(self, filename):
encoding = tokenize.detect_encoding(f.readline)[0]
finally:
f.close()
with _open_with_encoding(filename, "r", encoding=encoding) as f:
return _from_system_newlines(f.read()), encoding
with io.open(filename, "r", encoding=encoding) as f:
return f.read(), encoding

def refactor_file(self, filename, write=False, doctests_only=False):
"""Refactors a file."""
Expand Down Expand Up @@ -530,16 +514,16 @@ def write_file(self, new_text, filename, old_text, encoding=None):
set.
"""
try:
f = _open_with_encoding(filename, "w", encoding=encoding)
fp = io.open(filename, "w", encoding=encoding)
except OSError as err:
self.log_error("Can't create %s: %s", filename, err)
return
try:
f.write(_to_system_newlines(new_text))
except OSError as err:
self.log_error("Can't write %s: %s", filename, err)
finally:
f.close()

with fp:
try:
fp.write(new_text)
except OSError as err:
self.log_error("Can't write %s: %s", filename, err)
self.log_debug("Wrote changes to %s", filename)
self.wrote = True

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4598,7 +4598,7 @@ class TestEncoding(TestCase):
def _test_module_encoding(self, path):
path, _ = os.path.splitext(path)
path += ".py"
with codecs.open(path, 'r', 'utf-8') as f:
with open(path, 'r', encoding='utf-8') as f:
f.read()

def test_argparse_module_encoding(self):
Expand Down