Skip to content

Commit 868a63e

Browse files
Pavel Mikhailovskiistrangepleasures
Pavel Mikhailovskii
andauthoredFeb 5, 2021
Handle allow_stdin=false (#124)
Co-authored-by: Pavel Mikhailovskii <pavel.mikhailovskii@jetbrains.com>
1 parent 0ea9551 commit 868a63e

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ artifacts/
2121

2222
# Project Python virtual environments
2323
/venv/
24+
/distrib/venv/
2425

2526
# Jupyter notebooks checkpoints
2627
**/.ipynb_checkpoints

‎gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# kotlinVersion=1.4.255-SNAPSHOT
2-
kotlinVersion=1.5.0-dev-1206
2+
kotlinVersion=1.4.30
33
kotlinLanguageLevel=1.4
44
jvmTarget=1.8
55

‎src/main/kotlin/org/jetbrains/kotlin/jupyter/connection.kt

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.min
88
import org.zeromq.SocketType
99
import org.zeromq.ZMQ
1010
import java.io.Closeable
11+
import java.io.IOException
1112
import java.security.SignatureException
1213
import javax.crypto.Mac
1314
import javax.crypto.spec.SecretKeySpec
@@ -208,3 +209,9 @@ fun ZMQ.Socket.receiveMessage(start: ByteArray, hmac: HMAC): Message? {
208209
content.parseJson()
209210
)
210211
}
212+
213+
object DisabledStdinInputStream : java.io.InputStream() {
214+
override fun read(): Int {
215+
throw IOException("Input from stdin is unsupported by the client")
216+
}
217+
}

‎src/main/kotlin/org/jetbrains/kotlin/jupyter/protocol.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ fun JupyterConnection.evalWithIO(config: OutputConfig, srcMessage: Message, body
394394
System.setErr(PrintStream(forkedError, false, "UTF-8"))
395395

396396
val `in` = System.`in`
397-
System.setIn(stdinIn)
397+
val allowStdIn = srcMessage.content?.boolean("allow_stdin") ?: true
398+
System.setIn(if (allowStdIn) stdinIn else DisabledStdinInputStream)
398399
try {
399400
return try {
400401
val exec = body()

‎src/test/kotlin/org/jetbrains/kotlin/jupyter/test/executeTests.kt

+26-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.jetbrains.kotlin.jupyter.get
77
import org.jetbrains.kotlin.jupyter.jsonObject
88
import org.junit.jupiter.api.Assertions.assertEquals
99
import org.junit.jupiter.api.Assertions.assertNull
10+
import org.junit.jupiter.api.Assertions.assertTrue
1011
import org.junit.jupiter.api.Test
1112
import org.junit.jupiter.api.Timeout
1213
import org.zeromq.ZMQ
@@ -24,6 +25,7 @@ class ExecuteTests : KernelServerTestsBase() {
2425
hasResult: Boolean = true,
2526
ioPubChecker: (ZMQ.Socket) -> Unit = {},
2627
inputs: List<String> = emptyList(),
28+
allowStdin: Boolean = true,
2729
): Any? {
2830
val context = ZMQ.context(1)
2931
val shell = ClientSocket(context, JupyterSockets.shell)
@@ -35,7 +37,7 @@ class ExecuteTests : KernelServerTestsBase() {
3537
ioPub.connect()
3638
stdin.connect()
3739

38-
shell.sendMessage("execute_request", content = jsonObject("code" to code))
40+
shell.sendMessage("execute_request", content = jsonObject("code" to code, "allow_stdin" to allowStdin))
3941
inputs.forEach {
4042
stdin.sendMessage("input_reply", jsonObject("value" to it))
4143
}
@@ -69,6 +71,19 @@ class ExecuteTests : KernelServerTestsBase() {
6971
}
7072
}
7173

74+
private fun testWithNoStdin(code: String) {
75+
doExecute(
76+
code,
77+
hasResult = false,
78+
allowStdin = false,
79+
ioPubChecker = {
80+
val msg = it.receiveMessage()
81+
assertEquals("stream", msg.type())
82+
assertTrue((msg.content!!["text"] as String).startsWith("java.io.IOException: Input from stdin is unsupported by the client"))
83+
}
84+
)
85+
}
86+
7287
@Test
7388
fun testExecute() {
7489
val res = doExecute("2+2") as JsonObject
@@ -153,4 +168,14 @@ class ExecuteTests : KernelServerTestsBase() {
153168
val res = doExecute(code, inputs = listOf("42"))
154169
assertEquals(jsonObject("text/plain" to "42"), res)
155170
}
171+
172+
@Test
173+
fun testReadLineWithNoStdin() {
174+
testWithNoStdin("readLine() ?: \"blah\"")
175+
}
176+
177+
@Test
178+
fun testStdinReadWithNoStdin() {
179+
testWithNoStdin("System.`in`.read()")
180+
}
156181
}

0 commit comments

Comments
 (0)
Please sign in to comment.