-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[IPython] Source inspection dispatcher for better IDLE compatibility #1222
Merged
Merged
Changes from 28 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
80fd302
[Misc] Use 'dill.source' of 'inspect' to run codes in interactive shell
archibate 6ee33d0
merge master
archibate 4da77a6
[skip ci] Add dill to CI
archibate d64c2c2
_ShellInspectorWrapper
archibate 3d629bf
Hack IDLE to make it happy
archibate c495d70
Fix IDLE
archibate 742ca63
[skip ci] cache: we do care user experience!!
archibate d36c58b
improve stability
archibate 17785f8
[skip ci] fix example exit
archibate 26011ee
[skip ci] Merge branch 'master' into dill
archibate d01c0ba
[skip ci] fix exit in IPython
archibate 67e5f0a
[skip ci] improve comment
archibate 2f18409
fix exec risk in ti debug (@rexwangcc)
archibate d28a6b3
[skip ci] Fix ti debug too verbose
archibate f43b482
Better line
archibate a74160c
fix test_cli
archibate 69e4eac
[skip ci] idle_hacker.py
archibate d57797f
fix typo and improve hacker
archibate 5fa29bf
[skip ci] reimprov
archibate 950ca17
[skip ci] Merge branch 'master' into dill
archibate 1726291
[skip ci] Merge branch 'master' into dill
archibate b62f1c0
improve idle inspector
archibate 280cd7b
[skip ci] add tag [IPython]
archibate 411af8b
[skip ci] Merge branch 'master' into dill
archibate 794bf05
revert idle
archibate 44a194d
[skip ci] revert off-topic cuda debug
archibate 4a5c37a
[skip ci] clean
archibate bfaeb84
[skip ci] enforce code format
taichi-gardener 343feca
[skip ci] no atexit
archibate 7b759f0
fix
archibate f12b8d9
[skip ci] Update python/taichi/lang/shell.py
archibate e4f1900
add dill to jenkins
yuanming-hu fcbba02
Merge branch 'master' into dill
yuanming-hu 0e685ad
Merge branch 'dill' of github.com:archibate/taichi into dill
archibate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
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,98 @@ | ||
import sys, os, atexit | ||
|
||
|
||
class ShellType: | ||
NATIVE = 'Python shell' | ||
IPYTHON = 'IPython TerminalInteractiveShell' | ||
JUPYTER = 'IPython ZMQInteractiveShell' | ||
IPYBASED = 'IPython Based Shell' | ||
SCRIPT = None | ||
|
||
|
||
def get_shell_name(): | ||
""" | ||
Detect which type of shell is using. | ||
Can be IPython, IDLE, Python native, or none. | ||
""" | ||
shell = os.environ.get('TI_SHELL_TYPE') | ||
if shell is not None: | ||
return getattr(ShellType, shell.upper()) | ||
Comment on lines
+17
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If they encounter any problem detecting shell type, they can still use a env var to force which to wrap. |
||
|
||
try: | ||
import __main__ as main | ||
if hasattr(main, '__file__'): # Called from a script? | ||
return ShellType.SCRIPT | ||
except: | ||
pass | ||
|
||
# Let's detect which type of interactive shell is being used. | ||
# As you can see, huge engineering efforts are done here just to | ||
# make IDLE and IPython happy, wish our user really love them :) | ||
archibate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
try: # IPython / Jupyter? | ||
return 'IPython ' + get_ipython().__class__.__name__ | ||
except: | ||
# Note that we can't simply do `'IPython' in sys.modules`, | ||
# since it seems `torch` will import IPython on it's own too.. | ||
if hasattr(__builtins__, '__IPYTHON__'): | ||
return ShellType.IPYBASED | ||
|
||
try: | ||
if getattr(sys, 'ps1', sys.flags.interactive): | ||
return ShellType.NATIVE | ||
except: | ||
pass | ||
|
||
return ShellType.SCRIPT | ||
|
||
|
||
class ShellInspectorWrapper: | ||
""" | ||
Wrapper of the `inspect` module. When interactive shell detected, | ||
we will redirect getsource() calls to the corresponding inspector | ||
provided by / suitable for each type of shell. | ||
""" | ||
def __init__(self): | ||
self.name = get_shell_name() | ||
|
||
if self.name is not None: | ||
print('[Taichi] Interactive shell detected:', self.name) | ||
|
||
if self.name is None: | ||
# `inspect` for "Python script" | ||
import inspect | ||
self.getsource = inspect.getsource | ||
self.getsourcelines = inspect.getsourcelines | ||
self.getsourcefile = inspect.getsourcefile | ||
|
||
elif self.name == ShellType.NATIVE: | ||
# `dill.source` for "Python native shell" | ||
import dill | ||
self.getsource = dill.source.getsource | ||
self.getsourcelines = dill.source.getsourcelines | ||
self.getsourcefile = dill.source.getsourcefile | ||
|
||
elif self.name.startswith('IPython'): | ||
# `IPython.core.oinspect` for "IPython advanced shell" | ||
def getsource(o): | ||
import IPython | ||
return IPython.core.oinspect.getsource(o) | ||
|
||
def getsourcelines(o): | ||
import IPython | ||
lineno = IPython.core.oinspect.find_source_lines(o) | ||
lines = IPython.core.oinspect.getsource(o).split('\n') | ||
return lines, lineno | ||
|
||
def getsourcefile(o): | ||
return '<IPython>' | ||
|
||
self.getsource = getsource | ||
self.getsourcelines = getsourcelines | ||
self.getsourcefile = getsourcefile | ||
|
||
else: | ||
raise RuntimeError(f'Shell type "{self.name}" not supported') | ||
|
||
|
||
oinspect = None |
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
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 |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
'colorama', | ||
'setuptools', | ||
'astor', | ||
'dill', | ||
# For testing: | ||
'pytest', | ||
'pytest-xdist', | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IPython seems doesn't like
exit
... Either usesys.exit
orbreak
instead.