jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1323853?usp=email )

Change subject: welcome: Close signature file on read errors
......................................................................

welcome: Close signature file on read errors

Manage the locally configured signature file with its context manager
after opening it. This preserves the existing encoding and error handling
while ensuring the file closes if reading raises.

Change-Id: I399a1ff5a128235876ffd49132f562c98d92dacd
---
M scripts/welcome.py
A tests/welcome_tests.py
2 files changed, 46 insertions(+), 2 deletions(-)

Approvals:
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/scripts/welcome.py b/scripts/welcome.py
index ecf0c85..22d87bb 100755
--- a/scripts/welcome.py
+++ b/scripts/welcome.py
@@ -794,8 +794,8 @@
                 pywikibot.error('No fileName!')
                 raise FilenameNotSet('No signature filename specified.')

-            sign_text = f.read()
-            f.close()
+            with f:
+                sign_text = f.read()
         else:
             # Read from wiki page
             sign_page_name = i18n.translate(self.site, RANDOM_SIGN)
diff --git a/tests/welcome_tests.py b/tests/welcome_tests.py
new file mode 100755
index 0000000..03f8ba7
--- /dev/null
+++ b/tests/welcome_tests.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+#
+# (C) Pywikibot team, 2026
+#
+# Distributed under the terms of the MIT license.
+#
+"""Tests for the welcome script."""
+from __future__ import annotations
+
+import unittest
+from types import SimpleNamespace
+from unittest.mock import MagicMock, patch
+
+from scripts import welcome
+from tests.aspects import TestCase
+
+
+class TestWelcomeBot(TestCase):
+
+    """Test :class:`welcome.WelcomeBot`."""
+
+    net = False
+
+    def test_signature_file_closed_on_read_error(self) -> None:
+        """Test that the signature file is closed when reading fails."""
+        file_obj = MagicMock()
+        file_obj.__enter__.return_value = file_obj
+        file_obj.read.side_effect = OSError
+
+        with (
+            patch.object(
+                welcome.globalvar, 'sign_file_name', 'signatures.txt'),
+            patch.object(welcome.pywikibot.config, 'datafilepath',
+                         return_value='signatures.txt'),
+            patch('builtins.open', return_value=file_obj),
+            self.assertRaises(OSError),
+        ):
+            welcome.WelcomeBot.define_sign(SimpleNamespace())
+
+        file_obj.__exit__.assert_called_once()
+
+
+if __name__ == '__main__':
+    unittest.main()

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1323853?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: I399a1ff5a128235876ffd49132f562c98d92dacd
Gerrit-Change-Number: 1323853
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]

Reply via email to