Skip to content

Commit e2a669a

Browse files
matthewdeanmartinacdha
authored andcommitted
attempt one
1 parent 08315f1 commit e2a669a

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

src/bagit/__init__.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,25 @@ def find_locale_dir():
131131
#: Block size used when reading files for hashing:
132132
HASH_BLOCK_SIZE = 512 * 1024
133133

134+
135+
def open_text_file(filename, mode="r", encoding="utf-8", errors="strict"):
136+
# Open the underlying file in binary mode so the codec wrapper keeps codecs.open() behavior without its deprecation.
137+
binary_mode = mode.replace("t", "")
138+
if "b" not in binary_mode:
139+
binary_mode += "b"
140+
141+
stream = open(filename, binary_mode)
142+
if "r" in mode and all(flag not in mode for flag in ("w", "a", "+")):
143+
wrapped = codecs.getreader(encoding)(stream, errors=errors)
144+
else:
145+
wrapped = codecs.getwriter(encoding)(stream, errors=errors)
146+
147+
wrapped.encoding = codecs.lookup(encoding).name.upper()
148+
return wrapped
149+
150+
134151
#: Convenience function used everywhere we want to open a file to read text
135152
#: rather than undecoded bytes:
136-
open_text_file = partial(codecs.open, encoding="utf-8", errors="strict")
137153

138154
# This is the same as decoding the byte values in codecs.BOM:
139155
UNICODE_BYTE_ORDER_MARK = "\ufeff"
@@ -1423,8 +1439,8 @@ def _encode_filename(s):
14231439

14241440

14251441
def _decode_filename(s):
1426-
s = re.sub(r"%0D", "\r", s, re.IGNORECASE)
1427-
s = re.sub(r"%0A", "\n", s, re.IGNORECASE)
1442+
s = re.sub(r"%0D", "\r", s, flags=re.IGNORECASE)
1443+
s = re.sub(r"%0A", "\n", s, flags=re.IGNORECASE)
14281444
return s
14291445

14301446

0 commit comments

Comments
 (0)