Page MenuHomePhabricator

Using redirected excepion classes leads to unexpected exception
Closed, ResolvedPublic

Description

Using the redirected pywikibot.SpamfilterError leads to unexpected exception:

(Python 3.7 and Python 3.8, not sure about 3.5-3.6)

>>> import pwb, pywikibot as py
>>> from contextlib import suppress
>>> s = py.Site()
>>> p = py.Page(s, 'user:xqt/Test')  # this is a protected page
>>> p.text += '\ntest edit'

Works with suppressing py.LockedPage exception only:

>>> with suppress(py.LockedPage):  # works for LockedPage exception
	p.save()


WARNING: API error protectedpage: This page has been protected to prevent editing or other actions.

Works with suppressing py.SpamblacklistError and py.LockedPage exception

>>> with suppress(py.SpamblacklistError, py.LockedPage):  # works too
	p.save()

	
WARNING: API error protectedpage: This page has been protected to prevent editing or other actions.

But fails if py.SpamfilterError is to be catched by try-except:

>>> with suppress(py.SpamfilterError, py.LockedPage):  # FAILS
	p.save()

	
WARNING: <pyshell#90>:1: DeprecationWarning: SpamfilterError is deprecated; use SpamblacklistError instead.

WARNING: API error protectedpage: This page has been protected to prevent editing or other actions.
Traceback (most recent call last):
  File "C:\pwb\GIT\core\pywikibot\site\__init__.py", line 5404, in editpage
    result = req.submit()
  File "C:\pwb\GIT\core\pywikibot\data\api.py", line 2113, in submit
    raise APIError(**result['error'])
pywikibot.data.api.APIError: protectedpage: This page has been protected to prevent editing or other actions.
[help: See https://de.wikipedia.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; for notice of API deprecations and breaking changes.]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#90>", line 2, in <module>
    p.save()
  File "C:\pwb\GIT\core\pywikibot\tools\__init__.py", line 1797, in wrapper
    return obj(*__args, **__kw)
  File "C:\pwb\GIT\core\pywikibot\tools\__init__.py", line 1797, in wrapper
    return obj(*__args, **__kw)
  File "C:\pwb\GIT\core\pywikibot\page\__init__.py", line 1301, in save
    self._save(summary=summary, watch=watch, minor=minor, botflag=botflag,
  File "C:\pwb\GIT\core\pywikibot\page\__init__.py", line 136, in wrapper
    handle(func, self, *args, **kwargs)
  File "C:\pwb\GIT\core\pywikibot\page\__init__.py", line 127, in handle
    raise err
  File "C:\pwb\GIT\core\pywikibot\page\__init__.py", line 115, in handle
    func(self, *args, **kwargs)
  File "C:\pwb\GIT\core\pywikibot\page\__init__.py", line 1313, in _save
    done = self.site.editpage(self, summary=summary, minor=minor,
  File "C:\pwb\GIT\core\pywikibot\site\__init__.py", line 1339, in callee
    return fn(self, *args, **kwargs)
  File "C:\pwb\GIT\core\pywikibot\site\__init__.py", line 5431, in editpage
    raise exception(page)
pywikibot.exceptions.LockedPage: Page [[de:Benutzer:Xqt/Test]] is locked.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#90>", line 2, in <module>
    p.save()
  File "C:\Python38\lib\contextlib.py", line 369, in __exit__
    return exctype is not None and issubclass(exctype, self._exceptions)
TypeError: issubclass() arg 2 must be a class or tuple of classes
>>>

The same problem exist for the other deprecated exceptions like UserActionRefuse or PageNotFound.

But it works with Python 2.7:

>>> import pwb, pywikibot as py
WARNING: pywikibot\__init__.py:128: FutureWarning: 

Python 2.7.18 will be dropped soon.
It is recommended to use Python 3.5 or above.
See T213287 for further information.

  FutureWarning)  # probably adjust the line no in utils.execute()

>>> s = py.Site()
>>> p = py.Page(s, 'user:xqt/Test')  # this is a protected page
>>> p.text += '\ntest edit'
>>> try:
	p.save()
except (py.SpamfilterError, py.LockedPage):
	pass

WARNING: API error protectedpage: This page has been protected to prevent editing or other actions.
WARNING: __main__:4: DeprecationWarning: SpamfilterError is deprecated; use SpamblacklistError instead.

>>>

Event Timeline

Xqt triaged this task as High priority.May 26 2020, 8:10 PM

See also:

>>> issubclass(py.SpamblacklistError, Exception)
True
>>> issubclass(py.LockedPage, Exception)
True
>>> issubclass(py.SpamfilterError, Exception)
WARNING: <pyshell#104>:1: DeprecationWarning: SpamfilterError is deprecated; use SpamblacklistError instead.

Traceback (most recent call last):
  File "<pyshell#104>", line 1, in <module>
    issubclass(py.SpamfilterError, Exception)
TypeError: issubclass() arg 1 must be a class
>>>

for both: Python 3 an Python 2

Change 598876 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [bugix] Remove deprecated exceptions

https://gerrit.wikimedia.org/r/598876

Change 598977 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [bugix] Remove deprecated exceptions

https://gerrit.wikimedia.org/r/598977

Change 598876 merged by jenkins-bot:
[pywikibot/core@master] [bugix] replace SpamfilterError exceptions

https://gerrit.wikimedia.org/r/598876

Xqt renamed this task from Using the redirected pywikibot.SpamfilterError leads to unexpected exception to Using redirected excepion classes leads to unexpected exception.Jun 19 2020, 6:35 AM

Change 598977 merged by jenkins-bot:
[pywikibot/core@master] [bugfix] Remove deprecated exceptions

https://gerrit.wikimedia.org/r/598977