From: W. Trevor King Date: Wed, 6 Feb 2013 02:21:07 +0000 (-0500) Subject: question: Pass stdin through to the ScriptQuestion-invoked script X-Git-Tag: v0.1~38 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=90dd6fc097cff562d399a11dd75c3e06c3e90cc6;p=quizzer.git 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`). --- 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