-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pass_fds parameter to PtyProcess:spawn() #49
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import fcntl | ||
import os | ||
import time | ||
import select | ||
import tempfile | ||
import unittest | ||
from ptyprocess.ptyprocess import which | ||
from ptyprocess import PtyProcess, PtyProcessUnicode | ||
|
@@ -111,3 +113,37 @@ def test_interactive_repl_unicode_noecho(self): | |
@unittest.skipIf(which('bc') is None, "bc(1) not found on this server.") | ||
def test_interactive_repl_unicode_echo(self): | ||
self._interactive_repl_unicode(echo=True) | ||
|
||
def test_pass_fds(self): | ||
with tempfile.NamedTemporaryFile() as temp_file: | ||
temp_file_fd = temp_file.fileno() | ||
temp_file_name = temp_file.name | ||
|
||
# Temporary files are CLOEXEC by default | ||
fcntl.fcntl(temp_file_fd, | ||
fcntl.F_SETFD, | ||
fcntl.fcntl(temp_file_fd, fcntl.F_GETFD) & | ||
~fcntl.FD_CLOEXEC) | ||
|
||
# You can write with pass_fds | ||
p = PtyProcess.spawn(['bash', | ||
'-c', | ||
'printf hello >&{}'.format(temp_file_fd)], | ||
echo=True, | ||
pass_fds=(temp_file_fd,)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth having a test that when you don't specify it in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that the more tests, the better :) I've added that one too. |
||
p.wait() | ||
assert p.status == 0 | ||
|
||
with open(temp_file_name, 'r') as temp_file_r: | ||
assert temp_file_r.read() == 'hello' | ||
|
||
# You can't write without pass_fds | ||
p = PtyProcess.spawn(['bash', | ||
'-c', | ||
'printf bye >&{}'.format(temp_file_fd)], | ||
echo=True) | ||
p.wait() | ||
assert p.status != 0 | ||
|
||
with open(temp_file_name, 'r') as temp_file_r: | ||
assert temp_file_r.read() == 'hello' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: you document it as a list, but it will only work with a tuple, because you concatenate it to a tuple (
pass_fds + (exec_err_pipe_write,)
). Let's cast it to a set, like subprocess does; that should work with any sequence, and also eliminates any odd behaviour with duplicate entries.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right!