From b828885eff508a904ff7a5a0c28207dbfb7d2387 Mon Sep 17 00:00:00 2001 From: Tim Felgentreff Date: Tue, 30 Oct 2018 14:05:45 +0100 Subject: [PATCH] support the 'input' builtin (cherry picked from commit 5743c7c44a3b9f11b115a6c145d7e1ca4f6fdd15) --- .../builtins/modules/BuiltinFunctions.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 7a4188a19a..2772881e3b 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -68,14 +68,18 @@ import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError; import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; +import java.nio.CharBuffer; import java.util.List; import java.util.function.Supplier; import com.oracle.graal.python.PythonLanguage; import com.oracle.graal.python.builtins.Builtin; import com.oracle.graal.python.builtins.CoreFunctions; +import com.oracle.graal.python.builtins.PythonBuiltinClassType; import com.oracle.graal.python.builtins.PythonBuiltins; import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.GetAttrNodeFactory; import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.NextNodeFactory; @@ -1535,4 +1539,44 @@ public Object doit(Object object) { return "truffle ast dump not supported for " + object.toString(); } } + + @Builtin(name = "input", keywordArguments = {"prompt"}) + @GenerateNodeFactory + abstract static class InputNode extends PythonUnaryBuiltinNode { + @Specialization + @TruffleBoundary + String input(@SuppressWarnings("unused") PNone prompt) { + CharBuffer buf = CharBuffer.allocate(1000); + try { + InputStream stdin = getContext().getStandardIn(); + int read = stdin.read(); + while (read != -1 && read != '\n') { + if (buf.remaining() == 0) { + CharBuffer newBuf = CharBuffer.allocate(buf.capacity() * 2); + newBuf.put(buf); + buf = newBuf; + } + buf.put((char) read); + read = stdin.read(); + } + buf.limit(buf.position()); + buf.rewind(); + return buf.toString(); + } catch (IOException e) { + throw raise(PythonBuiltinClassType.EOFError, e.getMessage()); + } + } + + @Specialization + String inputPrompt(PString prompt) { + return inputPrompt(prompt.getValue()); + } + + @Specialization + @TruffleBoundary + String inputPrompt(String prompt) { + new PrintStream(getContext().getStandardOut()).println(prompt); + return input(null); + } + } }