-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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 | ||
|
@@ -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.""" | ||
|
@@ -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 | ||
|
||
|
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.