jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1326690?usp=email )
Change subject: memento: Close created sessions on request errors
......................................................................
memento: Close created sessions on request errors
MementoClient.request_head closed internally created sessions only
after successful requests. Ensure cleanup also runs when the HEAD
request raises while leaving caller-provided sessions untouched.
Change-Id: I5049ccb40e563f099779c998f70ccd88ed8ea1e2
---
M pywikibot/data/memento.py
M tests/memento_tests.py
2 files changed, 25 insertions(+), 2 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/data/memento.py b/pywikibot/data/memento.py
index f5b58f0..bcf3b85 100644
--- a/pywikibot/data/memento.py
+++ b/pywikibot/data/memento.py
@@ -308,8 +308,9 @@
except (InvalidSchema, MissingSchema):
raise ValueError(
f'Only HTTP URIs are supported, URI {uri} unrecognized.')
- if session_set:
- session.close()
+ finally:
+ if session_set:
+ session.close()
return response
diff --git a/tests/memento_tests.py b/tests/memento_tests.py
index e443696..83600dd 100755
--- a/tests/memento_tests.py
+++ b/tests/memento_tests.py
@@ -10,6 +10,7 @@
import unittest
from contextlib import suppress
from datetime import datetime
+from unittest.mock import patch
from urllib.parse import urlparse
from requests.exceptions import ConnectionError as RequestsConnectionError
@@ -37,6 +38,27 @@
return get_closest_memento_url(url, when, self.timegate_uri)
+@require_modules('memento_client')
+class TestMementoRequestHead(TestCase):
+
+ """Test Memento HEAD requests."""
+
+ net = False
+
+ @patch('pywikibot.data.memento.requests.Session')
+ def test_closes_created_session_on_error(self, session_class) -> None:
+ """Test that a created session is closed after a request error."""
+ from pywikibot.data.memento import MementoClient
+
+ session = session_class.return_value
+ session.head.side_effect = RequestsConnectionError
+
+ with self.assertRaises(RequestsConnectionError):
+ MementoClient.request_head('https://example.org')
+
+ session.close.assert_called_once_with()
+
+
class TestMementoArchive(MementoTestCase):
"""Web Archive Memento tests."""
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1326690?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: I5049ccb40e563f099779c998f70ccd88ed8ea1e2
Gerrit-Change-Number: 1326690
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]