Xqt has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/370660 )
Change subject: [IMPR] Use context manager with contextlib.suppress ...................................................................... [IMPR] Use context manager with contextlib.suppress - use context manager with contextlib.suppress to ignore specified exceptions - suppress is introduces with py 3.4, backport it for previous python versions using the contextmanager decorator which was introduced with py 2.5 - replace ignored exceptions with this method - remove some "pass" NOPs Change-Id: I5bad43fe3bc56c0c268026029394e3610ebbe8b6 --- M pywikibot/__init__.py M pywikibot/data/api.py M pywikibot/date.py M pywikibot/i18n.py M pywikibot/page.py M pywikibot/site.py M pywikibot/site_detect.py M pywikibot/textlib.py M pywikibot/tools/__init__.py M scripts/redirect.py M scripts/replace.py M scripts/upload.py M tests/api_tests.py 13 files changed, 66 insertions(+), 99 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/60/370660/1 diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index e4000cc..c1512b0 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -73,6 +73,7 @@ normalize_username, MediaWikiVersion, redirect_func, + suppress, ModuleDeprecationWrapper as _ModuleDeprecationWrapper, PY2, UnicodeMixin, @@ -1326,11 +1327,9 @@ return # only need one drop() call because all throttles use the same global pid - try: + with suppress(IndexError): list(_sites.values())[0].throttle.drop() log(u"Dropped throttle(s).") - except IndexError: - pass atexit.register(_flush) @@ -1352,10 +1351,8 @@ if not _putthread.isAlive(): try: page_put_queue.mutex.acquire() - try: + with suppress(AssertionError, RuntimeError): _putthread.start() - except (AssertionError, RuntimeError): - pass finally: page_put_queue.mutex.release() page_put_queue.put((request, args, kwargs)) diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py index f905bc6..9130048 100644 --- a/pywikibot/data/api.py +++ b/pywikibot/data/api.py @@ -40,7 +40,7 @@ ) from pywikibot.tools import ( MediaWikiVersion, deprecated, itergroup, ip, PY2, getargspec, - UnicodeType + suppress, UnicodeType ) from pywikibot.tools.formatter import color_format @@ -2004,12 +2004,10 @@ if param.endswith("limit"): # param values are stored a list of str value = self._params[param][0] - try: + with suppress(BaseException): self._params[param] = [str(int(value) // 2)] pywikibot.output(u"Set %s = %s" % (param, self._params[param])) - except: - pass self.wait() continue if not result: @@ -2269,11 +2267,8 @@ @return: directory name @rtype: basestring """ - try: + with suppress(OSError): # directory might already exists os.makedirs(dir) - except OSError: - # directory already exists - pass return dir def _uniquedescriptionstr(self): diff --git a/pywikibot/date.py b/pywikibot/date.py index c1e2a49..7eb3504 100644 --- a/pywikibot/date.py +++ b/pywikibot/date.py @@ -7,7 +7,7 @@ # (C) Andre Engels, 2004-2005 # (C) Yuri Astrakhan, 2005-2006 (<Firstname><Lastname>@gmail.com) # (years/decades/centuries/millenniums str <=> int conversions) -# (C) Pywikibot team, 2004-2016 +# (C) Pywikibot team, 2004-2017 # # Distributed under the terms of the MIT license. # @@ -20,7 +20,7 @@ import re import sys -from pywikibot.tools import first_lower, first_upper, deprecated +from pywikibot.tools import first_lower, first_upper, deprecated, suppress if sys.version_info[0] > 2: unicode = str @@ -63,12 +63,10 @@ if isinstance(value, basestring): # Try all functions, and test result against predicates for func, pred in tuplst: - try: + with suppress(BaseException): res = func(value) if pred(res): return res - except: - pass else: # Find a predicate that gives true for this int value, and run a # function @@ -377,8 +375,8 @@ decoders = [] for s in _reParameters.split(pattern): if s is None: - pass - elif (len(s) in (2, 3) and s[0] == '%' and + continue + if (len(s) in (2, 3) and s[0] == '%' and s[-1] in _digitDecoders and (len(s) == 2 or s[1] in _decimalDigits)): # Must match a "%2d" or "%d" style @@ -2355,23 +2353,19 @@ @rtype: tuple """ for dictName, dict in formats.items(): - try: + with suppress(BaseException): year = dict[lang](title) return dictName, year - except: - pass # sometimes the title may begin with an upper case while its listed as # lower case, or the other way around # change case of the first character to the opposite, and try again if ignoreFirstLetterCase: - try: + with suppress(BaseException): if title[0].isupper(): title = first_lower(title) else: title = first_upper(title) return getAutoFormat(lang, title, ignoreFirstLetterCase=False) - except: - pass return None, None diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index 241a819..78f0b69 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -41,7 +41,8 @@ from pywikibot.exceptions import Error from pywikibot.plural import plural_rules from pywikibot.tools import ( - deprecated, deprecated_args, issue_deprecation_warning, StringTypes) + deprecated, deprecated_args, issue_deprecation_warning, StringTypes, + suppress) PLURAL_PATTERN = r'{{PLURAL:(?:%\()?([^\)]*?)(?:\)d)?\|(.*?)}}' @@ -517,11 +518,10 @@ # else we check for PLURAL variants trans = _extract_plural(code, trans, plural_parameters) if parameters: - try: + # On error: parameter is for PLURAL variants only, + # don't change the string + with suppress(KeyError, TypeError): return trans % parameters - except (KeyError, TypeError): - # parameter is for PLURAL variants only, don't change the string - pass return trans @@ -645,10 +645,8 @@ # This is called due to the old twntranslate function which ignored # KeyError. Instead only_plural should be used. if isinstance(parameters.source, dict): - try: + with suppress(KeyError): trans %= parameters.source - except KeyError: - pass parameters = None if parameters is not None and not isinstance(parameters, Mapping): diff --git a/pywikibot/page.py b/pywikibot/page.py index 3fbcf79..22889e5 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -74,7 +74,7 @@ add_full_name, manage_wrapping, ModuleDeprecationWrapper as _ModuleDeprecationWrapper, first_upper, redirect_func, remove_last_args, _NotImplementedWarning, - OrderedDict, Counter, + OrderedDict, Counter, suppress, ) from pywikibot.tools.ip import ip_regexp from pywikibot.tools.ip import is_IP @@ -1371,10 +1371,8 @@ """Clear the cached attributes of the page.""" self._revisions = {} for attr in self._cache_attrs: - try: + with suppress(AttributeError): delattr(self, attr) - except AttributeError: - pass def purge(self, **kwargs): """ @@ -5014,11 +5012,10 @@ precision = coord_args[2] else: precision = 0.0001 # Default value (~10 m at equator) - try: + + with suppress(TypeError): if self.target.precision is not None: precision = max(precision, self.target.precision) - except TypeError: - pass return (abs(self.target.lat - coord_args[0]) <= precision and abs(self.target.lon - coord_args[1]) <= precision) @@ -5934,6 +5931,6 @@ except UnicodeError as ex: if not firstException: firstException = ex - pass + # Couldn't convert, raise the original exception raise firstException diff --git a/pywikibot/site.py b/pywikibot/site.py index 2e282d1..ec718d1 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -73,7 +73,7 @@ redirect_func, issue_deprecation_warning, manage_wrapping, MediaWikiVersion, first_upper, normalize_username, merge_unique_dicts, - PY2, + PY2, suppress, filter_unique, ) from pywikibot.tools.ip import is_IP @@ -2067,14 +2067,14 @@ # check whether a login cookie already exists for this user # or check user identity when OAuth enabled self._loginstatus = LoginStatus.IN_PROGRESS - try: + + # APIError may occur if you are not logged in (no API read permissions) + with suppress(api.APIError): self.getuserinfo(force=True) if self.userinfo['name'] == self._username[sysop] and \ self.logged_in(sysop): return - # May occur if you are not logged in (no API read permissions). - except api.APIError: - pass + if self.is_oauth_token_available(): if sysop: raise NoUsername('No sysop is permitted with OAuth') @@ -4826,10 +4826,8 @@ if not self.logged_in(): self.login() if "deletedhistory" not in self.userinfo['rights']: - try: + with suppress(NoUsername): self.login(True) - except NoUsername: - pass if "deletedhistory" not in self.userinfo['rights']: raise Error( "deletedrevs: " @@ -4837,10 +4835,8 @@ % self.user()) if get_text: if "undelete" not in self.userinfo['rights']: - try: + with suppress(NoUsername): self.login(True) - except NoUsername: - pass if "undelete" not in self.userinfo['rights']: raise Error( "deletedrevs: " diff --git a/pywikibot/site_detect.py b/pywikibot/site_detect.py index 347bd63..b726e1d 100644 --- a/pywikibot/site_detect.py +++ b/pywikibot/site_detect.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Classes for detecting a MediaWiki site.""" # -# (C) Pywikibot team, 2010-2015 +# (C) Pywikibot team, 2010-2017 # # Distributed under the terms of the MIT license. # @@ -19,7 +19,7 @@ from pywikibot.comms.http import fetch from pywikibot.exceptions import ServerError -from pywikibot.tools import MediaWikiVersion, PY2, PYTHON_VERSION +from pywikibot.tools import MediaWikiVersion, PY2, PYTHON_VERSION, suppress if not PY2: from html.parser import HTMLParser @@ -134,11 +134,9 @@ pywikibot.log( 'wgEnableApi is not enabled in HTML of %s' % self.fromurl) - try: + with suppress(AttributeError): self.version = MediaWikiVersion( self.REwgVersion.search(data).group(1)) - except AttributeError: - pass self.server = self.REwgServer.search(data).groups()[0] self.scriptpath = self.REwgScriptPath.search(data).groups()[0] @@ -289,11 +287,9 @@ if tag == "meta": if attrs.get('name') == 'generator': self.generator = attrs["content"] - try: + with suppress(ValueError): self.version = MediaWikiVersion.from_generator( self.generator) - except ValueError: - pass elif tag == 'link' and 'rel' in attrs and 'href' in attrs: if attrs['rel'] in ('EditURI', 'stylesheet', 'search'): self.set_api_url(attrs['href']) diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index ebb72a9..3baf681 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -45,7 +45,8 @@ OrderedDict, StringTypes, UnicodeType, - issue_deprecation_warning + issue_deprecation_warning, + suppress ) # cache for replaceExcept to avoid recompile or regexes each call @@ -399,10 +400,8 @@ last = 0 for group_match in group_regex.finditer(new): group_id = group_match.group(1) or group_match.group(2) - try: + with suppress(ValueError): group_id = int(group_id) - except ValueError: - pass try: replacement += new[last:group_match.start()] replacement += match.group(group_id) or '' diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py index 7e1c9c1..665506d 100644 --- a/pywikibot/tools/__init__.py +++ b/pywikibot/tools/__init__.py @@ -9,6 +9,7 @@ __version__ = '$Id$' import collections +import contextlib import gzip import hashlib import inspect @@ -153,6 +154,18 @@ Counter = collections.Counter OrderedDict = collections.OrderedDict count = itertools.count + +if PYTHON_VERSION < (3, 4): + + @contextlib.contextmanager + def suppress(*exceptions): + """Context manager method to ignore exceptions.""" + try: + yield + except exceptions: + pass +else: + suppress = contextlib.suppress def has_module(module): @@ -677,13 +690,11 @@ """ s = slice(*args) marker = kwargs.pop('marker', '…') - try: + with suppress(KeyError): k, v = kwargs.popitem() raise TypeError( "islice_with_ellipsis() take only 'marker' as keyword arg, not %s" % k) - except KeyError: - pass _iterable = iter(iterable) for el in itertools.islice(_iterable, *args): @@ -1687,7 +1698,7 @@ if self._deprecated[attr][1]: return self._deprecated[attr][1] elif '.' in self._deprecated[attr][0]: - try: + with suppress(Exception): package_name = self._deprecated[attr][0].split('.', 1)[0] module = __import__(package_name) context = {package_name: module} @@ -1698,8 +1709,6 @@ self._deprecated[attr][2] ) return replacement - except Exception: - pass return getattr(self._module, attr) diff --git a/scripts/redirect.py b/scripts/redirect.py index bd8ddec..06faab4 100755 --- a/scripts/redirect.py +++ b/scripts/redirect.py @@ -90,7 +90,7 @@ from pywikibot.exceptions import ArgumentDeprecationWarning from pywikibot.textlib import extract_templates_and_params_regex_simple from pywikibot.tools.formatter import color_format -from pywikibot.tools import issue_deprecation_warning +from pywikibot.tools import issue_deprecation_warning, suppress if sys.version_info[0] > 2: basestring = (str, ) @@ -280,16 +280,13 @@ try: if pages[target]: final = target - try: + with suppress(KeyError): while result <= maxlen: result += 1 final = redirects[final] # result = None - except KeyError: - pass except KeyError: result = None - pass yield (redirect, result, target, final) def retrieve_broken_redirects(self): @@ -511,16 +508,12 @@ pywikibot.warning( u'Redirect target %s is not a valid page title.' % str(e)[10:]) - pass except pywikibot.InvalidTitle: pywikibot.exception() - pass except pywikibot.NoPage: movedTarget = None - try: + with suppress(pywikibot.NoMoveTarget): movedTarget = targetPage.moved_target() - except pywikibot.NoMoveTarget: - pass if movedTarget: if not movedTarget.exists(): # FIXME: Test to another move @@ -608,8 +601,7 @@ u'Skipping: Redirect target %s is not a redirect.' % newRedir.title(asLink=True)) break # do nothing - else: - pass # target found + # else target found except pywikibot.SectionError: pywikibot.warning( u"Redirect target section %s doesn't exist." @@ -788,12 +780,11 @@ # or number if ns == '': ns = '0' - try: + + # ValueError may occure with -namespace:all Process all namespaces. + # Only works with the API read interface. + with suppress(ValueError): ns = int(ns) - except ValueError: - # -namespace:all Process all namespaces. - # Only works with the API read interface. - pass if ns not in namespaces: namespaces.append(ns) elif option == 'offset': diff --git a/scripts/replace.py b/scripts/replace.py index 7e32b7d..0350cd9 100755 --- a/scripts/replace.py +++ b/scripts/replace.py @@ -162,6 +162,7 @@ deprecated, deprecated_args, issue_deprecation_warning, + suppress, ) from pywikibot.tools.formatter import color_format @@ -457,13 +458,11 @@ yield pywikibot.Page(self.site, entry.title) except KeyboardInterrupt: - try: + with suppress(NameError): if not self.skipping: pywikibot.output( u'To resume, use "-xmlstart:%s" on the command line.' % entry.title) - except NameError: - pass def isTitleExcepted(self, title): """ diff --git a/scripts/upload.py b/scripts/upload.py index 9c27b78..db13900 100755 --- a/scripts/upload.py +++ b/scripts/upload.py @@ -129,8 +129,6 @@ suffix = 1 << 10 elif suffix == "mi": suffix = 1 << 20 - else: - pass # huh? else: suffix = 1 chunk_size = math.trunc(base * suffix) diff --git a/tests/api_tests.py b/tests/api_tests.py index 9036102..56da7c2 100644 --- a/tests/api_tests.py +++ b/tests/api_tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """API test module.""" # -# (C) Pywikibot team, 2007-2016 +# (C) Pywikibot team, 2007-2017 # # Distributed under the terms of the MIT license. # @@ -19,7 +19,7 @@ from pywikibot.throttle import Throttle from pywikibot.tools import ( MediaWikiVersion, - PY2, + PY2, suppress, UnicodeType, ) @@ -1082,7 +1082,5 @@ if __name__ == '__main__': # pragma: no cover - try: + with suppress(SystemExit): unittest.main() - except SystemExit: - pass -- To view, visit https://gerrit.wikimedia.org/r/370660 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5bad43fe3bc56c0c268026029394e3610ebbe8b6 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Xqt <i...@gno.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits