From 8d5f338378fe70aebe23bbd626bd98d0b1934d63 Mon Sep 17 00:00:00 2001 From: fly1d <309400591+fly1d@users.noreply.github.com> Date: Fri, 30 Oct 2026 03:12:40 +0800 Subject: [PATCH] Handle tuple arguments in spawn --- ptyprocess/ptyprocess.py | 2 +- tests/test_spawn.py | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/ptyprocess/ptyprocess.py b/ptyprocess/ptyprocess.py index daf20b1..e395591 100644 --- a/ptyprocess/ptyprocess.py +++ b/ptyprocess/ptyprocess.py @@ -214,7 +214,7 @@ def spawn( raise TypeError("Expected a list or tuple for argv, got %r" % argv) # Shallow copy of argv so we can modify it - argv = argv[:] + argv = list(argv) command = argv[0] command_with_path = which(command) diff --git a/tests/test_spawn.py b/tests/test_spawn.py index 8aefff1..8b60edd 100755 --- a/tests/test_spawn.py +++ b/tests/test_spawn.py @@ -2,8 +2,10 @@ import os import time import select +import sys import tempfile import unittest +from unittest import mock from ptyprocess.ptyprocess import which from ptyprocess import PtyProcess, PtyProcessUnicode @@ -61,6 +63,52 @@ def test_spawn_unicode_decoding(self): p = PtyProcessUnicode.spawn(['printf', '\\344\\272!'], codec_errors='backslashreplace') assert p.read() == '\\xe4\\xba!' + def _spawn_argv(self, process_type, argv_type): + command = os.path.basename(sys.executable) + argv = argv_type([ + command, '-c', + 'import sys; sys.stdout.write(repr(sys.argv[1:])); sys.exit(7)', + 'one argument with spaces', 'second', + ]) + original = argv[:] + with mock.patch.dict(os.environ, {'PATH': os.path.dirname(sys.executable)}): + child = process_type.spawn(argv) + try: + self.assertEqual(argv, original) + self.assertTrue(os.path.isabs(child.argv[0])) + self.assertNotEqual(child.argv[0], command) + output = child.string_type() + deadline = time.monotonic() + 10 + while True: + remaining = deadline - time.monotonic() + self.assertGreater(remaining, 0, 'Child output timed out') + ready, _, _ = select.select([child.fd], [], [], remaining) + self.assertTrue(ready, 'Child output timed out') + try: + output += child.read() + except EOFError: + break + expected = "['one argument with spaces', 'second']" + if process_type is PtyProcess: + expected = expected.encode('ascii') + self.assertEqual(output, expected) + self.assertEqual(child.wait(), 7) + self.assertEqual(argv, original) + finally: + child.close(force=True) + + def test_spawn_tuple(self): + self._spawn_argv(PtyProcess, tuple) + + def test_spawn_unicode_tuple(self): + self._spawn_argv(PtyProcessUnicode, tuple) + + def test_spawn_list_preserves_argv(self): + self._spawn_argv(PtyProcess, list) + + def test_spawn_unicode_list_preserves_argv(self): + self._spawn_argv(PtyProcessUnicode, list) + def _interactive_repl_unicode(self, echo): """Test Call and response with echo ON/OFF.""" # given,