-
Notifications
You must be signed in to change notification settings - Fork 485
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
Replace codecs.open with open #2378
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 | ||||
---|---|---|---|---|---|---|
|
@@ -17,7 +17,6 @@ | |||||
""" | ||||||
|
||||||
import argparse | ||||||
import codecs | ||||||
import configparser | ||||||
import fnmatch | ||||||
import os | ||||||
|
@@ -172,7 +171,7 @@ def open(self, filename): | |||||
|
||||||
def open_with_chardet(self, filename): | ||||||
self.encdetector.reset() | ||||||
with codecs.open(filename, 'rb') as f: | ||||||
with open(filename, 'rb') as f: | ||||||
for line in f: | ||||||
self.encdetector.feed(line) | ||||||
if self.encdetector.done: | ||||||
|
@@ -181,7 +180,7 @@ def open_with_chardet(self, filename): | |||||
encoding = self.encdetector.result['encoding'] | ||||||
|
||||||
try: | ||||||
f = codecs.open(filename, 'r', encoding=encoding) | ||||||
f = open(filename, encoding=encoding, newline='') | ||||||
except UnicodeDecodeError: | ||||||
print("ERROR: Could not detect encoding: %s" % filename, | ||||||
file=sys.stderr) | ||||||
|
@@ -205,7 +204,7 @@ def open_with_internal(self, filename): | |||||
elif not self.quiet_level & QuietLevels.ENCODING: | ||||||
print("WARNING: Trying next encoding %s" | ||||||
% encoding, file=sys.stderr) | ||||||
with codecs.open(filename, 'r', encoding=encoding) as f: | ||||||
with open(filename, encoding=encoding, newline='') as f: | ||||||
try: | ||||||
lines = f.readlines() | ||||||
except UnicodeDecodeError: | ||||||
|
@@ -463,19 +462,19 @@ def parse_ignore_words_option(ignore_words_option): | |||||
|
||||||
|
||||||
def build_exclude_hashes(filename, exclude_lines): | ||||||
with codecs.open(filename, mode='r', encoding='utf-8') as f: | ||||||
with open(filename, encoding='utf-8') as f: | ||||||
for line in f: | ||||||
exclude_lines.add(line) | ||||||
|
||||||
|
||||||
def build_ignore_words(filename, ignore_words): | ||||||
with codecs.open(filename, mode='r', encoding='utf-8') as f: | ||||||
with open(filename, encoding='utf-8') as f: | ||||||
for line in f: | ||||||
ignore_words.add(line.strip()) | ||||||
|
||||||
|
||||||
def build_dict(filename, misspellings, ignore_words): | ||||||
with codecs.open(filename, mode='r', encoding='utf-8') as f: | ||||||
with open(filename, encoding='utf-8') as f: | ||||||
for line in f: | ||||||
[key, data] = line.split('->') | ||||||
# TODO for now, convert both to lower. Someday we can maybe add | ||||||
|
@@ -767,7 +766,7 @@ def parse_file(filename, colors, summary, misspellings, exclude_lines, | |||||
print("%sFIXED:%s %s" | ||||||
% (colors.FWORD, colors.DISABLE, filename), | ||||||
file=sys.stderr) | ||||||
with codecs.open(filename, 'w', encoding=encoding) as f: | ||||||
with open(filename, 'w', encoding=encoding, newline='') as f: | ||||||
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. I had to look this up in codecs.open:
I wonder whether strictly equivalent functionality wouldn't require changing the mode from
Therefore, But then codespell/codespell_lib/_codespell.py Line 188 in a700083
codespell/codespell_lib/_codespell.py Line 212 in a700083
With codecs , all files were read in binary mode, then written back in binary mode, which preserves newlines. Without codecs , how do we preserve newlines? Maybe I am missing something here, but I believe more work is needed to preserve newlines.
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. I think I get it:
Perhaps this should be explained in the commit? |
||||||
f.writelines(lines) | ||||||
return bad_count | ||||||
|
||||||
|
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 one should have been
open()
in the first place, since we do not use the encoding argument.