Skip to content
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

Support clients without stdin support (allow_stdin=false) #124

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ artifacts/

# Project Python virtual environments
/venv/
/distrib/venv/

# Jupyter notebooks checkpoints
**/.ipynb_checkpoints
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# kotlinVersion=1.4.255-SNAPSHOT
kotlinVersion=1.5.0-dev-1206
kotlinVersion=1.4.30
kotlinLanguageLevel=1.4
jvmTarget=1.8

Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/org/jetbrains/kotlin/jupyter/connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.min
import org.zeromq.SocketType
import org.zeromq.ZMQ
import java.io.Closeable
import java.io.IOException
import java.security.SignatureException
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
Expand Down Expand Up @@ -208,3 +209,9 @@ fun ZMQ.Socket.receiveMessage(start: ByteArray, hmac: HMAC): Message? {
content.parseJson()
)
}

object DisabledStdinInputStream : java.io.InputStream() {
override fun read(): Int {
throw IOException("Input from stdin is unsupported by the client")
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/org/jetbrains/kotlin/jupyter/protocol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ fun JupyterConnection.evalWithIO(config: OutputConfig, srcMessage: Message, body
System.setErr(PrintStream(forkedError, false, "UTF-8"))

val `in` = System.`in`
System.setIn(stdinIn)
val allowStdIn = srcMessage.content?.boolean("allow_stdin") ?: true
System.setIn(if (allowStdIn) stdinIn else DisabledStdinInputStream)
try {
return try {
val exec = body()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.kotlin.jupyter.get
import org.jetbrains.kotlin.jupyter.jsonObject
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Timeout
import org.zeromq.ZMQ
Expand All @@ -24,6 +25,7 @@ class ExecuteTests : KernelServerTestsBase() {
hasResult: Boolean = true,
ioPubChecker: (ZMQ.Socket) -> Unit = {},
inputs: List<String> = emptyList(),
allowStdin: Boolean = true,
): Any? {
val context = ZMQ.context(1)
val shell = ClientSocket(context, JupyterSockets.shell)
Expand All @@ -35,7 +37,7 @@ class ExecuteTests : KernelServerTestsBase() {
ioPub.connect()
stdin.connect()

shell.sendMessage("execute_request", content = jsonObject("code" to code))
shell.sendMessage("execute_request", content = jsonObject("code" to code, "allow_stdin" to allowStdin))
inputs.forEach {
stdin.sendMessage("input_reply", jsonObject("value" to it))
}
Expand Down Expand Up @@ -69,6 +71,19 @@ class ExecuteTests : KernelServerTestsBase() {
}
}

private fun testWithNoStdin(code: String) {
doExecute(
code,
hasResult = false,
allowStdin = false,
ioPubChecker = {
val msg = it.receiveMessage()
assertEquals("stream", msg.type())
assertTrue((msg.content!!["text"] as String).startsWith("java.io.IOException: Input from stdin is unsupported by the client"))
}
)
}

@Test
fun testExecute() {
val res = doExecute("2+2") as JsonObject
Expand Down Expand Up @@ -153,4 +168,14 @@ class ExecuteTests : KernelServerTestsBase() {
val res = doExecute(code, inputs = listOf("42"))
assertEquals(jsonObject("text/plain" to "42"), res)
}

@Test
fun testReadLineWithNoStdin() {
testWithNoStdin("readLine() ?: \"blah\"")
}

@Test
fun testStdinReadWithNoStdin() {
testWithNoStdin("System.`in`.read()")
}
}