Skip to content

Commit 01056d4

Browse files
strangepleasuresileasile
authored andcommitted
Fix #326 - proper handling of store_history
1 parent 948e92e commit 01056d4

File tree

8 files changed

+26
-12
lines changed

8 files changed

+26
-12
lines changed

jupyter-lib/test-kit/src/main/kotlin/org/jetbrains/kotlinx/jupyter/testkit/JupyterReplTestCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class JupyterReplTestCase {
1616
}
1717

1818
fun execEx(code: Code): EvalResultEx {
19-
return repl.evalEx(code, null, -1)
19+
return repl.evalEx(code, null, -1, storeHistory)
2020
}
2121

2222
fun exec(code: Code): Any? {

src/main/kotlin/org/jetbrains/kotlinx/jupyter/ikotlin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fun kernelServer(config: KernelConfig, runtimeProperties: ReplRuntimeProperties
108108

109109
log.info("Begin listening for events")
110110

111-
val executionCount = AtomicLong(1)
111+
val executionCount = AtomicLong(0)
112112

113113
val repl = ReplForJupyterImpl(config, runtimeProperties, scriptReceivers)
114114

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@ fun JupyterConnection.Socket.shellMessagesHandler(msg: Message, repl: ReplForJup
265265

266266
is ExecuteRequest -> {
267267
connection.contextMessage = msg
268-
val count = executionCount.getAndIncrement()
268+
val count = executionCount.updateAndGet {
269+
if (content.storeHistory) it + 1 else it
270+
}
269271
val startedTime = ISO8601DateNow
270272

271273
val displayHandler = SocketDisplayHandler(connection.iopub, repl.notebook, msg)
@@ -284,7 +286,7 @@ fun JupyterConnection.Socket.shellMessagesHandler(msg: Message, repl: ReplForJup
284286
runCommand(code, repl)
285287
} else {
286288
connection.evalWithIO(repl, msg) {
287-
repl.eval(code, displayHandler, count.toInt())
289+
repl.eval(code, displayHandler, count.toInt(), content.storeHistory)
288290
}
289291
}
290292

src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ interface ReplOptions {
107107

108108
interface ReplForJupyter {
109109

110-
fun eval(code: Code, displayHandler: DisplayHandler? = null, jupyterId: Int = -1): EvalResult
110+
fun eval(code: Code, displayHandler: DisplayHandler? = null, jupyterId: Int = -1, storeHistory: Boolean = true): EvalResult
111111

112112
fun <T> eval(execution: ExecutionCallback<T>): T
113113

@@ -379,7 +379,7 @@ class ReplForJupyterImpl(
379379
})
380380
}
381381

382-
fun evalEx(code: Code, displayHandler: DisplayHandler?, jupyterId: Int): EvalResultEx {
382+
fun evalEx(code: Code, displayHandler: DisplayHandler?, jupyterId: Int, storeHistory: Boolean): EvalResultEx {
383383
return withEvalContext {
384384
rethrowAsLibraryException(LibraryProblemPart.BEFORE_CELL_CALLBACKS) {
385385
beforeCellExecution.forEach { executor.execute(it) }
@@ -392,7 +392,9 @@ class ReplForJupyterImpl(
392392
val result = try {
393393
log.debug("Current cell id: $jupyterId")
394394
executor.execute(code, displayHandler, currentCellId = jupyterId - 1) { internalId, codeToExecute ->
395-
cell = notebook.addCell(internalId, codeToExecute, EvalData(jupyterId, code))
395+
if (storeHistory) {
396+
cell = notebook.addCell(internalId, codeToExecute, EvalData(jupyterId, code))
397+
}
396398
}
397399
} finally {
398400
compiledData = internalEvaluator.popAddedCompiledScripts()
@@ -430,8 +432,8 @@ class ReplForJupyterImpl(
430432
}
431433
}
432434

433-
override fun eval(code: Code, displayHandler: DisplayHandler?, jupyterId: Int): EvalResult {
434-
return evalEx(code, displayHandler, jupyterId).run { EvalResult(renderedValue, metadata) }
435+
override fun eval(code: Code, displayHandler: DisplayHandler?, jupyterId: Int, storeHistory: Boolean): EvalResult {
436+
return evalEx(code, displayHandler, jupyterId, storeHistory).run { EvalResult(renderedValue, metadata) }
435437
}
436438

437439
override fun <T> eval(execution: ExecutionCallback<T>): T {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ class ExecuteTests : KernelServerTestsBase() {
8585
executeReplyChecker: (Message) -> Unit = {},
8686
inputs: List<String> = emptyList(),
8787
allowStdin: Boolean = true,
88+
storeHistory: Boolean = true
8889
): Any? {
8990
try {
9091
val shell = this.shell!!
9192
val ioPub = this.ioPub!!
9293
val stdin = this.stdin!!
93-
shell.sendMessage(MessageType.EXECUTE_REQUEST, content = ExecuteRequest(code, allowStdin = allowStdin))
94+
shell.sendMessage(MessageType.EXECUTE_REQUEST, content = ExecuteRequest(code, allowStdin = allowStdin, storeHistory = storeHistory))
9495
inputs.forEach {
9596
stdin.sendMessage(MessageType.INPUT_REPLY, InputReply(it))
9697
}
@@ -312,9 +313,11 @@ class ExecuteTests : KernelServerTestsBase() {
312313
}
313314
val res1 = doExecute("42", executeReplyChecker = { checkCounter(it, 1) })
314315
val res2 = doExecute("43", executeReplyChecker = { checkCounter(it, 2) })
316+
val res3 = doExecute("44", storeHistory = false, executeReplyChecker = { checkCounter(it, 2) })
315317

316318
assertEquals(jsonObject("text/plain" to "42"), res1)
317319
assertEquals(jsonObject("text/plain" to "43"), res2)
320+
assertEquals(jsonObject("text/plain" to "44"), res3)
318321
}
319322

320323
@Test

src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/repl/AbstractSingleReplTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ abstract class AbstractSingleReplTest : AbstractReplTest() {
88
protected abstract val repl: ReplForJupyter
99

1010
protected fun eval(code: Code, displayHandler: DisplayHandler? = null, jupyterId: Int = -1) =
11-
repl.eval(code, displayHandler, jupyterId)
11+
repl.eval(code, displayHandler, jupyterId, jupyterId > 0)
1212
}

src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/repl/ReplTests.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ class ReplTests : AbstractSingleReplTest() {
385385
assertFails { eval("Out[3]") }
386386
}
387387

388+
@Test
389+
fun testNoHistory() {
390+
eval("1+1", null)
391+
assertFails { eval("Out[1]") }
392+
}
393+
388394
@Test
389395
fun testOutputMagic() {
390396
eval("%output --max-cell-size=100500 --no-stdout")

src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/repl/ReplWithTestResolverTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class ReplWithTestResolverTests : AbstractSingleReplTest() {
4848
"Bill", 135,
4949
"Mark", 160
5050
).typed<Unit>()
51-
""".trimIndent()
51+
""".trimIndent(),
52+
jupyterId = 1
5253
)
5354

5455
val value = res.resultValue

0 commit comments

Comments
 (0)