Skip to content

Commit 10ce7b6

Browse files
committed
Python, Construct: add missing imports of KaitaiStream
Fixes tests: - Python: - ExprBytesNonLiteral - Construct: - EnumOfValueInst - ExprBytesNonLiteral - ExprBytesOps - ExprEnum
1 parent 128e179 commit 10ce7b6

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ class PythonTranslator(provider: TypeProvider, importList: ImportList) extends B
5858

5959
override def doEnumByLabel(enumTypeAbs: List[String], label: String): String =
6060
s"${PythonCompiler.types2class(enumTypeAbs)}.$label"
61-
override def doEnumById(enumTypeAbs: List[String], id: String): String =
61+
override def doEnumById(enumTypeAbs: List[String], id: String): String = {
62+
importList.add(s"from kaitaistruct import ${PythonCompiler.kstreamName}")
63+
6264
s"${PythonCompiler.kstreamName}.resolve_enum(${PythonCompiler.types2class(enumTypeAbs)}, $id)"
65+
}
6366

6467
override def booleanOp(op: Ast.boolop) = op match {
6568
case Ast.boolop.Or => "or"
@@ -98,16 +101,25 @@ class PythonTranslator(provider: TypeProvider, importList: ImportList) extends B
98101

99102
override def bytesLength(value: Ast.expr): String =
100103
s"len(${translate(value)})"
101-
override def bytesSubscript(container: Ast.expr, idx: Ast.expr): String =
104+
override def bytesSubscript(container: Ast.expr, idx: Ast.expr): String = {
105+
importList.add(s"from kaitaistruct import ${PythonCompiler.kstreamName}")
106+
102107
s"${PythonCompiler.kstreamName}.byte_array_index(${translate(container)}, ${translate(idx)})"
108+
}
103109
override def bytesFirst(a: Ast.expr): String =
104110
bytesSubscript(a, Ast.expr.IntNum(0))
105111
override def bytesLast(a: Ast.expr): String =
106112
bytesSubscript(a, Ast.expr.IntNum(-1))
107-
override def bytesMin(b: Ast.expr): String =
113+
override def bytesMin(b: Ast.expr): String = {
114+
importList.add(s"from kaitaistruct import ${PythonCompiler.kstreamName}")
115+
108116
s"${PythonCompiler.kstreamName}.byte_array_min(${translate(b)})"
109-
override def bytesMax(b: Ast.expr): String =
117+
}
118+
override def bytesMax(b: Ast.expr): String = {
119+
importList.add(s"from kaitaistruct import ${PythonCompiler.kstreamName}")
120+
110121
s"${PythonCompiler.kstreamName}.byte_array_max(${translate(b)})"
122+
}
111123

112124

113125
override def strLength(value: Ast.expr): String =

0 commit comments

Comments
 (0)