Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Jul 25, 2020
1 parent 21317e3 commit 202c235
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 259 deletions.
144 changes: 0 additions & 144 deletions python/taichi/idle_hacker.py

This file was deleted.

99 changes: 0 additions & 99 deletions python/taichi/lang/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class ShellType:
IPYTHON = 'IPython TerminalInteractiveShell'
JUPYTER = 'IPython ZMQInteractiveShell'
IPYBASED = 'IPython Based Shell'
IDLE = 'Python IDLE shell'
SCRIPT = None


Expand Down Expand Up @@ -46,9 +45,6 @@ def get_shell_name(exclude_script=False):
if hasattr(__builtins__, '__IPYTHON__'):
return ShellType.IPYBASED

if 'idlelib' in sys.modules:
return ShellType.IDLE

try:
if getattr(sys, 'ps1', sys.flags.interactive):
return ShellType.NATIVE
Expand Down Expand Up @@ -78,10 +74,6 @@ def create_inspector(name):
# `IPython.core.oinspect` for "IPython advanced shell"
return IPythonInspectorWrapper()

elif name == ShellType.IDLE:
# `.tidle_xxx` for "Python IDLE shell"
return IDLEInspectorWrapper()

else:
raise RuntimeError(f'Shell type "{name}" not supported')

Expand All @@ -92,46 +84,12 @@ def __init__(self):

self.inspector = self.create_inspector(self.name)

if hasattr(self.inspector, 'startup_clean'):
self.inspector.startup_clean()

def try_reset_shell_type(self):
new_name = self.get_shell_name(exclude_script=True)
if self.name != new_name:
print(
f'[Taichi] Shell type changed from "{self.name}" to "{new_name}"'
)

self.name = new_name
self.inspector = self.create_inspector(self.name)

def _catch_forward(foo):
"""
If Taichi starts within IDLE file mode, and after that user moved to interactive mode,
then there will be an OSError, since it's switched from None to Python IDLE shell...
We have to reset the shell type and create a corresponding inspector at this moment.
"""
import functools

@functools.wraps(foo)
def wrapped(self, *args, **kwargs):
try:
return foo(self, *args, **kwargs)
except OSError:
self.try_reset_shell_type()
return foo(self, *args, **kwargs)

return wrapped

@_catch_forward
def getsource(self, o):
return self.inspector.getsource(o)

@_catch_forward
def getsourcelines(self, o):
return self.inspector.getsourcelines(o)

@_catch_forward
def getsourcefile(self, o):
return self.inspector.getsourcefile(o)

Expand All @@ -157,62 +115,5 @@ def getsourcefile(self, o):
return f'<IPython:{lineno}>'


class IDLEInspectorWrapper:
"""`inspect` module wrapper for IDLE / Blender scripting module"""

# Thanks to IDLE's lack of support with `inspect`,
# we have to use a dirty hack to support Taichi there.

def __init__(self):
self.idle_cache = {}
from taichi.idle_hacker import startup_clean
self.startup_clean = startup_clean

def getsource(self, o):
func_id = id(o)
if func_id in self.idle_cache:
return self.idle_cache[func_id]

from taichi.idle_hacker import read_ipc_file
src = read_ipc_file()

# If user added our 'hacker-code' correctly,
# then the content of `.tidle_xxx` should be:
#
# ===
# import taichi as ti
#
# ===
# @ti.kernel
# def func(): # x.find('def ') locate to here
# pass
#
# ===
# func()
#

func_name = o.__name__
for x in reversed(src.split('===')):
x = x.strip()
i = x.find('def ')
if i == -1:
continue
name = x[i + 4:].split(':', maxsplit=1)[0]
name = name.split('(', maxsplit=1)[0]
if name.strip() == func_name:
self.idle_cache[func_name] = x
return x
else:
raise NameError(f'Could not find source for {func_name}!')

def getsourcelines(self, o):
lineno = 2 # TODO: consider include lineno in .tidle_xxx?
lines = self.getsource(o).split('\n')
return lines, lineno

def getsourcefile(self, o):
return '<IDLE>'


oinspect = ShellInspectorWrapper()
# TODO: also detect print according to shell type
16 changes: 0 additions & 16 deletions python/taichi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,22 +968,6 @@ def debug(self, arguments: list = sys.argv[2:]):

runpy.run_path(args.filename, run_name='__main__')

@register
def idle_hacker(self, arguments: list = sys.argv[2:]):
"""Run idle hack code injector"""
parser = argparse.ArgumentParser(
prog='ti idle_hacker', description=f"{self.idle_hacker.__doc__}")
parser.add_argument(
'-r',
'--revert',
dest='revert',
action='store_true',
help='Revert the hack injection in case you meet troubles')
args = parser.parse_args(arguments)

from .idle_hacker import main
return main(revert=args.revert)

@register
def task(self, arguments: list = sys.argv[2:]):
"""Run a specific task"""
Expand Down

0 comments on commit 202c235

Please sign in to comment.