Skip to content

Commit 6a17bf9

Browse files
authored
fix: make push_path open in binary mode so it works on non-text files (#1458)
There was a bug in `push_path` where it called `open` with the default (text) mode on the local files, causing it to not work on binary files (more specifically, if they weren't valid UTF-8). This is the fix for that -- `push_path` should treat the files as raw bytes, so read in binary mode. Also change the test to ensure binary / non-UTF-8 files work as expected. Fixes #1455.
1 parent 3fb8548 commit 6a17bf9

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

ops/model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2662,7 +2662,7 @@ def local_list(source_path: Path) -> List[pebble.FileInfo]:
26622662
if info.type is pebble.FileType.DIRECTORY:
26632663
self.make_dir(dstpath, make_parents=True)
26642664
continue
2665-
with open(info.path) as src:
2665+
with open(info.path, 'rb') as src:
26662666
self.push(
26672667
dstpath,
26682668
src,

test/test_model.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1369,8 +1369,8 @@ def test_recursive_push_and_pull(case: PushPullCase):
13691369
for file in case.files:
13701370
fpath = os.path.join(push_src.name, file[1:])
13711371
os.makedirs(os.path.dirname(fpath), exist_ok=True)
1372-
with open(fpath, 'w') as f:
1373-
f.write('hello')
1372+
with open(fpath, 'wb') as f:
1373+
f.write(b'push \xc3\x28') # invalid UTF-8 to ensure binary works
13741374
if case.dirs:
13751375
for directory in case.dirs:
13761376
fpath = os.path.join(push_src.name, directory[1:])
@@ -1402,6 +1402,8 @@ def test_recursive_push_and_pull(case: PushPullCase):
14021402
), f'push_path gave wrong expected errors: want {case.errors}, got {errors}'
14031403
for fpath in case.want:
14041404
assert c.exists(fpath), f'push_path failed: file {fpath} missing at destination'
1405+
content = c.pull(fpath, encoding=None).read()
1406+
assert content == b'push \xc3\x28'
14051407
for fdir in case.want_dirs:
14061408
assert c.isdir(fdir), f'push_path failed: dir {fdir} missing at destination'
14071409

0 commit comments

Comments
 (0)