Skip to content

Notes on making `println` work

Piotr Krzemiński edited this page Mar 4, 2022 · 9 revisions

Code under test

fun main() {
    println("Hello world!")
}

Current state of Python backend

Currently getting this error:

Traceback (most recent call last):
  File "python/e2e-tests/out/compiled.py", line 13554, in <module>
    main()
  File "python/e2e-tests/out/compiled.py", line 13551, in main
    println('Hello world!')
  File "python/e2e-tests/out/compiled.py", line 8591, in println
    output.println_qi8yb4_k_(message)
AttributeError: 'NoneType' object has no attribute 'println_qi8yb4_k_'

Trying to go from the top of this stack trace:

if __name__ == "__main__":
    main()
def main():
    println('Hello world!')
def println(message):
    output.println_qi8yb4_k_(message)

The problem is that the top-level variable output is set to None:

output = None

Comparison with JS backend

  function main() {
    println('Hello world!');
  }
  function println(message) {
    output.println_qi8yb4_k$(message);
  }

'output' is assigned a value at the bottom of the whole file, but before calling main:

output = output$init$();
// ...
main();

Manual changes I did to make things work (not fully yet)

$ git diff
diff --git a/python/e2e-tests/out/compiled.py b/python/e2e-tests/out/compiled.py
index 5033b4a7e3b..0085c4ff426 100644
--- a/python/e2e-tests/out/compiled.py
+++ b/python/e2e-tests/out/compiled.py
@@ -8506,7 +8506,8 @@ class BufferedOutputToConsoleLog_0(BufferedOutput_0):
         BufferedOutput_0.__init__(self)
     
     def print_qi8yb4_k_(self, message):
-        s = kotlin_String(js('String')(message))
+        print("print")
+        s = message
         tmp0_nativeLastIndexOf_0 = s
         i = kotlin_Int(tmp0_nativeLastIndexOf_0.lastIndexOf('\n', 0))
         if i >= 0:
@@ -8536,6 +8537,8 @@ class BufferedOutputToConsoleLog_0(BufferedOutput_0):
         pass
     
     def println_qi8yb4_k_(self, message):
+        print("println")
+        self.print_qi8yb4_k_(message)
         pass
     
     def equals(self, other):
@@ -8588,11 +8591,13 @@ class BufferedOutput_0(BaseOutput):
     
 
 def println(message):
+    print("In println")
+    print(output)
+    print(output.println_qi8yb4_k_)
     output.println_qi8yb4_k_(message)
 
 def output_init_():
-    isNode_2 = kotlin_Boolean(js('typeof process !== \'undefined\' && process.versions && !!process.versions.node'))
-    return (NodeJsOutput_0(js('process.stdout'))) if (isNode_2) else (BufferedOutputToConsoleLog_0())
+    return BufferedOutputToConsoleLog_0()
 
 def _get_EmptyContinuation_():
     return EmptyContinuation
@@ -13551,4 +13556,5 @@ def main():
     println('Hello world!')
 
 if __name__ == "__main__":
+    output = output_init_()
     main()

Current problem:

Traceback (most recent call last):
  File "python/e2e-tests/out/compiled.py", line 13560, in <module>
    main()
  File "python/e2e-tests/out/compiled.py", line 13556, in main
    println('Hello world!')
  File "python/e2e-tests/out/compiled.py", line 8597, in println
    output.println_qi8yb4_k_(message)
  File "python/e2e-tests/out/compiled.py", line 8541, in println_qi8yb4_k_
    self.print_qi8yb4_k_(message)
  File "python/e2e-tests/out/compiled.py", line 8512, in print_qi8yb4_k_
    i = kotlin_Int(tmp0_nativeLastIndexOf_0.lastIndexOf('\n', 0))
NameError: name 'kotlin_Int' is not defined

We may not have this problem once we throw away JS-specific implementation of println, it comes from here: https://github.com/krzema12/kotlin-python/blob/python-backend/libraries/stdlib/js/src/kotlin/console.kt