Skip to content

Commit 5bc662c

Browse files
src/ tests/: fix use of open() in src/__init__.py.
Change all calls of `open()` to `io.open()` so we don't pick up `pymudf.open()`. New test_4746() checks use of `io.open()` in `Archive.add()`. Also fixed Archive.add(<name>, <path>) to open file in binary mode.
1 parent d7ba123 commit 5bc662c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ def _make_output(
120120
assert 0, f'Expected prefix `fd:`, `path:`. `path+:` or `logging:` in {text=}.'
121121

122122
if fd is not None:
123-
ret = open(fd, mode='w', closefd=False)
123+
ret = io.open(fd, mode='w', closefd=False)
124124
elif stream is not None:
125125
assert hasattr(stream, 'write')
126126
assert hasattr(stream, 'flush')
127127
ret = stream
128128
elif path is not None:
129-
ret = open(path, 'w')
129+
ret = io.open(path, 'w')
130130
elif path_append is not None:
131-
ret = open(path_append, 'a')
131+
ret = io.open(path_append, 'a')
132132
elif (0
133133
or pylogging is not None
134134
or pylogging_logger is not None
@@ -2010,7 +2010,7 @@ def make_subarch(entries, mount, fmt):
20102010
elif os.path.isfile(content):
20112011
assert isinstance(path, str) and path != '', \
20122012
f'Need name for binary content, but {path=}.'
2013-
with open(content) as f:
2013+
with io.open(content, 'rb') as f:
20142014
ff = f.read()
20152015
self._add_treeitem(ff, path)
20162016
return make_subarch([path], None, 'tree')
@@ -2055,7 +2055,7 @@ def make_subarch(entries, mount, fmt):
20552055
self._add_treeitem(data, name, path=path)
20562056
elif isinstance(data, str):
20572057
if os.path.isfile(data):
2058-
with open(data, 'rb') as f:
2058+
with io.open(data, 'rb') as f:
20592059
ff = f.read()
20602060
self._add_treeitem(ff, name, path=path)
20612061
else:
@@ -7559,7 +7559,7 @@ def build_subset(buffer, unc_set, gid_set):
75597559
]
75607560

75617561
# store glyph ids or unicodes as file
7562-
with open(f"{tmp_dir}/uncfile.txt", "w", encoding='utf8') as unc_file:
7562+
with io.open(f"{tmp_dir}/uncfile.txt", "w", encoding='utf8') as unc_file:
75637563
if 0xFFFD in unc_set: # error unicode exists -> use glyphs
75647564
args.append(f"--gids-file={uncfile_path}")
75657565
gid_set.add(189)
@@ -7574,7 +7574,7 @@ def build_subset(buffer, unc_set, gid_set):
75747574
unc_file.write("%04x\n" % unc)
75757575

75767576
# store fontbuffer as a file
7577-
with open(oldfont_path, "wb") as fontfile:
7577+
with io.open(oldfont_path, "wb") as fontfile:
75787578
fontfile.write(buffer)
75797579
try:
75807580
os.remove(newfont_path) # remove old file

tests/test_general.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,3 +2180,8 @@ def process_document(document):
21802180
print(f'Processing {path_b=}', flush=1)
21812181
document_b = mupdf.fz_open_document(path_b)
21822182
process_document(document_b)
2183+
2184+
2185+
def test_4746():
2186+
archive = pymupdf.Archive('.')
2187+
archive.add('foo', __file__)

0 commit comments

Comments
 (0)