From 83bf99a47e7387fe50b204efbd5a2a5d9ee57827 Mon Sep 17 00:00:00 2001 From: Marin Date: Mon, 4 Apr 2022 20:48:40 +0200 Subject: [PATCH 1/2] add name to lzma fileobject --- Lib/lzma.py | 5 +++++ Lib/test/test_lzma.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Lib/lzma.py b/Lib/lzma.py index 800f52198fbb79..399cb888c58662 100644 --- a/Lib/lzma.py +++ b/Lib/lzma.py @@ -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)): + self.name = '' + else: + self.name = os.fspath(filename) + def close(self): """Flush and close the file. diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py index 145c8cfced4080..514fdc7eda01f5 100644 --- a/Lib/test/test_lzma.py +++ b/Lib/test/test_lzma.py @@ -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) From adf6d43d0b0aff5aa72896155fd5bad4000cebb6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 4 Apr 2022 18:57:27 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2022-04-04-18-57-26.bpo-47218.rf37SB.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-04-04-18-57-26.bpo-47218.rf37SB.rst diff --git a/Misc/NEWS.d/next/Library/2022-04-04-18-57-26.bpo-47218.rf37SB.rst b/Misc/NEWS.d/next/Library/2022-04-04-18-57-26.bpo-47218.rf37SB.rst new file mode 100644 index 00000000000000..eecb281851dd4e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-04-04-18-57-26.bpo-47218.rf37SB.rst @@ -0,0 +1 @@ +Added name attribute for LZMAFile (where appropriate and possible).