-
Notifications
You must be signed in to change notification settings - Fork 188
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
Add bin/python that mimics Python CLI #562
Closed
Closed
Changes from all commits
Commits
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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
deps/deps.jl | ||
deps/PYTHON | ||
deps/build.log | ||
*.pyc |
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,28 @@ | ||
#!/usr/bin/env julia | ||
|
||
description = """ | ||
Python interpreter inside a Julia process. | ||
|
||
This command line interface mimics a basic subset of Python program so that | ||
Python program involving calls to Julia functions can be run without setting | ||
up PyJulia (which is currently hard for some platforms). | ||
|
||
Although this script has -i option and it can do a basic REPL, contrl-c may | ||
crash the whole process. Consider using IPython >= 7 which can be launched by: | ||
|
||
python -m IPython | ||
""" | ||
|
||
using PyCall | ||
|
||
# Initialize julia.Julia once so that subsequent calls of julia.Julia() | ||
# uses pre-configured DLL. | ||
pyimport("julia")[:Julia](init_julia=false) | ||
|
||
pushfirst!(PyVector(pyimport("sys")["path"]), | ||
joinpath(dirname(dirname(pathof(PyCall))), "lib")) | ||
|
||
code = pyimport("pseudo_python_cli")[:main](ARGS, description) | ||
if code isa Integer | ||
exit(code) | ||
end |
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,132 @@ | ||
''' | ||
Pseudo Python command line interface. | ||
|
||
It tries to mimic a subset of Python CLI: | ||
https://docs.python.org/3/using/cmdline.html | ||
''' | ||
|
||
import argparse | ||
import code | ||
import runpy | ||
import sys | ||
import traceback | ||
|
||
|
||
def python(module, command, script, script_args, interactive): | ||
if command: | ||
sys.argv[0] = "-c" | ||
elif script: | ||
sys.argv[0] = script | ||
sys.argv[1:] = script_args | ||
|
||
banner = "" | ||
try: | ||
if command: | ||
scope = {} | ||
exec(command, scope) | ||
elif module: | ||
scope = runpy.run_module( | ||
module, | ||
run_name="__main__", | ||
alter_sys=True) | ||
elif script == "-": | ||
source = sys.stdin.read() | ||
exec(compile(source, "<stdin>", "exec"), scope) | ||
elif script: | ||
scope = runpy.run_path( | ||
script, | ||
run_name="__main__") | ||
else: | ||
interactive = True | ||
scope = None | ||
banner = None # show banner | ||
except Exception: | ||
if not interactive: | ||
raise | ||
traceback.print_exc() | ||
|
||
if interactive: | ||
code.interact(banner=banner, local=scope) | ||
|
||
|
||
class CustomFormatter(argparse.RawDescriptionHelpFormatter, | ||
argparse.ArgumentDefaultsHelpFormatter): | ||
pass | ||
|
||
|
||
def make_parser(description=__doc__): | ||
parser = argparse.ArgumentParser( | ||
prog=None if sys.argv[0] else "python", | ||
usage="%(prog)s [option] ... [-c cmd | -m mod | script | -] [args]", | ||
formatter_class=CustomFormatter, | ||
description=description) | ||
|
||
parser.add_argument( | ||
"-i", dest="interactive", action="store_true", | ||
help=''' | ||
inspect interactively after running script. | ||
''') | ||
|
||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument( | ||
"-c", dest="command", | ||
help=''' | ||
Execute the Python code in COMMAND. | ||
''') | ||
group.add_argument( | ||
"-m", dest="module", | ||
help=''' | ||
Search sys.path for the named MODULE and execute its contents | ||
as the __main__ module. | ||
''') | ||
|
||
parser.add_argument( | ||
"script", nargs="?", | ||
help="path to file") | ||
|
||
return parser | ||
|
||
|
||
def split_args(args): | ||
''' | ||
Split arguments to Python and `sys.argv[1:]`. | ||
''' | ||
it = iter(args) | ||
py_args = [] | ||
for a in it: | ||
if a in ("-c", "-m"): | ||
py_args.append(a) | ||
try: | ||
a = next(it) | ||
except StopIteration: | ||
break | ||
py_args.append(a) | ||
break | ||
elif a == "-": | ||
py_args.append(a) | ||
break | ||
elif a.startswith("-"): | ||
py_args.append(a) | ||
else: # script | ||
py_args.append(a) | ||
break | ||
return py_args, list(it) | ||
|
||
|
||
def main(args=None, description=__doc__): | ||
if args is None: | ||
args = sys.argv[1:] | ||
parser = make_parser(description=description) | ||
py_args, script_args = split_args(args) | ||
try: | ||
ns = parser.parse_args(py_args) | ||
python(script_args=script_args, **vars(ns)) | ||
except SystemExit as err: | ||
return err.code | ||
except Exception: | ||
traceback.print_exc() | ||
return 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
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.
Can't this be done in Julia?