Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ def __init__(self, filename=None, mode="r", *,
trailing_error=LZMAError, format=format, filters=filters)
self._buffer = io.BufferedReader(raw)

if not isinstance(filename, (str, bytes)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it is a path-like object?

Also, if it is a file, we perhaps can use its name. Actually, we perhaps can simply use self._fp.name.

self.name = ''
else:
self.name = os.fspath(filename)

def close(self):
"""Flush and close the file.

Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,34 @@ def test_init_with_preset_and_filters(self):
LZMAFile(BytesIO(), "w", format=lzma.FORMAT_RAW,
preset=6, filters=FILTERS_RAW_1)

def test_filename(self):
with TempFile(TESTFN):
with LZMAFile(TESTFN) as f:
self.assertTrue(hasattr(f, 'name'))
self.assertTrue(f.name == TESTFN)

def test_path_filename(self):
filename = pathlib.Path(TESTFN)
with TempFile(filename):
with LZMAFile(filename) as f:
self.assertTrue(hasattr(f, 'name'))
self.assertTrue(f.name == '')

def test_name_bytesIO(self):
f = LZMAFile(BytesIO(COMPRESSED_XZ))
try:
self.assertTrue(hasattr(f, 'name'))
self.assertTrue(f.name == '')
finally:
f.close()

f = LZMAFile(BytesIO(), "w")
try:
self.assertTrue(hasattr(f, 'name'))
self.assertTrue(f.name == '')
finally:
f.close()

def test_close(self):
with BytesIO(COMPRESSED_XZ) as src:
f = LZMAFile(src)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added name attribute for LZMAFile (where appropriate and possible).