From 90dd6fc097cff562d399a11dd75c3e06c3e90cc6 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 5 Feb 2013 21:21:07 -0500 Subject: [PATCH] question: Pass stdin through to the ScriptQuestion-invoked script Use a temporary script to hold the answer script, to leave stdin open. This allows users to use commands that need stdin (e.g. `git commit`, which spawns an `$EDITOR`). --- quizzer/question.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/quizzer/question.py b/quizzer/question.py index 8577b93..9644954 100644 --- a/quizzer/question.py +++ b/quizzer/question.py @@ -132,16 +132,19 @@ class ScriptQuestion (Question): prefix = '{}-'.format(type(self).__name__) if not self.multiline: answer = [answer] - with _tempfile.TemporaryDirectory(prefix=prefix) as tempdir: - script = '\n'.join(self.setup + answer + self.teardown) - status,stdout,stderr = _util.invoke( - args=[self.interpreter], - stdin=script, - cwd=tempdir, - universal_newlines=True, - timeout=self.timeout, - ) - dirname = _os_path.basename(tempdir) + script = '\n'.join(self.setup + answer + self.teardown) + with _tempfile.NamedTemporaryFile( + mode='w', prefix='{}script-'.format(prefix)) as tempscript: + tempscript.write(script) + tempscript.flush() + with _tempfile.TemporaryDirectory(prefix=prefix) as tempdir: + status,stdout,stderr = _util.invoke( + args=[self.interpreter, tempscript.name], + cwd=tempdir, + universal_newlines=True, + timeout=self.timeout, + ) + dirname = _os_path.basename(tempdir) stdout = stdout.replace(dirname, '{}XXXXXX'.format(prefix)) stderr = stderr.replace(dirname, '{}XXXXXX'.format(prefix)) return status,stdout,stderr -- 2.26.2