-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix meta class call handler generation. (#2271)
In cases were the "self" parameter is not explicitly given in a class __init__ function we did crash because we assumed that at least one positional parameter is always defined. This is now fixed.
- Loading branch information
Showing
2 changed files
with
62 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
joern-cli/frontends/pysrc2cpg/src/test/scala/io/joern/pysrc2cpg/cpg/ClassCpgTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.joern.pysrc2cpg.cpg | ||
|
||
import io.joern.pysrc2cpg.PySrc2CpgFixture | ||
import io.shiftleft.semanticcpg.language._ | ||
|
||
class ClassCpgTests extends PySrc2CpgFixture(withOssDataflow = false) { | ||
"class meta call handler" should { | ||
"have no self parameter if self is explicit" in { | ||
val cpg = code("""class Foo: | ||
| def __init__(self, x): | ||
| pass | ||
|""".stripMargin) | ||
|
||
val handlerMethod = cpg.method.name("<metaClassCallHandler>").head | ||
handlerMethod.fullName shouldBe "Test0.py:<module>.Foo.<metaClassCallHandler>" | ||
handlerMethod.lineNumber shouldBe Some(2) | ||
|
||
handlerMethod.parameter.size shouldBe 1 | ||
val xParameter = handlerMethod.parameter.head | ||
xParameter.name shouldBe "x" | ||
|
||
} | ||
|
||
"have no self parameter if self is in varargs" in { | ||
val cpg = code("""class Foo: | ||
| def __init__(*x): | ||
| pass | ||
|""".stripMargin) | ||
|
||
val handlerMethod = cpg.method.name("<metaClassCallHandler>").head | ||
handlerMethod.fullName shouldBe "Test0.py:<module>.Foo.<metaClassCallHandler>" | ||
handlerMethod.lineNumber shouldBe Some(2) | ||
|
||
handlerMethod.parameter.size shouldBe 1 | ||
val xParameter = handlerMethod.parameter.head | ||
xParameter.name shouldBe "x" | ||
|
||
} | ||
} | ||
|
||
} |