Skip to content

Commit 364c77b

Browse files
committed
improve add_file_from_memory
closes #68
1 parent fcbec5b commit 364c77b

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

libarchive/write.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def add_file_from_memory(
8686
:type entry_path: str
8787
:param entry_size: entire size of entry in bytes
8888
:type entry_size: int
89-
:param entry_data: content of entry as a list of byte-like objects
90-
:type entry_data: iterable
89+
:param entry_data: content of entry
90+
:type entry_data: bytes or Iterable[bytes]
9191
:param filetype: which type of file: normal, symlink etc.
9292
should entry be created as
9393
:type filetype: octal number
@@ -96,6 +96,13 @@ def add_file_from_memory(
9696
"""
9797
archive_pointer = self._pointer
9898

99+
if isinstance(entry_data, bytes):
100+
entry_data = (entry_data,)
101+
elif isinstance(entry_data, str):
102+
raise TypeError(
103+
"entry_data: expected bytes, got %r" % type(entry_data)
104+
)
105+
99106
with new_archive_entry() as archive_entry_pointer:
100107
archive_entry = ArchiveEntry(None, archive_entry_pointer)
101108

tests/test_rwx.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,8 @@ def test_write_not_fail(write_fail_mock):
126126
def test_adding_entry_from_memory(archfmt, data_bytes):
127127
entry_path = 'testfile.data'
128128

129-
# entry_data must be a list of byte-like objects for maximum compatibility
130-
# (Use of other data types can have undesirable side-effects)
131-
# entry_size is the total size in bytes of the entry_data items
132-
entry_data = [data_bytes]
133-
entry_size = sum(len(bdata) for bdata in entry_data)
129+
entry_data = data_bytes
130+
entry_size = len(data_bytes)
134131

135132
blocks = []
136133

@@ -144,7 +141,7 @@ def write_callback(data):
144141
buf = b''.join(blocks)
145142
with libarchive.memory_reader(buf) as memory_archive:
146143
for archive_entry in memory_archive:
147-
expected = b''.join(entry_data)
144+
expected = entry_data
148145
actual = b''.join(archive_entry.get_blocks())
149146
assert expected == actual
150147
assert archive_entry.path == entry_path

0 commit comments

Comments
 (0)