taichi: IDLE still doesn't work for me

Describe the bug I saw this post today https://github.com/taichi-dev/taichi/pull/1222 and I realized this is already in 0.6.14 release, and I decided to try it out, but it still doesn’t work for me. To Reproduce the codes are these:

import taichi as ti

ti.init()

a = 1


@ti.kernel
def test_kernel_print():
    print(a)


test_kernel_print()

Log/Screenshots When I do it in IDLE concole, it still gives me this:

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    test_kernel_print()
  File "C:\Users\Eary\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\lang\kernel.py", line 559, in wrapped
    return primal(*args, **kwargs)
  File "C:\Users\Eary\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\lang\kernel.py", line 488, in __call__
    self.materialize(key=key, args=args, arg_features=arg_features)
  File "C:\Users\Eary\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\lang\kernel.py", line 300, in materialize
    src = remove_indent(oinspect.getsource(self.func))
  File "C:\Users\Eary\AppData\Local\Programs\Python\Python38\lib\inspect.py", line 985, in getsource
    lines, lnum = getsourcelines(object)
  File "C:\Users\Eary\AppData\Local\Programs\Python\Python38\lib\inspect.py", line 967, in getsourcelines
    lines, lnum = findsource(object)
  File "C:\Users\Eary\AppData\Local\Programs\Python\Python38\lib\inspect.py", line 798, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

When I try it in IDLE with a file, it kind of runs but it prints nothing.

[Taichi] mode=release
[Taichi] version 0.6.18, llvm 10.0.0, commit 35845482, python 3.8.1
[Taichi] Starting on arch=x64
>>> 

After reading the manual’s “warning” section at https://taichi.readthedocs.io/en/stable/debugging.html?highlight=idle#run-time-print-in-kernels , I tried to change the ti.init() to ti.init(arch=ti.opengl) but it doesn’t work.

[Taichi] mode=release
[Taichi] version 0.6.18, llvm 10.0.0, commit 35845482, python 3.8.1
[Taichi] Starting on arch=opengl
>>> 

Another thing, it works in PyCharm with ti.init() ti.init(arch=ti.cuda) etc., it will print a 1 for me. But when using ti.init(arch=ti.opengl), PyCharm also prints nothing at all.

I try it in Blender’s scripting workspace as well. (I assume this is related based on @yuanming-hu 's reply at https://github.com/taichi-dev/taichi_elements/issues/54#issuecomment-645720321) It gives me the same error in both the console and the file:

[Taichi] mode=release
[Taichi] version 0.6.18, llvm 10.0.0, commit 35845482, python 3.7.7
[Taichi] Starting on arch=x64
Traceback (most recent call last):
  File "\Text", line 13, in <module>
  File "C:\Users\Eary\Desktop\blender-2.90.0-taichi-branch\blender-2.90.0-4db63b648643-windows64\2.90\python\lib\site-packages\taichi\lang\kernel.py", line 559, in wrapped
    return primal(*args, **kwargs)
  File "C:\Users\Eary\Desktop\blender-2.90.0-taichi-branch\blender-2.90.0-4db63b648643-windows64\2.90\python\lib\site-packages\taichi\lang\kernel.py", line 488, in __call__
    self.materialize(key=key, args=args, arg_features=arg_features)
  File "C:\Users\Eary\Desktop\blender-2.90.0-taichi-branch\blender-2.90.0-4db63b648643-windows64\2.90\python\lib\site-packages\taichi\lang\kernel.py", line 300, in materialize
    src = remove_indent(oinspect.getsource(self.func))
  File "C:\Users\Eary\Desktop\blender-2.90.0-taichi-branch\blender-2.90.0-4db63b648643-windows64\2.90\python\lib\inspect.py", line 973, in getsource
    lines, lnum = getsourcelines(object)
  File "C:\Users\Eary\Desktop\blender-2.90.0-taichi-branch\blender-2.90.0-4db63b648643-windows64\2.90\python\lib\inspect.py", line 955, in getsourcelines
    lines, lnum = findsource(object)
  File "C:\Users\Eary\Desktop\blender-2.90.0-taichi-branch\blender-2.90.0-4db63b648643-windows64\2.90\python\lib\inspect.py", line 778, in findsource
    raise OSError('source code not available')
OSError: source code not available

Also I noticed Blender is still crashing when using ti.init(arch=opengl), so I guess that was the reason for https://github.com/taichi-dev/taichi_elements/issues/54 then?

About this issue

Most upvoted comments

Great! For the 1 and 4, that’s because of IDLE limitation, we can’t get source code in interactive mode. However, using IPython and Jupyter have no such problem. In fact, I used to make a PR #1308 to perform a dirty hack into IDLE to make Taichi work with it (succeed!). But I finally closed it since I believe people should be able to install IPython to replace IDLE for development. WDYT? Dirty hack or request people to install IPython?

https://github.com/taichi-dev/taichi/issues/1483#issuecomment-673519228 Yes, def func(): pass works And yes, Blender file mode also works

Fixed in sourceinspect==0.0.3.

waterwave.py

Thank for reporting, I’ll try to fix this first.

Yes, def func(): pass works And yes, Blender file mode also works

It seems that if we use def func(): pass (inlined) the issue will be gone? I know how to fix then. Also, does the Blender file mode (the window on the right) work on your end?

Oh! Sorry about my typo! Please remove the extra ') at the end of line. Also, do not left indent before __import__, it will not be executed unless __name__ == '__main__'.

For the IDLE error, please follow its instruction and add that ‘hack code’.

So I need to manually copy the line to the .py file? That sounds a bit troublesome… But I will try.

Hi, despite it’s now merged into master, the next version 0.6.26 of taichi not released on PyPI yet. It will be likely released in Wed. Sorry 😃

So we need to pip install another package, right?

Yes, please see https://github.com/taichi-dev/sourceinspect.git. The package sourceinspect will be automatically installed when installing taichi, thanks to pip’s dep resolving mechanism.

No sure how it works, do you mind explaining?

In fact, it’s really simple, here’s the simplified workflow: I wrote sourceinspect, providing exactly the same API as inspect (a Python builtin module). In Taichi, we perform import sourceinspect as inspect, now our source code inspection are redirected into sourceinspect. When no Blender detected, sourceinspect simply redirect invocations back to inspect, nothing changes. When Blender is detected, sourceinspect will begin to do its own mocks to inspect source code in Blender.

def get_blender_text_name(file):
    if file.startswith('/') and file.count('/') == 1:
        return file[1:]      # untitled blender file, "/Text"

    i = file.rfind('.blend/')
    if i != -1:
        return file[i + 7:]  # saved blender file, "hello.blend/Text"

    return None

Thanking blender devs 😕

Yes, the ti idle_hacker will save a backup of the old file, so no worry 😃 We may also have a ti idle_hacker -r command for easy revert.

A quick question here, will there be an option to revert back to the original IDLE in case some problems happen?

My thought is to use taichi in Blender’s scripting module, that’s all I want. It seems very troublesome to use IPython in Blender, so I would vote for dirty hack, if it does not have some serious drawbacks.

For the 2 and 3, could you try adding ti.sync() at the end of program?

Thank for reporting these bugs. IIUC this issue contains 5 issues:

  1. OSError in IDLE interactive mode
  2. print not working in IDLE file mode
  3. print on OpenGL not working in PyCharm
  4. OSError in Blender scripting module
  5. OpenGL crashes in Blender