jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/400400 )
Change subject: utils.py: Use more descriptive variable names in detailed_show_warning
......................................................................
utils.py: Use more descriptive variable names in detailed_show_warning
Also in DeprecationTestCase use source_adjustment_skips instead of skip_list
which could be easily confused with a mechanism to prevent warnings from
being shown.
Change-Id: I1d6ebacfe4f4062017c5a217a0a0a91d6d2b1f4e
---
M tests/aspects.py
M tests/utils.py
2 files changed, 33 insertions(+), 23 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/tests/aspects.py b/tests/aspects.py
index d132ad2..077d2c1 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -1492,7 +1492,7 @@
_generic_match = re.compile(r'.* is deprecated(; use .* instead)?\.')
- skip_list = [
+ source_adjustment_skips = [
unittest.case._AssertRaisesContext,
TestCase.assertRaises,
TestCase.assertRaisesRegex,
@@ -1506,7 +1506,7 @@
# Python 3 component in the call stack of _AssertRaisesContext
if hasattr(unittest.case, '_AssertRaisesBaseContext'):
- skip_list.append(unittest.case._AssertRaisesBaseContext)
+ source_adjustment_skips.append(unittest.case._AssertRaisesBaseContext)
def __init__(self, *args, **kwargs):
"""Constructor."""
@@ -1520,7 +1520,8 @@
self._do_test_warning_filename = True
self._ignore_unknown_warning_packages = False
- self.context_manager = WarningSourceSkipContextManager(self.skip_list)
+ self.context_manager = WarningSourceSkipContextManager(
+ self.source_adjustment_skips)
def _reset_messages(self):
"""Reset captured deprecation warnings."""
@@ -1667,7 +1668,7 @@
assertion, *args, **kwargs)
self.assertOneDeprecation()
- skip_list = DeprecationTestCase.skip_list + [
+ source_adjustment_skips = DeprecationTestCase.source_adjustment_skips + [
CapturingTestCase.process_assert,
CapturingTestCase.patch_assert,
]
diff --git a/tests/utils.py b/tests/utils.py
index f73f48e..240cda8 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -182,7 +182,8 @@
"""
Constructor.
- @param skip_list: List of objects to be skipped
+ @param skip_list: List of objects to be skipped. The source of any
+ warning that matches the skip_list won't be adjusted.
@type skip_list: list of object or (obj, str, int, int)
"""
super(WarningSourceSkipContextManager, self).__init__(record=True)
@@ -220,40 +221,48 @@
"""Enter the context manager."""
def detailed_show_warning(*args, **kwargs):
"""Replacement handler for
warnings.showwarning."""
- entry = warnings.WarningMessage(*args, **kwargs)
+ warn_msg = warnings.WarningMessage(*args, **kwargs)
- skip_lines = 0
- entry_line_found = False
+ skip_frames = 0
+ a_frame_has_matched_warn_msg = False
- for (_, filename, fileno, _, line, _) in inspect.stack():
- if any(start <= fileno <= end
+ # The following for-loop will adjust the warn_msg only if the
+ # warning does not match the skip_list.
+ for (_, frame_filename, frame_lineno, _, _, _) in inspect.stack():
+ if any(start <= frame_lineno <= end
for (_, skip_filename, start, end) in self.skip_list
- if skip_filename == filename):
- if entry_line_found:
+ if skip_filename == frame_filename):
+ # this frame matches to one of the items in the skip_list
+ if a_frame_has_matched_warn_msg:
continue
else:
- skip_lines += 1
+ skip_frames += 1
- if (filename, fileno) == (entry.filename, entry.lineno):
- if not skip_lines:
+ if (
+ frame_filename == warn_msg.filename
+ and frame_lineno == warn_msg.lineno
+ ):
+ if not skip_frames:
break
- entry_line_found = True
+ a_frame_has_matched_warn_msg = True
- if entry_line_found:
- if not skip_lines:
- (entry.filename, entry.lineno) = (filename, fileno)
+ if a_frame_has_matched_warn_msg:
+ if not skip_frames:
+ # adjust the warn_msg
+ warn_msg.filename = frame_filename
+ warn_msg.lineno = frame_lineno
break
else:
- skip_lines -= 1
+ skip_frames -= 1
# Avoid failures because cryptography is mentioning Python 2.6
# is outdated
if PYTHON_VERSION < (2, 7):
- if (isinstance(entry, DeprecationWarning) and
- str(entry.message) == PYTHON_26_CRYPTO_WARN):
+ if (isinstance(warn_msg, DeprecationWarning) and
+ str(warn_msg.message) == PYTHON_26_CRYPTO_WARN):
return
- log.append(entry)
+ log.append(warn_msg)
log = super(WarningSourceSkipContextManager, self).__enter__()
self._module.showwarning = detailed_show_warning
--
To view, visit
https://gerrit.wikimedia.org/r/400400
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I1d6ebacfe4f4062017c5a217a0a0a91d6d2b1f4e
Gerrit-PatchSet: 5
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Framawiki <framawiki(a)tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>