question: Pass stdin through to the ScriptQuestion-invoked script
authorW. Trevor King <wking@tremily.us>
Wed, 6 Feb 2013 02:21:07 +0000 (21:21 -0500)
committerW. Trevor King <wking@tremily.us>
Wed, 6 Feb 2013 02:25:22 +0000 (21:25 -0500)
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

index 8577b93aa610adec163b56bc5023998ee824f057..96449541e229bb0c410915c0db52e067fd1123e2 100644 (file)
@@ -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