jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1358454?usp=email )
Change subject: [bugfix] Use the bot's ignore_*_errors attributes in userPut
......................................................................
[bugfix] Use the bot's ignore_*_errors attributes in userPut
CurrentPageBot sets ignore_save_related_errors to True, but only
put_current() used it. userPut() and _save_page() always defaulted to
False, so a CurrentPageBot calling userPut() stopped with
OtherPageSaveError, for example on a page with {{nobots}}.
Add ignore_save_related_errors and ignore_server_errors attributes to
BaseBot, both False, and use them as defaults in _save_page(). Bots
derived from CurrentPageBot, and WikidataBot.user_add_claim(), now
report and skip save related errors unless the keyword is given.
Other bots behave as before.
Bug: T156908
Change-Id: Ic961d03b4dcff4c43c6ca322336859293d8cda61
---
M pywikibot/bot.py
M tests/bot_tests.py
2 files changed, 83 insertions(+), 11 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 37a1562..1afed5a 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -1182,6 +1182,22 @@
.. version-added:: 7.2
"""
+ ignore_save_related_errors: bool = False
+ """Attribute to determine whether errors related to page save are
+ reported and ignored by :meth:`userPut` instead of being raised. The
+ *ignore_save_related_errors* keyword of :meth:`userPut` overrides it.
+
+ .. version-added:: 11.10
+ """
+
+ ignore_server_errors: bool = False
+ """Attribute to determine whether server errors are reported and
+ ignored by :meth:`userPut` instead of being raised. The
+ *ignore_server_errors* keyword of :meth:`userPut` overrides it.
+
+ .. version-added:: 11.10
+ """
+
available_options = {
'always': False, # By default ask for confirmation when putting a page
}
@@ -1324,9 +1340,15 @@
:keyword asynchronous: Passed to page.save
:keyword summary: Passed to page.save
:keyword show_diff: Show changes between oldtext and newtext (enabled)
- :keyword ignore_save_related_errors: Report and ignore (disabled)
- :keyword ignore_server_errors: Report and ignore (disabled)
+ :keyword ignore_save_related_errors: Report and ignore (default:
+ :attr:`ignore_save_related_errors`)
+ :keyword ignore_server_errors: Report and ignore (default:
+ :attr:`ignore_server_errors`)
:return: Whether the page was saved successfully
+
+ .. version-changed:: 11.10
+ *ignore_save_related_errors* and *ignore_server_errors*
+ default to the class attributes of the same name.
"""
if oldtext.rstrip() == newtext.rstrip():
pywikibot.info(f'No changes were needed on {page}')
@@ -1356,10 +1378,11 @@
:param args: Passed to the function
:param kwargs: Passed to the function
:keyword ignore_server_errors: If True, server errors will be reported
- and ignored (default: False)
+ and ignored (default: :attr:`ignore_server_errors`)
:kwtype ignore_server_errors: bool
:keyword ignore_save_related_errors: If True, errors related to
- page save will be reported and ignored (default: False)
+ page save will be reported and ignored (default:
+ :attr:`ignore_save_related_errors`)
:kwtype ignore_save_related_errors: bool
:return: Whether the page was saved successfully
@@ -1371,9 +1394,10 @@
if 'asynchronous' not in kwargs and self.opt.always:
kwargs['asynchronous'] = True
- ignore_save_related_errors = kwargs.pop('ignore_save_related_errors',
- False)
- ignore_server_errors = kwargs.pop('ignore_server_errors', False)
+ ignore_save_related_errors = kwargs.pop(
+ 'ignore_save_related_errors', self.ignore_save_related_errors)
+ ignore_server_errors = kwargs.pop('ignore_server_errors',
+ self.ignore_server_errors)
try:
func(*args, **kwargs)
@@ -1870,7 +1894,6 @@
"""
ignore_save_related_errors = True
- ignore_server_errors = False
def treat_page(self) -> None:
"""Process one page (Abstract method)."""
@@ -2131,9 +2154,10 @@
:param source: Site where the claim comes from
:param bot: Whether to flag as bot (if possible)
:keyword ignore_server_errors: If True, server errors will be reported
- and ignored (default: False)
+ and ignored (default: :attr:`ignore_server_errors`)
:keyword ignore_save_related_errors: If True, errors related to
- page save will be reported and ignored (default: False)
+ page save will be reported and ignored (default:
+ :attr:`ignore_save_related_errors`)
:return: Whether the item was saved successfully
.. note:: calling this method sets the current_page property
diff --git a/tests/bot_tests.py b/tests/bot_tests.py
index 572c595..66062f4 100755
--- a/tests/bot_tests.py
+++ b/tests/bot_tests.py
@@ -12,11 +12,12 @@
from contextlib import suppress
from tempfile import TemporaryDirectory
from textwrap import dedent
-from unittest.mock import patch
+from unittest.mock import MagicMock, patch
import pywikibot
import pywikibot.bot
from pywikibot import config, i18n
+from pywikibot.exceptions import OtherPageSaveError, ServerError
from pywikibot.specialbots import UploadRobot
from tests.aspects import DefaultSiteTestCase, SiteAttributeTestCase, TestCase
from tests.utils import execute
@@ -489,6 +490,53 @@
{'sub_opt': 1})
+class TestIgnoreSaveErrors(TestCase):
+
+ """Test the ignore_*_errors attributes of bots in userPut()."""
+
+ net = False
+
+ @staticmethod
+ def put(bot, error, **kwargs) -> bool:
+ """Call userPut with a page whose save raises an error."""
+ page = MagicMock(spec=pywikibot.Page)
+ if error is OtherPageSaveError:
+ error = OtherPageSaveError(
+ page, 'Editing restricted by {{bots}} template')
+ page.save.side_effect = error
+ return bot.userPut(page, 'old', 'new', show_diff=False, **kwargs)
+
+ def test_base_bot(self) -> None:
+ """Test that BaseBot raises errors by default."""
+ bot = pywikibot.bot.BaseBot(always=True)
+ with self.assertRaises(OtherPageSaveError):
+ self.put(bot, OtherPageSaveError)
+ with self.assertRaises(ServerError):
+ self.put(bot, ServerError('server error'))
+ self.assertFalse(
+ self.put(bot, OtherPageSaveError,
+ ignore_save_related_errors=True))
+
+ def test_current_page_bot(self) -> None:
+ """Test that CurrentPageBot ignores save related errors."""
+ bot = pywikibot.bot.CurrentPageBot(always=True)
+ self.assertFalse(self.put(bot, OtherPageSaveError))
+ with self.assertRaises(OtherPageSaveError):
+ self.put(bot, OtherPageSaveError,
+ ignore_save_related_errors=False)
+ with self.assertRaises(ServerError):
+ self.put(bot, ServerError('server error'))
+
+ def test_ignore_server_errors(self) -> None:
+ """Test a bot which ignores server errors."""
+ bot = pywikibot.bot.CurrentPageBot(always=True)
+ bot.ignore_server_errors = True
+ self.assertFalse(self.put(bot, ServerError('server error')))
+ with self.assertRaises(ServerError):
+ self.put(bot, ServerError('server error'),
+ ignore_server_errors=False)
+
+
if __name__ == '__main__':
with suppress(SystemExit):
unittest.main()
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1358454?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: Ic961d03b4dcff4c43c6ca322336859293d8cda61
Gerrit-Change-Number: 1358454
Gerrit-PatchSet: 3
Gerrit-Owner: AkashKumar7902 <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Mahveotm <[email protected]>
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]