diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/ExpressionHandler.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/ExpressionHandler.kt index 4494517246..3a76f13291 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/ExpressionHandler.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/ExpressionHandler.kt @@ -49,7 +49,13 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) : } private fun handleTuple(node: PythonAST.Tuple): Expression { - TODO() + val lst = mutableListOf() + for (e in node.elts) { + lst += handle(e) + } + val ile = newInitializerListExpression(rawNode = node) + ile.initializers = lst.toList() + return ile } private fun handleIfExp(node: PythonAST.IfExp): Expression { @@ -62,7 +68,18 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) : } private fun handleDict(node: PythonAST.Dict): Expression { - TODO() + val lst = mutableListOf() + for (i in node.values.indices) { + lst += + newKeyValueExpression( + key = node.keys[i]?.let { handle(it) }, + value = handle(node.values[i]), + rawNode = node + ) + } + val ile = newInitializerListExpression(rawNode = node) + ile.initializers = lst.toList() + return ile } private fun handleCompare(node: PythonAST.Compare): Expression { diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonAST.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonAST.kt index fb96e26aec..a61c5fe0be 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonAST.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonAST.kt @@ -554,7 +554,7 @@ interface PythonAST { * ``` */ class Dict(pyObject: PyObject) : ExprBase(pyObject) { - val keys: List by lazy { "keys" of pyObject } + val keys: List by lazy { "keys" of pyObject } val values: List by lazy { "values" of pyObject } } diff --git a/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt b/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt index 9d9aed8341..fd85b15911 100644 --- a/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt +++ b/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt @@ -908,9 +908,8 @@ class PythonFrontendTest : BaseTest() { assertLocalName("minor", ifCond.lhs as? Reference) // phr = {"user_id": user_id} | content - val phrDeclaration = - (ifThen.statements[0] as? DeclarationStatement)?.declarations?.get(0) - as? VariableDeclaration + val phrDeclaration = (ifThen.statements[0] as? AssignExpression)?.declarations?.get(0) + assertNotNull(phrDeclaration) assertLocalName("phr", phrDeclaration) val phrInitializer = phrDeclaration.firstAssignment as? BinaryOperator @@ -919,9 +918,7 @@ class PythonFrontendTest : BaseTest() { assertEquals(true, phrInitializer.lhs is InitializerListExpression) // z = {"user_id": user_id} - val elseStmt1 = - (ifElse.statements[0] as? DeclarationStatement)?.declarations?.get(0) - as? VariableDeclaration + val elseStmt1 = (ifElse.statements[0] as? AssignExpression)?.declarations?.get(0) assertNotNull(elseStmt1) assertLocalName("z", elseStmt1)