Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ptyprocess/ptyprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down