Skip to content

Commit ec6f947

Browse files
committed
Fix increment logic, introduce structure for the eval() method
1 parent 258d102 commit ec6f947

File tree

10 files changed

+60
-28
lines changed

10 files changed

+60
-28
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.jetbrains.kotlinx.jupyter.testkit
33
import io.kotest.matchers.nulls.shouldNotBeNull
44
import io.kotest.matchers.types.shouldBeInstanceOf
55
import jupyter.kotlin.DependsOn
6+
import org.jetbrains.kotlinx.jupyter.EvalRequestData
67
import org.jetbrains.kotlinx.jupyter.ReplForJupyterImpl
78
import org.jetbrains.kotlinx.jupyter.api.Code
89
import org.jetbrains.kotlinx.jupyter.api.MimeTypedResult
@@ -16,7 +17,7 @@ abstract class JupyterReplTestCase {
1617
}
1718

1819
fun execEx(code: Code): EvalResultEx {
19-
return repl.evalEx(code, null, -1, true)
20+
return repl.evalEx(EvalRequestData(code))
2021
}
2122

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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ class CodeCellImpl(
9595
class EvalData(
9696
val executionCounter: Int,
9797
val rawCode: String,
98-
)
98+
) {
99+
constructor(evalRequestData: EvalRequestData) : this(evalRequestData.jupyterId, evalRequestData.code)
100+
}
99101

100102
class NotebookImpl(
101103
private val runtimeProperties: ReplRuntimeProperties,

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(0)
111+
val executionCount = AtomicLong(1)
112112

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

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

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

266266
is ExecuteRequest -> {
267267
connection.contextMessage = msg
268-
val count = executionCount.updateAndGet {
268+
val count = executionCount.getAndUpdate {
269269
if (content.storeHistory) it + 1 else it
270270
}
271271
val startedTime = ISO8601DateNow
@@ -286,7 +286,15 @@ fun JupyterConnection.Socket.shellMessagesHandler(msg: Message, repl: ReplForJup
286286
runCommand(code, repl)
287287
} else {
288288
connection.evalWithIO(repl, msg) {
289-
repl.eval(code, displayHandler, count.toInt(), content.storeHistory)
289+
repl.eval(
290+
EvalRequestData(
291+
code,
292+
displayHandler,
293+
count.toInt(),
294+
content.storeHistory,
295+
content.silent,
296+
)
297+
)
290298
}
291299
}
292300

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ import kotlin.script.experimental.jvm.jvm
7979

8080
data class CheckResult(val isComplete: Boolean = true)
8181

82+
class EvalRequestData(
83+
val code: Code,
84+
val displayHandler: DisplayHandler? = null,
85+
val jupyterId: Int = -1,
86+
val storeHistory: Boolean = true,
87+
val isSilent: Boolean = false,
88+
)
89+
8290
class ReplEvalRuntimeException(message: String, cause: Throwable? = null) : ReplException(message, cause)
8391

8492
enum class ExecutedCodeLogging {
@@ -107,7 +115,8 @@ interface ReplOptions {
107115

108116
interface ReplForJupyter {
109117

110-
fun eval(code: Code, displayHandler: DisplayHandler? = null, jupyterId: Int = -1, storeHistory: Boolean = true): EvalResult
118+
fun eval(code: Code): EvalResult = eval(EvalRequestData(code))
119+
fun eval(evalData: EvalRequestData): EvalResult
111120

112121
fun <T> eval(execution: ExecutionCallback<T>): T
113122

@@ -379,7 +388,7 @@ class ReplForJupyterImpl(
379388
})
380389
}
381390

382-
fun evalEx(code: Code, displayHandler: DisplayHandler?, jupyterId: Int, storeHistory: Boolean): EvalResultEx {
391+
fun evalEx(evalData: EvalRequestData): EvalResultEx {
383392
return withEvalContext {
384393
rethrowAsLibraryException(LibraryProblemPart.BEFORE_CELL_CALLBACKS) {
385394
beforeCellExecution.forEach { executor.execute(it) }
@@ -390,10 +399,10 @@ class ReplForJupyterImpl(
390399
val compiledData: SerializedCompiledScriptsData
391400
val newImports: List<String>
392401
val result = try {
393-
log.debug("Current cell id: $jupyterId")
394-
executor.execute(code, displayHandler, currentCellId = jupyterId - 1) { internalId, codeToExecute ->
395-
if (storeHistory) {
396-
cell = notebook.addCell(internalId, codeToExecute, EvalData(jupyterId, code))
402+
log.debug("Current cell id: ${evalData.jupyterId}")
403+
executor.execute(evalData.code, evalData.displayHandler, currentCellId = evalData.jupyterId - 1) { internalId, codeToExecute ->
404+
if (evalData.storeHistory) {
405+
cell = notebook.addCell(internalId, codeToExecute, EvalData(evalData))
397406
}
398407
}
399408
} finally {
@@ -432,8 +441,8 @@ class ReplForJupyterImpl(
432441
}
433442
}
434443

435-
override fun eval(code: Code, displayHandler: DisplayHandler?, jupyterId: Int, storeHistory: Boolean): EvalResult {
436-
return evalEx(code, displayHandler, jupyterId, storeHistory).run { EvalResult(renderedValue, metadata) }
444+
override fun eval(evalData: EvalRequestData): EvalResult {
445+
return evalEx(evalData).run { EvalResult(renderedValue, metadata) }
437446
}
438447

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ApiTest : AbstractSingleReplTest() {
1515
override val repl = makeSimpleRepl()
1616

1717
private fun jEval(jupyterId: Int, code: String): EvalResult {
18-
return repl.eval(code, jupyterId = jupyterId)
18+
return eval(code, jupyterId = jupyterId)
1919
}
2020

2121
@Test
@@ -46,7 +46,7 @@ class ApiTest : AbstractSingleReplTest() {
4646

4747
@Test
4848
fun testVarsReportFormat() {
49-
val res = eval(
49+
eval(
5050
"""
5151
val x = 1
5252
val y = "abc"

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,23 @@ class ExecuteTests : KernelServerTestsBase() {
311311
val data = message.data.content as ExecuteReply
312312
assertEquals(expectedCounter, data.executionCount)
313313
}
314-
val res1 = doExecute("42", executeReplyChecker = { checkCounter(it, 1) })
315-
val res2 = doExecute("43", executeReplyChecker = { checkCounter(it, 2) })
316-
val res3 = doExecute("44", storeHistory = false, executeReplyChecker = { checkCounter(it, 2) })
314+
val res1 = doExecute("41", executeReplyChecker = { checkCounter(it, 1) })
315+
val res2 = doExecute("42", executeReplyChecker = { checkCounter(it, 2) })
316+
val res3 = doExecute(
317+
" \"\${Out[1]} \${Out[2]}\" ",
318+
storeHistory = false,
319+
executeReplyChecker = { checkCounter(it, 3) }
320+
)
321+
val res4 = doExecute(
322+
"try { Out[3] } catch(e: ArrayIndexOutOfBoundsException) { null }",
323+
storeHistory = false,
324+
executeReplyChecker = { checkCounter(it, 3) }
325+
)
317326

318-
assertEquals(jsonObject("text/plain" to "42"), res1)
319-
assertEquals(jsonObject("text/plain" to "43"), res2)
320-
assertEquals(jsonObject("text/plain" to "44"), res3)
327+
assertEquals(jsonObject("text/plain" to "41"), res1)
328+
assertEquals(jsonObject("text/plain" to "42"), res2)
329+
assertEquals(jsonObject("text/plain" to "41 42"), res3)
330+
assertEquals(jsonObject("text/plain" to "null"), res4)
321331
}
322332

323333
@Test
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.jetbrains.kotlinx.jupyter.test.repl
22

33
import org.jetbrains.kotlinx.jupyter.DisplayHandler
4+
import org.jetbrains.kotlinx.jupyter.EvalRequestData
45
import org.jetbrains.kotlinx.jupyter.ReplForJupyter
56
import org.jetbrains.kotlinx.jupyter.api.Code
67

78
abstract class AbstractSingleReplTest : AbstractReplTest() {
89
protected abstract val repl: ReplForJupyter
910

10-
protected fun eval(code: Code, displayHandler: DisplayHandler? = null, jupyterId: Int = -1) =
11-
repl.eval(code, displayHandler, jupyterId, jupyterId > 0)
11+
protected fun eval(code: Code, displayHandler: DisplayHandler? = null, jupyterId: Int = -1, storeHistory: Boolean = true) =
12+
repl.eval(EvalRequestData(code, displayHandler, jupyterId, storeHistory))
1213
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlinx.coroutines.runBlocking
55
import kotlinx.serialization.encodeToString
66
import kotlinx.serialization.json.Json
77
import org.jetbrains.kotlinx.jupyter.OutputConfig
8+
import org.jetbrains.kotlinx.jupyter.ReplEvalRuntimeException
89
import org.jetbrains.kotlinx.jupyter.api.VariableStateImpl
910
import org.jetbrains.kotlinx.jupyter.exceptions.ReplCompilerException
1011
import org.jetbrains.kotlinx.jupyter.generateDiagnostic
@@ -23,6 +24,7 @@ import java.io.File
2324
import kotlin.script.experimental.api.SourceCode
2425
import kotlin.test.assertEquals
2526
import kotlin.test.assertFails
27+
import kotlin.test.assertFailsWith
2628
import kotlin.test.assertFalse
2729
import kotlin.test.assertNotNull
2830
import kotlin.test.fail
@@ -387,8 +389,8 @@ class ReplTests : AbstractSingleReplTest() {
387389

388390
@Test
389391
fun testNoHistory() {
390-
eval("1+1", null)
391-
assertFails { eval("Out[1]") }
392+
eval("1+1", storeHistory = false)
393+
assertFailsWith<ReplEvalRuntimeException>("There is no cell with number '1'") { eval("Out[1]") }
392394
}
393395

394396
@Test
@@ -484,7 +486,7 @@ class ReplVarsTest : AbstractSingleReplTest() {
484486
""".trimIndent()
485487
)
486488

487-
val varsUpdate = mutableMapOf<String, String>(
489+
val varsUpdate = mutableMapOf(
488490
"x" to "1",
489491
"y" to "0",
490492
"z" to "47"

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

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

5554
val value = res.resultValue

0 commit comments

Comments
 (0)