8000 Support binary read when encoding=None (#29) · jaraco/cpython@f1ccb15 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1ccb15

Browse files
authored
Support binary read when encoding=None (python#29)
Closes python#28
1 parent 1f0aeb8 commit f1ccb15

File tree

6 files changed

+49
-5
lines changed

6 files changed

+49
-5
lines changed

importlib_resources/_py2.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def read(package, file_name, encoding='utf-8', errors='strict'):
7373
package = _get_package(package)
7474
# Note this is **not** builtins.open()!
7575
with open(package, file_name) as binary_file:
76-
return binary_file.read().decode(encoding=encoding, errors=errors)
76+
contents = binary_file.read()
77+
if encoding is None:
78+
return contents
79+
return contents.decode(encoding=encoding, errors=errors)
7780

7881

7982
@contextmanager

importlib_resources/_py3.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ def open(package: Package, file_name: FileName) -> BinaryIO:
7777
return BytesIO(data)
7878

7979

80-
def read(package: Package, file_name: FileName, encoding: str = 'utf-8',
81-
errors: str = 'strict') -> str:
80+
def read(package: Package,
81+
file_name: FileName,
82+
encoding: str = 'utf-8',
83+
errors: str = 'strict') -> Union[str, bytes]:
8284
"""Return the decoded string of the resource.
8385
8486
The decoding-related arguments have the same semantics as those of
@@ -88,6 +90,8 @@ def read(package: Package, file_name: FileName, encoding: str = 'utf-8',
8890
package = _get_package(package)
8991
# Note this is **not** builtins.open()!
9092
with open(package, file_name) as binary_file:
93+
if encoding is None:
94+
return binary_file.read()
9195
# Decoding from io.TextIOWrapper() instead of str.decode() in hopes
9296
# that the former will be smarter about memory usage.
9397
text_file = TextIOWrapper(
4 Bytes
Binary file not shown.
-182 Bytes
Binary file not shown.

importlib_resources/tests/test_read.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ class ReadTests:
1515

1616
def test_default_encoding(self):
1717
result = resources.read(self.data, 'utf-8.file')
18-
self.assertEqual("Hello, UTF-8 world!\n", result)
18+
self.assertEqual('Hello, UTF-8 world!\n', result)
1919

2020
def test_encoding(self):
2121
result = resources.read(self.data, 'utf-16.file', encoding='utf-16')
22-
self.assertEqual("Hello, UTF-16 world!\n", result)
22+
self.assertEqual('Hello, UTF-16 world!\n', result)
2323

2424
def test_errors(self):
2525
# Raises UnicodeError without the 'errors' argument.
2626
resources.read(
2727
self.data, 'utf-16.file', encoding='utf-8', errors='ignore')
2828

29+
def test_no_encoding(self):
30+
result = resources.read(self.data, 'binary.file', encoding=None)
31+
self.assertEqual(b'\0\1\2\3', result)
32+
2933

3034
class ReadDiskTests(ReadTests, unittest.TestCase):
3135

update-tests.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
3+
"""Remake the ziptestdata.zip file.
4+
5+
Run this to rebuild the importlib_resources/tests/data/ziptestdata.zip file,
6+
e.g. if you want to add a new file to the zip.
7+
8+
This will replace the file with the new build, but it won't commit anything to
9+
git.
10+
"""
11+
12+
import os
13+
from zipfile import ZipFile
14+
15+
RELPATH = 'importlib_resources/tests/data'
16+
CONTENTS = [
17+
# filenames - the source will always be prepended by
18+
# importlib_resources/tests/data/ziptestdata.zip and the destination will
19+
# always be prepended by ziptestdata/
20+
'__init__.py',
21+
'binary.file',
22+
'utf-16.file',
23+
'utf-8.file',
24+
]
25+
26+
27+
zip_file_path = os.path.join(RELPATH, 'ziptestdata.zip')
28+
29+
with ZipFile(zip_file_path, 'w') as zf:
30+
for filename in CONTENTS:
31+
src = os.path.join(RELPATH, filename)
32+
dst = os.path.join('ziptestdata', filename)
33+
zf.write(src, dst)

0 commit comments

Comments
 (0)
0