Skip to content

Commit

Permalink
Repalce codecs.open with open
Browse files Browse the repository at this point in the history
Since Python 3, using codecs.open() as the builtin supports an encoding
argument. Use the simpler and more conventional pattern.
  • Loading branch information
jdufresne committed Jun 25, 2022
1 parent 718b8d1 commit d545d76
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"""

import argparse
import codecs
import configparser
import fnmatch
import os
Expand Down Expand Up @@ -175,7 +174,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:
Expand All @@ -184,7 +183,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)
except UnicodeDecodeError:
print("ERROR: Could not detect encoding: %s" % filename,
file=sys.stderr)
Expand All @@ -203,7 +202,7 @@ def open_with_internal(self, filename):
curr = 0
while True:
try:
f = codecs.open(filename, 'r', encoding=encodings[curr])
f = open(filename, encoding=encodings[curr])
except UnicodeDecodeError:
if not self.quiet_level & QuietLevels.ENCODING:
print("WARNING: Decoding file using encoding=%s failed: %s"
Expand Down Expand Up @@ -439,19 +438,19 @@ def parse_ignore_words_option(ignore_words_option):


def build_exclude_hashes(filename, exclude_lines):
with codecs.open(filename, 'r') as f:
with open(filename) 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
Expand Down Expand Up @@ -737,7 +736,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) as f:
f.writelines(lines)
return bad_count

Expand Down

0 comments on commit d545d76

Please sign in to comment.