Skip to content

Commit 9777393

Browse files
authored
Python small fixes (#2250)
* Swap focusedFile and focusedClass * Fix invalid string representation of float and keys in state-dict * Fix code generation for tuple * Fix \ in strings and coverage socket * Fix missed module imports from stateInit
1 parent a209473 commit 9777393

File tree

9 files changed

+48
-25
lines changed

9 files changed

+48
-25
lines changed

utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonLanguageAssistant.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ object PythonLanguageAssistant : LanguageAssistant() {
4444
PythonDialogProcessor.createDialogAndGenerateTests(
4545
project,
4646
targets.pyClasses + targets.pyFunctions,
47-
targets.focusedClass ?: targets.focusedFunction,
47+
targets.focusedFunction ?: targets.focusedClass,
4848
targets.editor,
4949
)
5050
}
@@ -66,8 +66,8 @@ object PythonLanguageAssistant : LanguageAssistant() {
6666
val file = e.getData(CommonDataKeys.PSI_FILE) as? PyFile ?: return null
6767
val element = findPsiElement(file, editor) ?: return null
6868

69-
val allFunctions = file.topLevelFunctions.filter { fineFunction(it) }
70-
val allClasses = file.topLevelClasses.filter { fineClass(it) }
69+
val rootFunctions = file.topLevelFunctions.filter { fineFunction(it) }
70+
val rootClasses = file.topLevelClasses.filter { fineClass(it) }
7171

7272
val containingClass = getContainingElement<PyClass>(element) { fineClass(it) }
7373
val containingFunction: PyFunction? =
@@ -80,18 +80,18 @@ object PythonLanguageAssistant : LanguageAssistant() {
8080
ancestors.count { it is PyClass } == 1 && fineFunction(func)
8181
}
8282

83-
if (allClasses.isEmpty()) {
84-
return if (allFunctions.isEmpty()) {
83+
if (rootClasses.isEmpty()) {
84+
return if (rootFunctions.isEmpty()) {
8585
null
8686
} else {
87-
resultFunctions.addAll(allFunctions)
87+
resultFunctions.addAll(rootFunctions)
8888
focusedFunction = containingFunction
8989
Targets(resultClasses, resultFunctions, null, focusedFunction, editor)
9090
}
9191
} else {
9292
if (containingClass == null) {
93-
resultClasses.addAll(allClasses)
94-
resultFunctions.addAll(allFunctions)
93+
resultClasses.addAll(rootClasses)
94+
resultFunctions.addAll(rootFunctions)
9595
focusedFunction = containingFunction
9696
} else {
9797
resultFunctions.addAll(containingClass.methods.filter { fineFunction(it) })

utbot-python/samples/easy_samples/general.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,9 @@ def n(x, y):
206206

207207
def list_of_list(x: List[List[InventoryItem]]):
208208
return x
209+
210+
211+
def make_tuple(x: list[int]):
212+
if len(x) == 0:
213+
return tuple()
214+
return tuple(x)

utbot-python/src/main/kotlin/org/utbot/python/PythonEngine.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ class PythonEngine(
339339
logger.info { "Cannot fuzz values for types: ${parameters.map { it.pythonTypeRepresentation() }}" }
340340
}
341341
}
342-
manager.disconnect()
342+
manager.shutdown()
343343
}
344344
}
345345
}

utbot-python/src/main/kotlin/org/utbot/python/PythonTestGenerationProcessor.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import org.utbot.python.code.PythonCode
1818
import org.utbot.python.framework.api.python.PythonClassId
1919
import org.utbot.python.framework.api.python.PythonMethodId
2020
import org.utbot.python.framework.api.python.PythonModel
21+
import org.utbot.python.framework.api.python.PythonUtExecution
2122
import org.utbot.python.framework.api.python.RawPythonAnnotation
2223
import org.utbot.python.framework.api.python.util.pythonAnyClassId
2324
import org.utbot.python.framework.codegen.model.PythonCodeGenerator
@@ -153,8 +154,13 @@ object PythonTestGenerationProcessor {
153154

154155
val importParamModules = notEmptyTests.flatMap { testSet ->
155156
testSet.executions.flatMap { execution ->
156-
(execution.stateBefore.parameters + execution.stateAfter.parameters +
157-
listOf(execution.stateBefore.thisInstance, execution.stateAfter.thisInstance))
157+
val params = (execution.stateAfter.parameters + execution.stateBefore.parameters).toMutableSet()
158+
val self = mutableListOf(execution.stateBefore.thisInstance, execution.stateAfter.thisInstance)
159+
if (execution is PythonUtExecution) {
160+
params.addAll(execution.stateInit.parameters)
161+
self.add(execution.stateInit.thisInstance)
162+
}
163+
(params + self)
158164
.filterNotNull()
159165
.flatMap { utModel ->
160166
(utModel as PythonModel).let {

utbot-python/src/main/kotlin/org/utbot/python/evaluation/PythonWorkerManager.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PythonWorkerManager(
3333
connect()
3434
}
3535

36-
fun connect() {
36+
private fun connect() {
3737
val processStartTime = System.currentTimeMillis()
3838
process = startProcess(listOf(
3939
pythonPath,
@@ -65,14 +65,18 @@ class PythonWorkerManager(
6565
fun disconnect() {
6666
workerSocket.close()
6767
process.destroy()
68-
coverageReceiver.kill()
6968
}
7069

71-
fun reconnect() {
70+
private fun reconnect() {
7271
disconnect()
7372
connect()
7473
}
7574

75+
fun shutdown() {
76+
disconnect()
77+
coverageReceiver.kill()
78+
}
79+
7680
fun runWithCoverage(
7781
fuzzedValues: FunctionArguments,
7882
additionalModulesToImport: Set<String>,

utbot-python/src/main/kotlin/org/utbot/python/evaluation/serialiation/PythonObjectParser.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
55
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
66
import org.utbot.python.framework.api.python.PythonClassId
77
import org.utbot.python.framework.api.python.PythonTree
8-
import org.utbot.python.framework.api.python.util.pythonStrClassId
98

109
object PythonObjectParser {
1110
private val moshi = Moshi.Builder()
@@ -149,10 +148,7 @@ fun PythonTree.PythonTreeNode.toMemoryObject(memoryDump: MemoryDump): String {
149148

150149
is PythonTree.ReduceNode -> {
151150
val stateObjId = PythonTree.DictNode(this.state.entries.associate {
152-
PythonTree.PrimitiveNode(
153-
pythonStrClassId,
154-
it.key
155-
) to it.value
151+
PythonTree.fromString(it.key) to it.value
156152
}.toMutableMap())
157153
val argsIds = PythonTree.ListNode(this.args.withIndex().associate { it.index to it.value }.toMutableMap())
158154
val listItemsIds =

utbot-python/src/main/kotlin/org/utbot/python/framework/api/python/PythonTree.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,15 @@ object PythonTree {
305305
}
306306

307307
fun fromFloat(value: Double): PrimitiveNode {
308+
val stringValue =
309+
when (value) {
310+
Double.POSITIVE_INFINITY -> "float('inf')"
311+
Double.NEGATIVE_INFINITY -> "-float('inf')"
312+
else -> value.toString()
313+
}
308314
return PrimitiveNode(
309315
pythonFloatClassId,
310-
value.toString()
316+
stringValue
311317
)
312318
}
313319

utbot-python/src/main/kotlin/org/utbot/python/framework/api/python/util/PythonUtils.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fun String.toSnakeCase(): String {
1818

1919
fun String.toPythonRepr(): String {
2020
val repr = this
21+
.replace(Regex("((?<!\\\\)(\\\\\\\\)*)(\\\\)(?!\\\\)$"), "$1$3$3")
2122
.replace("\"", "\\\"")
2223
.replace("\\\\\"", "\\\"")
2324
return "\"$repr\""

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/visitor/CgPythonRenderer.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,12 +536,16 @@ internal class CgPythonRenderer(
536536
}
537537

538538
override fun visit(element: CgPythonTuple) {
539-
print("(")
540-
element.elements.renderSeparated()
541-
if (element.elements.size == 1) {
542-
print(",")
539+
if (element.elements.isEmpty()) {
540+
print("tuple()")
541+
} else {
542+
print("(")
543+
element.elements.renderSeparated()
544+
if (element.elements.size == 1) {
545+
print(",")
546+
}
547+
print(")")
543548
}
544-
print(")")
545549
}
546550

547551
override fun visit(element: CgPythonSet) {

0 commit comments

Comments
 (0)