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

Change subject: editor: Close temporary descriptor before launch
......................................................................

editor: Close temporary descriptor before launch

TextEditor reopened the temporary path while retaining the descriptor
returned by mkstemp until the external editor exited. This held an
unused descriptor throughout the editing session.

Close the descriptor immediately after creating the Path object, then
write the initial text through Path.write_text().

Change-Id: I292fe7ddfa38f8f0c00f39f99b77e4c1311ce7ee
---
M pywikibot/editor.py
M tests/editor_tests.py
2 files changed, 22 insertions(+), 1 deletion(-)

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




diff --git a/pywikibot/editor.py b/pywikibot/editor.py
index 6c188d9..b8bdec0 100644
--- a/pywikibot/editor.py
+++ b/pywikibot/editor.py
@@ -129,6 +129,7 @@
             handle, filename = tempfile.mkstemp(
                 suffix=f'.{config.editor_filename_extension}', text=True)
             path = Path(filename)
+            os.close(handle)

             try:
                 encoding = config.editor_encoding
@@ -145,7 +146,6 @@
                 return path.read_text(encoding=encoding)

             finally:
-                os.close(handle)
                 os.unlink(path)

         if GUI_ERROR:
diff --git a/tests/editor_tests.py b/tests/editor_tests.py
index 1d7350f..dafa755 100755
--- a/tests/editor_tests.py
+++ b/tests/editor_tests.py
@@ -7,8 +7,10 @@
 """Tests for :mod:`editor` module."""
 from __future__ import annotations

+import os
 import unittest
 from contextlib import suppress
+from unittest.mock import patch

 from pywikibot import config, editor
 from tests.aspects import TestCase
@@ -51,6 +53,25 @@
         te = editor.TextEditor()
         self.assertEqual(te.editor, 'custom_editor')

+    def test_descriptor_closed_before_editor(self):
+        """Test that the temporary descriptor is closed before editing."""
+        config.editor = 'custom_editor'
+        real_mkstemp = editor.tempfile.mkstemp
+        handle = -1
+
+        def mkstemp(*args, **kwargs):
+            nonlocal handle
+            handle, filename = real_mkstemp(*args, **kwargs)
+            return handle, filename
+
+        def run(*args, **kwargs):
+            with self.assertRaises(OSError):
+                os.fstat(handle)
+
+        with patch.object(editor.tempfile, 'mkstemp', side_effect=mkstemp):
+            with patch.object(editor.subprocess, 'run', side_effect=run):
+                self.assertIsNone(editor.TextEditor().edit('text'))
+

 if __name__ == '__main__':
     with suppress(SystemExit):

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