Skip to content

Commit e18787a

Browse files
committed
Update ReduceNode rendering and fix format
1 parent 3eecb21 commit e18787a

File tree

11 files changed

+179
-129
lines changed

11 files changed

+179
-129
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.utbot.python
22

3+
import kotlinx.coroutines.currentCoroutineContext
34
import kotlinx.coroutines.flow.Flow
45
import kotlinx.coroutines.flow.asFlow
56
import kotlinx.coroutines.flow.flow
7+
import kotlinx.coroutines.flow.isActive
8+
import kotlinx.coroutines.isActive
69
import mu.KotlinLogging
710
import org.utbot.framework.plugin.api.*
811
import org.utbot.fuzzer.*
@@ -244,6 +247,9 @@ class PythonEngine(
244247
evaluationInput.modelList
245248
)
246249

250+
if (!currentCoroutineContext().isActive) {
251+
return@PythonFuzzing PythonFeedback(control = Control.STOP)
252+
}
247253
when (val evaluationResult = jobResult.evalResult) {
248254
is PythonEvaluationError -> {
249255
if (evaluationResult.status != 0) {

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ import org.utbot.python.framework.api.python.NormalizedPythonAnnotation
1212
import org.utbot.python.framework.api.python.util.pythonAnyClassId
1313
import org.utbot.python.newtyping.PythonTypeDescription
1414
import org.utbot.python.newtyping.general.FunctionTypeCreator
15-
import org.utbot.python.newtyping.readMypyAnnotationStorage
15+
import org.utbot.python.newtyping.runmypy.RunMypy
1616
import org.utbot.python.typing.AnnotationFinder.findAnnotations
1717
import org.utbot.python.typing.MypyAnnotations
1818
import org.utbot.python.utils.AnnotationNormalizer.annotationFromProjectToClassId
19-
import org.utbot.python.utils.TemporaryFileManager
20-
import org.utbot.python.utils.runCommand
2119
import java.nio.file.Path
2220

2321
private val logger = KotlinLogging.logger {}
@@ -63,24 +61,13 @@ object PythonTestCaseGenerator {
6361
}
6462

6563
private fun newGenerate(method: PythonMethod): PythonTestSet {
66-
val mypyAnnotationSource = PythonTestCaseGenerator::class.java.getResource("/mypy/extract_annotations.py")?.readText() ?: error("Didn't find /mypy/extract_annotations.py")
67-
val mypyAnnotationFile = TemporaryFileManager.createTemporaryFile(mypyAnnotationSource, tag = "mypy_file")
68-
val mypyAnnotationPath = mypyAnnotationFile.absolutePath
69-
70-
val mypyIniSource = PythonTestCaseGenerator::class.java.getResource("/mypy/mypy_config.ini")?.readText() ?: error("Didn't find /mypy/mypy_config.ini")
71-
val mypyIniFile = TemporaryFileManager.createTemporaryFile(mypyIniSource, tag = "mypy_ini")
72-
val mypyIniPath = mypyIniFile.absolutePath
73-
val results = runCommand(
74-
listOf(
75-
pythonPath,
76-
mypyAnnotationPath,
77-
mypyIniPath,
78-
method.moduleFilename,
79-
)
64+
val mypyConfigFile = RunMypy.setConfigFile(directoriesForSysPath)
65+
val (storage, _) = RunMypy.readMypyAnnotationStorageAndInitialErrors(
66+
pythonPath,
67+
method.moduleFilename,
68+
mypyConfigFile
8069
)
81-
val moduleName = method.moduleFilename.split("/").last().split(".").first()
82-
val storage = readMypyAnnotationStorage(results.stdout)
83-
val functionDef = (storage.definitions[moduleName]!![method.name]!!.annotation.asUtBotType as FunctionTypeCreator.Original)
70+
val functionDef = (storage.definitions[curModule]!![method.name]!!.annotation.asUtBotType as FunctionTypeCreator.Original)
8471
val args = functionDef.arguments
8572

8673
storageForMypyMessages.clear()
@@ -135,6 +122,10 @@ object PythonTestCaseGenerator {
135122
if (withMinimization && missingLines?.isEmpty() == true) {//&& generated % CHUNK_SIZE == 0)
136123
coverageLimit = 0
137124
}
125+
126+
if (coverageLimit == 0) {
127+
isCancelled = {true}
128+
}
138129
}
139130
}
140131

utbot-python/src/main/kotlin/org/utbot/python/code/KlaxonPythonTreeParser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class KlaxonPythonTreeParser(
7676
memory[id] = initObject
7777
initObject.state = parsePythonDict(value.array("state")!!).items.map {
7878
(it.key as PythonTree.PrimitiveNode).repr to it.value
79-
}.toMap()
79+
}.toMap().toMutableMap()
8080
initObject.listitems = parsePythonList(value.array("listitems")!!).children
8181
initObject.dictitems = parsePythonDict(value.array("dictitems")!!).items
8282
return initObject

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class PythonTreeModel(
6060
override val allContainingClassIds: Set<PythonClassId>
6161
get() {
6262
val children = tree.children.map { PythonTreeModel(it, it.type) }
63-
return super.allContainingClassIds + children.flatMap { it.allContainingClassIds }
63+
return super.allContainingClassIds + children.filter {it.tree != tree}.flatMap { it.allContainingClassIds }
6464
}
6565
}
6666

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ object PythonTree {
140140
type: PythonClassId,
141141
val constructor: PythonClassId,
142142
val args: List<PythonTreeNode>,
143-
var state: Map<String, PythonTreeNode>,
143+
var state: MutableMap<String, PythonTreeNode>,
144144
var listitems: List<PythonTreeNode>,
145145
var dictitems: Map<PythonTreeNode, PythonTreeNode>,
146146
) : PythonTreeNode(type) {
@@ -149,7 +149,7 @@ object PythonTree {
149149
type: PythonClassId,
150150
constructor: PythonClassId,
151151
args: List<PythonTreeNode>,
152-
) : this(id, type, constructor, args, emptyMap(), emptyList(), emptyMap())
152+
) : this(id, type, constructor, args, emptyMap<String, PythonTreeNode>().toMutableMap(), emptyList(), emptyMap())
153153

154154
override val children: List<PythonTreeNode>
155155
get() = args + state.values + listitems + dictitems.values + dictitems.keys + PythonTreeNode(constructor)
@@ -185,6 +185,13 @@ object PythonTree {
185185
}
186186
}
187187

188+
fun fromObject(): PrimitiveNode {
189+
return PrimitiveNode(
190+
PythonClassId("builtins.object"),
191+
"object"
192+
)
193+
}
194+
188195
fun fromNone(): PrimitiveNode {
189196
return PrimitiveNode(
190197
pythonNoneClassId,

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonCgCallableAccessManager.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.utbot.python.framework.codegen.model.constructor.tree
22

33
import org.utbot.framework.codegen.domain.context.CgContext
44
import org.utbot.framework.codegen.domain.context.CgContextOwner
5+
import org.utbot.framework.codegen.domain.models.CgAssignment
56
import org.utbot.framework.codegen.domain.models.CgConstructorCall
67
import org.utbot.framework.codegen.domain.models.CgExecutableCall
78
import org.utbot.framework.codegen.domain.models.CgExpression
@@ -18,8 +19,12 @@ import org.utbot.framework.plugin.api.FieldId
1819
import org.utbot.framework.plugin.api.MethodId
1920
import org.utbot.framework.plugin.api.util.exceptions
2021
import org.utbot.python.framework.api.python.PythonMethodId
22+
import org.utbot.python.framework.api.python.PythonTree
23+
import org.utbot.python.framework.api.python.PythonTreeModel
2124
import org.utbot.python.framework.api.python.util.pythonAnyClassId
25+
import org.utbot.python.framework.codegen.PythonCgLanguageAssistant
2226
import org.utbot.python.framework.codegen.model.constructor.util.importIfNeeded
27+
import org.utbot.python.framework.codegen.model.tree.CgPythonTree
2328

2429
class PythonCgCallableAccessManagerImpl(val context: CgContext) : CgCallableAccessManager,
2530
CgContextOwner by context {
@@ -46,7 +51,28 @@ class PythonCgCallableAccessManagerImpl(val context: CgContext) : CgCallableAcce
4651
}
4752

4853
override fun CgIncompleteMethodCall.invoke(vararg args: Any?): CgMethodCall {
49-
val resolvedArgs = args.resolve()
54+
val resolvedArgs = emptyList<CgExpression>().toMutableList()
55+
args.forEach { arg ->
56+
if (arg is CgPythonTree) {
57+
resolvedArgs.add(arg.value)
58+
arg.children.forEach { +it }
59+
} else {
60+
resolvedArgs.add(arg as CgExpression)
61+
}
62+
}
63+
// resolvedArgs.forEach {
64+
// if (it is CgPythonTree) {
65+
// it.children.forEach { child ->
66+
// if (child is CgAssignment) {
67+
// if (!existingVariableNames.contains(child.lValue.toString())) {
68+
// +child
69+
// }
70+
// } else {
71+
// +child
72+
// }
73+
// }
74+
// }
75+
// }
5076
val methodCall = CgMethodCall(caller, method, resolvedArgs)
5177
if (method is PythonMethodId)
5278
newMethodCall(method)

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonCgMethodConstructor.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import org.utbot.python.framework.codegen.PythonCgLanguageAssistant
2020
import org.utbot.python.framework.codegen.model.tree.*
2121

2222
class PythonCgMethodConstructor(context: CgContext) : CgMethodConstructor(context) {
23+
val maxDepth: Int = 5
24+
2325
override fun assertEquality(expected: CgValue, actual: CgVariable) {
2426
pythonDeepEquals(expected, actual)
2527
}
@@ -47,7 +49,7 @@ class PythonCgMethodConstructor(context: CgContext) : CgMethodConstructor(contex
4749
testMethod(testMethodName, execution.displayName) {
4850
val statics = currentExecution!!.stateBefore.statics
4951
rememberInitialStaticFields(statics)
50-
(context.cgLanguageAssistant as PythonCgLanguageAssistant).memoryObjects.clear()
52+
// (context.cgLanguageAssistant as PythonCgLanguageAssistant).memoryObjects.clear()
5153

5254
val modificationInfo = StateModificationInfo()
5355
val fieldStateManager = context.cgLanguageAssistant.getCgFieldStateManager(context)
@@ -186,6 +188,7 @@ class PythonCgMethodConstructor(context: CgContext) : CgMethodConstructor(contex
186188
require(expected is CgPythonTree) {
187189
"Expected value have to be CgPythonTree but `${expected::class}` found"
188190
}
191+
(context.cgLanguageAssistant as PythonCgLanguageAssistant).memoryObjects.clear()
189192
val expectedValue = pythonBuildObject(expected.tree)
190193
pythonDeepTreeEquals(expected.tree, expectedValue, actual)
191194
}
@@ -282,9 +285,10 @@ class PythonCgMethodConstructor(context: CgContext) : CgMethodConstructor(contex
282285
private fun pythonDeepTreeEquals(
283286
expectedNode: PythonTree.PythonTreeNode,
284287
expected: CgValue,
285-
actual: CgVariable
288+
actual: CgVariable,
289+
depth: Int = maxDepth
286290
) {
287-
if (expectedNode.comparable) {
291+
if (expectedNode.comparable || depth == 0) {
288292
emptyLineIfNeeded()
289293
testFrameworkManager.assertEquals(
290294
expected,
@@ -352,7 +356,7 @@ class PythonCgMethodConstructor(context: CgContext) : CgMethodConstructor(contex
352356
)
353357
)
354358
}
355-
pythonDeepTreeEquals(value, fieldExpected, fieldActual)
359+
pythonDeepTreeEquals(value, fieldExpected, fieldActual, depth - 1)
356360
}
357361
} else {
358362
emptyLineIfNeeded()

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonCgVariableConstructor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class PythonCgVariableConstructor(context_: CgContext) : CgVariableConstructor(c
5858
}
5959
}
6060

61-
fun pythonBuildObject2(objectNode: PythonTree.PythonTreeNode): Pair<CgValue, List<CgStatement>> {
61+
private fun pythonBuildObject2(objectNode: PythonTree.PythonTreeNode): Pair<CgValue, List<CgStatement>> {
6262
return when (objectNode) {
6363
is PythonTree.PrimitiveNode -> {
6464
Pair(CgLiteral(objectNode.type, objectNode.repr), emptyList())

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,9 @@ internal class CgPythonRenderer(
322322
renderMethodDocumentation(element)
323323
renderMethodSignature(element)
324324
visit(element as CgMethod)
325-
println("pass")
325+
withIndent {
326+
println("pass")
327+
}
326328
}
327329

328330
override fun renderMethodSignature(element: CgParameterizedTestDataProviderMethod) {
@@ -507,7 +509,7 @@ internal class CgPythonRenderer(
507509
}
508510

509511
override fun visit(element: CgPythonTree) {
510-
// element.children.forEach { it.accept(this) }
512+
element.children.forEach { it.accept(this) }
511513
element.value.accept(this)
512514
}
513515

utbot-python/src/main/kotlin/org/utbot/python/fuzzing/provider/ReduceValueProvider.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ class ReduceValueProvider(
2525
val meta = type.meta as PythonConcreteCompositeTypeDescription
2626
val arguments = (type.getPythonAttributes().first { it.name == "__init__" }.type as FunctionTypeCreator.Original).arguments
2727
val nonSelfArgs = arguments.drop(1)
28+
29+
val fields = type.getPythonAttributes().filter { attr -> attr.type.getPythonAttributes().all { it.name != "__call__" }}
30+
31+
val modifications = emptyList<Routine.Call<Type, PythonTreeModel>>().toMutableList()
32+
modifications.addAll(fields.map { field ->
33+
Routine.Call(listOf(field.type)) { instance, arguments ->
34+
val obj = instance.tree as PythonTree.ReduceNode
35+
obj.state[field.name] = arguments.first().tree
36+
}
37+
})
38+
2839
yield(Seed.Recursive(
2940
construct = Routine.Create(nonSelfArgs) { v ->
3041
PythonTreeModel(
@@ -37,7 +48,8 @@ class ReduceValueProvider(
3748
PythonClassId(meta.name.toString())
3849
)
3950
},
40-
empty = Routine.Empty { PythonTreeModel(PythonTree.fromNone(), PythonClassId(meta.name.toString())) }
51+
modify = modifications.asSequence(),
52+
empty = Routine.Empty { PythonTreeModel(PythonTree.fromObject(), PythonClassId(meta.name.toString())) }
4153
))
4254
}
4355
}

0 commit comments

Comments
 (0)