From 139d7532c25ea73a2bc65b47ea520b6a02a63f3f Mon Sep 17 00:00:00 2001 From: Val Shkolnikov Date: Tue, 7 Dec 2021 12:46:54 -0800 Subject: [PATCH 1/4] Fix for Issue43098 tarfile list() method does not show file type. --- Lib/tarfile.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index c1ee1222e09b5a..bf871a6ea6776e 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1915,13 +1915,18 @@ def list(self, verbose=True, *, members=None): output is produced. `members' is optional and must be a subset of the list returned by getmembers(). """ + # Convert tarinfo type to stat type. + type2mode = {REGTYPE: stat.S_IFREG, SYMTYPE: stat.S_IFLNK, + FIFOTYPE: stat.S_IFIFO, CHRTYPE: stat.S_IFCHR, + DIRTYPE: stat.S_IFDIR, BLKTYPE: stat.S_IFBLK} self._check() if members is None: members = self for tarinfo in members: if verbose: - _safe_print(stat.filemode(tarinfo.mode)) + modetype = type2mode.get(tarinfo.type, 0) + _safe_print(stat.filemode(modetype | tarinfo.mode)) _safe_print("%s/%s" % (tarinfo.uname or tarinfo.uid, tarinfo.gname or tarinfo.gid)) if tarinfo.ischr() or tarinfo.isblk(): From 37c922c5ea3ca4b74b5112c64bb6da6b4eb2982f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 27 Nov 2022 03:36:27 +0000 Subject: [PATCH 2/4] =?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 --- .../2022-11-27-03-36-26.gh-issue-87264.kFnZgX.rst | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2022-11-27-03-36-26.gh-issue-87264.kFnZgX.rst diff --git a/Misc/NEWS.d/next/Documentation/2022-11-27-03-36-26.gh-issue-87264.kFnZgX.rst b/Misc/NEWS.d/next/Documentation/2022-11-27-03-36-26.gh-issue-87264.kFnZgX.rst new file mode 100644 index 00000000000000..08c915dff27219 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2022-11-27-03-36-26.gh-issue-87264.kFnZgX.rst @@ -0,0 +1,6 @@ +tarfile list() method shows file type correctly, eg. +$ python3 -m tarfile -v -l Python-3.11.0.tgz +drwxr-xr-x pablogsal/pablogsal 0 2022-10-24 10:48:40 Python-3.11.0/ +drwxr-xr-x pablogsal/pablogsal 0 2022-10-24 10:35:39 Python-3.11.0/Mac/ +-rw-r--r-- pablogsal/pablogsal 17973 2022-10-24 10:35:39 Python-3.11.0/Mac/README.rst +... From fc37df117346c14834c5a8ccd1afebaab1fddce2 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 28 Nov 2022 17:21:26 -0500 Subject: [PATCH 3/4] Condense blurb by omitting example and adding text --- .../2022-11-27-03-36-26.gh-issue-87264.kFnZgX.rst | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Misc/NEWS.d/next/Documentation/2022-11-27-03-36-26.gh-issue-87264.kFnZgX.rst b/Misc/NEWS.d/next/Documentation/2022-11-27-03-36-26.gh-issue-87264.kFnZgX.rst index 08c915dff27219..d9e8ba55bc22b2 100644 --- a/Misc/NEWS.d/next/Documentation/2022-11-27-03-36-26.gh-issue-87264.kFnZgX.rst +++ b/Misc/NEWS.d/next/Documentation/2022-11-27-03-36-26.gh-issue-87264.kFnZgX.rst @@ -1,6 +1 @@ -tarfile list() method shows file type correctly, eg. -$ python3 -m tarfile -v -l Python-3.11.0.tgz -drwxr-xr-x pablogsal/pablogsal 0 2022-10-24 10:48:40 Python-3.11.0/ -drwxr-xr-x pablogsal/pablogsal 0 2022-10-24 10:35:39 Python-3.11.0/Mac/ --rw-r--r-- pablogsal/pablogsal 17973 2022-10-24 10:35:39 Python-3.11.0/Mac/README.rst -... +tarfile list() method shows file type correctly instead of as question marks. From 5b819fb8032a7774518dde414f211ded7167587e Mon Sep 17 00:00:00 2001 From: Val Shkolnikov Date: Mon, 28 Nov 2022 15:52:17 -0800 Subject: [PATCH 4/4] Update test_tarfile.py --- Lib/test/test_tarfile.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 213932069201b9..c27bd88617b16f 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -302,11 +302,23 @@ def test_list_verbose(self): # accessories if verbose flag is being used # ... # ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/conttype - # ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype + # -rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype + # drwxr-xr-x tarfile/tarfile 0 2003-01-05 15:19:43 ustar/dirtype/ # ... - self.assertRegex(out, (br'\?rw-r--r-- tarfile/tarfile\s+7011 ' - br'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d ' - br'ustar/\w+type ?\r?\n') * 2) + # + # Array of values to modify the regex below: + # ((file_type, file_permissions, file_length), ...) + type_perm_lengths = ( + (r'\?', 'rw-r--r--', '7011'), ('-', 'rw-r--r--', '7011'), + ('d', 'rwxr-xr-x', '0'), ('d', 'rwxr-xr-x', '255'), + (r'\?', 'rw-r--r--', '0'), ('l', 'rwxrwxrwx', '0'), + ('b', 'rw-rw----', '3,0'), ('c', 'rw-rw-rw-', '1,3'), + ('p', 'rw-r--r--', '0')) + self.assertRegex(out.decode(), ''.join( + [(tp + (r'%s tarfile/tarfile\s+%s ' % (perm, ln) + + r'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d ' + r'ustar/\w+type[/>\sa-z-]*\n')) for tp, perm, ln + in type_perm_lengths])) # Make sure it prints the source of link with verbose flag self.assertIn(b'ustar/symtype -> regtype', out) self.assertIn(b'./ustar/linktest2/symtype -> ../linktest1/regtype', out)