jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[cleanup] remove outdated code of open_archive

- Remove import check for b2z. This module is a Python module since 2.3.
The check was introduced for the case that someone builds his own
Python (T123092). But this is obviousliy not a Pywikibot issue to
solve such cases.
- Remove import check for lzma. This module is a Python module since 3.3.
The check was necessary for Python 2 backward compatibility but no
longer necessary.
- remove test without lzma/bz2
- decline T123092

This also reverts commit d02a9bd6dad1f3b2965fcd9e257e4f80c1f1d4da.

Bug: T123092
Change-Id: Ib99d7834f0cd57591c30c913d481b9d3885cd9a6
---
M pywikibot/tools/__init__.py
M tests/tools_tests.py
2 files changed, 3 insertions(+), 78 deletions(-)

diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index 536d34f..72ce89b 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -4,9 +4,11 @@
#
# Distributed under the terms of the MIT license.
#
+import bz2
import gzip
import hashlib
import ipaddress
+import lzma
import os
import re
import stat
@@ -41,21 +43,6 @@

pkg_Version = pkg_resources.packaging.version.Version # noqa: N816

-try:
- import bz2
-except ImportError as bz2_import_error:
- try:
- import bz2file as bz2
- warn('package bz2 was not found; using bz2file', ImportWarning)
- except ImportError:
- warn('package bz2 and bz2file were not found', ImportWarning)
- bz2 = bz2_import_error
-
-try:
- import lzma
-except ImportError as lzma_import_error:
- lzma = lzma_import_error
-

__all__ = (
# deprecating functions
@@ -537,9 +524,6 @@
immediately raise that error but only on reading it.
:raises lzma.LZMAError: When error occurs during compression or
decompression or when initializing the state with lzma or xz.
- :raises ImportError: When file is compressed with bz2 but neither bz2 nor
- bz2file is importable, or when file is compressed with lzma or xz but
- lzma is not importable.
:return: A file-like object returning the uncompressed data in binary mode.
:rtype: file-like object
"""
@@ -575,8 +559,6 @@
extension = ''

if extension == 'bz2':
- if isinstance(bz2, ImportError):
- raise bz2
binary = bz2.BZ2File(filename, mode)

elif extension == 'gz':
@@ -604,8 +586,6 @@
binary = process.stdout

elif extension in ('lzma', 'xz'):
- if isinstance(lzma, ImportError):
- raise lzma
lzma_fmts = {'lzma': lzma.FORMAT_ALONE, 'xz': lzma.FORMAT_XZ}
binary = lzma.open(filename, mode, format=lzma_fmts[extension])

diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index 11ac059..3a4f362 100755
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -12,7 +12,6 @@
from collections import Counter, OrderedDict
from collections.abc import Mapping
from contextlib import suppress
-from importlib import import_module
from unittest import mock

from pywikibot import tools
@@ -31,7 +30,7 @@
)

from tests import join_xml_data_path
-from tests.aspects import TestCase, require_modules
+from tests.aspects import TestCase
from tests.utils import skipping


@@ -75,34 +74,6 @@
self._get_content(self.base_file + '.bz2', use_extension=False),
self.original_content)

- @require_modules('bz2file')
- def test_open_archive_with_bz2file(self):
- """Test open_archive when bz2file library."""
- old_bz2 = tools.bz2
- try:
- tools.bz2 = import_module('bz2file')
- self.assertEqual(self._get_content(self.base_file + '.bz2'),
- self.original_content)
- self.assertEqual(self._get_content(self.base_file + '.bz2',
- use_extension=False),
- self.original_content)
- finally:
- tools.bz2 = old_bz2
-
- def test_open_archive_without_bz2(self):
- """Test open_archive when bz2 and bz2file are not available."""
- old_bz2 = tools.bz2
- bz2_import_error = ('This is a fake exception message that is '
- 'used when bz2 and bz2file are not importable')
- try:
- tools.bz2 = ImportError(bz2_import_error)
- with self.assertRaisesRegex(
- ImportError,
- bz2_import_error):
- self._get_content(self.base_file + '.bz2')
- finally:
- tools.bz2 = old_bz2
-
def test_open_archive_gz(self):
"""Test open_archive with gz compressor in the standard library."""
self.assertEqual(
@@ -123,8 +94,6 @@

def test_open_archive_lzma(self):
"""Test open_archive with lzma compressor in the standard library."""
- if isinstance(tools.lzma, ImportError):
- self.skipTest('lzma not importable')
self.assertEqual(
self._get_content(self.base_file + '.lzma'), self.original_content)
# Legacy LZMA container formet has no magic, skipping
@@ -135,24 +104,6 @@
self._get_content(self.base_file + '.xz', use_extension=False),
self.original_content)

- def test_open_archive_without_lzma(self):
- """Test open_archive when lzma is not available."""
- old_lzma = tools.lzma
- lzma_import_error = ('This is a fake exception message that is '
- 'used when lzma is not importable')
- try:
- tools.lzma = ImportError(lzma_import_error)
- with self.assertRaisesRegex(
- ImportError,
- lzma_import_error):
- self._get_content(self.base_file + '.lzma')
- with self.assertRaisesRegex(
- ImportError,
- lzma_import_error):
- self._get_content(self.base_file + '.xz')
- finally:
- tools.lzma = old_lzma
-

class OpenArchiveWriteTestCase(TestCase):

@@ -227,18 +178,12 @@

def test_write_archive_lzma(self):
"""Test writing a lzma archive."""
- if isinstance(tools.lzma, ImportError):
- self.skipTest('lzma not importable')
-
content = self._write_content('.lzma')
with open(self.base_file + '.lzma', 'rb') as f:
self.assertEqual(content, f.read())

def test_write_archive_xz(self):
"""Test writing a xz archive."""
- if isinstance(tools.lzma, ImportError):
- self.skipTest('lzma not importable')
-
content = self._write_content('.xz')
self.assertEqual(content[:6], b'\xFD7zXZ\x00')


To view, visit change 837680. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ib99d7834f0cd57591c30c913d481b9d3885cd9a6
Gerrit-Change-Number: 837680
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: AbdealiJK <abdealikothari@gmail.com>
Gerrit-Reviewer: JJMC89 <JJMC89.Wikimedia@gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw@arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged