jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1336832?usp=email )
Change subject: tools: Use pathlib for archive suffix detection
......................................................................
tools: Use pathlib for archive suffix detection
Extracting the extension with string slicing rejects path-like filenames
and treats a leading dot in a hidden filename as a compression suffix.
Uppercase archive extensions are also treated as uncompressed files.
Use Path.suffix with case normalization to select the compression format.
Accept string-based path-like objects and convert them with os.fspath
before passing the filename to the archive backends.
Change-Id: I6badd86020b79cf406ac817e704016a679d94a7b
---
M pywikibot/tools/__init__.py
1 file changed, 7 insertions(+), 5 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index 32af080..bf6e8ff 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -20,6 +20,7 @@
from collections.abc import Callable
from contextlib import suppress
from functools import total_ordering, wraps
+from pathlib import Path
from types import TracebackType
from typing import IO, Any, Literal
from warnings import catch_warnings, showwarning, warn
@@ -728,7 +729,7 @@
@deprecated_signature(since='11.4.0')
-def open_archive(filename: str, /,
+def open_archive(filename: str | os.PathLike[str], /,
mode: str = 'rb', *,
use_extension: bool = True) -> IO[bytes]:
"""Open a file and uncompress it if needed.
@@ -748,8 +749,10 @@
keyword only. Uses :class:`SevenZipFile` to open 7zip-files.
.. version-changed:: 11.7
Honor *mode* for uncompressed archives.
+ .. version-changed:: 11.8
+ Accept path-like filenames and detect suffixes case-insensitively.
- :param filename: The filename.
+ :param filename: The filename or path-like object.
:param mode: The mode in which the file should be opened. It may
either be 'r', 'rb', 'a', 'ab', 'w' or 'wb'. All modes open the
file in binary mode. It defaults to 'rb'.
@@ -785,10 +788,9 @@
elif mode not in ('rb', 'ab', 'wb'):
raise ValueError(f'Invalid mode: "{mode}"')
+ filename = os.fspath(filename)
if use_extension:
- # if '.' not in filename, it'll be 1 character long but otherwise
- # contain the period
- extension = filename[filename.rfind('.'):][1:]
+ extension = Path(filename).suffix.removeprefix('.').lower()
else:
if mode != 'rb':
raise ValueError('Magic number detection only when reading')
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1336832?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I6badd86020b79cf406ac817e704016a679d94a7b
Gerrit-Change-Number: 1336832
Gerrit-PatchSet: 1
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]