| 1 |
|
| 2 |
|
| 3 |
"""Main entry point into the PyPy interpreter. For a list of options, type |
| 4 |
|
| 5 |
pyinteractive.py --help |
| 6 |
|
| 7 |
""" |
| 8 |
|
| 9 |
import os |
| 10 |
import sys |
| 11 |
import time |
| 12 |
|
| 13 |
pypy_path = os.path.join(os.path.dirname(__file__), '..', '..') |
| 14 |
sys.path.insert(0, os.path.abspath(pypy_path)) |
| 15 |
|
| 16 |
from pypy.tool import option |
| 17 |
from pypy.interpreter import main, interactive, error, gateway |
| 18 |
from rpython.config.config import OptionDescription, BoolOption, StrOption |
| 19 |
from rpython.config.config import Config, to_optparse |
| 20 |
from pypy.config import pypyoption |
| 21 |
|
| 22 |
|
| 23 |
cmdline_optiondescr = OptionDescription("interactive", "the options of pyinteractive.py", [ |
| 24 |
BoolOption("verbose", "show verbose interpreter-level traceback", |
| 25 |
default=os.getenv("PYPY_TB"), cmdline="-v"), |
| 26 |
BoolOption("interactive", "inspect interactively after running script", |
| 27 |
default=False, cmdline="-i"), |
| 28 |
BoolOption("completer", "use readline commandline completer", |
| 29 |
default=False, cmdline="-C"), |
| 30 |
BoolOption("optimize", |
| 31 |
"skip assert statements and remove docstrings when importing modules" |
| 32 |
" (this is -OO in regular CPython)", |
| 33 |
default=False, cmdline="-O"), |
| 34 |
BoolOption("no_site_import", "do not 'import site' on initialization", |
| 35 |
default=False, cmdline="-S"), |
| 36 |
BoolOption("runmodule", |
| 37 |
"library module to be run as a script (terminates option list)", |
| 38 |
default=False, cmdline="-m"), |
| 39 |
BoolOption("runcommand", |
| 40 |
"program passed in as CMD (terminates option list)", |
| 41 |
default=False, cmdline="-c"), |
| 42 |
StrOption("warn", |
| 43 |
"warning control (arg is action:message:category:module:lineno)", |
| 44 |
default=None, cmdline="-W"), |
| 45 |
|
| 46 |
]) |
| 47 |
|
| 48 |
pypy_init = gateway.applevel(''' |
| 49 |
def pypy_init(import_site): |
| 50 |
if import_site: |
| 51 |
import os, sys |
| 52 |
_MACOSX = sys.platform == 'darwin' |
| 53 |
if _MACOSX: |
| 54 |
# __PYVENV_LAUNCHER__, used by CPython on macOS, should be ignored |
| 55 |
# since it (possibly) results in a wrong sys.prefix and |
| 56 |
# sys.exec_prefix (and consequently sys.path). |
| 57 |
old_pyvenv_launcher = os.environ.pop('__PYVENV_LAUNCHER__', None) |
| 58 |
try: |
| 59 |
import site |
| 60 |
except: |
| 61 |
import sys |
| 62 |
print("'import site' failed", file=sys.stderr) |
| 63 |
if _MACOSX and old_pyvenv_launcher: |
| 64 |
os.environ['__PYVENV_LAUNCHER__'] = old_pyvenv_launcher |
| 65 |
''').interphook('pypy_init') |
| 66 |
|
| 67 |
|
| 68 |
def set_compiler(option, opt, value, parser): |
| 69 |
from rpython.translator.platform import set_platform |
| 70 |
set_platform('host', value) |
| 71 |
|
| 72 |
def main_(argv=None): |
| 73 |
starttime = time.time() |
| 74 |
config, parser = option.get_standard_options() |
| 75 |
interactiveconfig = Config(cmdline_optiondescr) |
| 76 |
to_optparse(interactiveconfig, parser=parser) |
| 77 |
def set_family_of_options(option, opt, value, parser): |
| 78 |
from pypy.config.pypyoption import set_pypy_opt_level |
| 79 |
set_pypy_opt_level(config, value) |
| 80 |
parser.add_option( |
| 81 |
'--cc', type=str, action="callback", |
| 82 |
callback=set_compiler, |
| 83 |
help="Compiler to use for compiling generated C") |
| 84 |
parser.add_option( |
| 85 |
'--opt', type=str, action="callback", |
| 86 |
callback=set_family_of_options, |
| 87 |
help="Set the family of options based on -opt=0,1,2,jit...") |
| 88 |
args = option.process_options(parser, argv[1:]) |
| 89 |
if interactiveconfig.verbose: |
| 90 |
error.RECORD_INTERPLEVEL_TRACEBACK = True |
| 91 |
|
| 92 |
config.objspace.suggest(allworkingmodules=False) |
| 93 |
config.objspace.usemodules.struct = True |
| 94 |
if config.objspace.allworkingmodules: |
| 95 |
pypyoption.enable_allworkingmodules(config) |
| 96 |
if config.objspace.usemodules._continuation: |
| 97 |
config.translation.continuation = True |
| 98 |
if config.objspace.usemodules.thread: |
| 99 |
config.translation.thread = True |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
space = option.make_objspace(config) |
| 104 |
|
| 105 |
space._starttime = starttime |
| 106 |
space.setitem(space.sys.w_dict, space.wrap('executable'), |
| 107 |
space.wrap(argv[0])) |
| 108 |
|
| 109 |
if interactiveconfig.optimize: |
| 110 |
|
| 111 |
space.appexec([], """(): |
| 112 |
import sys |
| 113 |
flags = list(sys.flags) |
| 114 |
flags[3] = 2 |
| 115 |
sys.flags = type(sys.flags)(flags) |
| 116 |
__builtins__.__dict__['__debug__'] = False |
| 117 |
""") |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
executable = argv[0] |
| 122 |
space.appexec([space.wrap(executable)], """(executable): |
| 123 |
import sys |
| 124 |
sys.pypy_find_stdlib(executable) |
| 125 |
""") |
| 126 |
|
| 127 |
|
| 128 |
warn_arg = interactiveconfig.warn |
| 129 |
if warn_arg is not None: |
| 130 |
space.appexec([space.wrap(warn_arg)], """(arg): |
| 131 |
import sys |
| 132 |
sys.warnoptions.append(arg)""") |
| 133 |
|
| 134 |
w_path = space.sys.get('path') |
| 135 |
path = os.getenv('PYTHONPATH') |
| 136 |
if path: |
| 137 |
path = path.split(os.pathsep) |
| 138 |
else: |
| 139 |
path = [] |
| 140 |
path.insert(0, '') |
| 141 |
for i, dir in enumerate(path): |
| 142 |
space.call_method(w_path, 'insert', space.wrap(i), space.wrap(dir)) |
| 143 |
|
| 144 |
|
| 145 |
go_interactive = interactiveconfig.interactive |
| 146 |
banner = '' |
| 147 |
exit_status = 0 |
| 148 |
command = None |
| 149 |
if interactiveconfig.runcommand: |
| 150 |
command = args[0] |
| 151 |
args[0] = '-c' |
| 152 |
if interactiveconfig.runmodule: |
| 153 |
command = args.pop(0) |
| 154 |
for arg in args: |
| 155 |
space.call_method(space.sys.get('argv'), 'append', |
| 156 |
space.newfilename(arg)) |
| 157 |
|
| 158 |
|
| 159 |
if interactiveconfig.runcommand: |
| 160 |
def doit(): |
| 161 |
main.run_string(command, space=space) |
| 162 |
elif interactiveconfig.runmodule: |
| 163 |
def doit(): |
| 164 |
main.run_module(command, args, space=space) |
| 165 |
elif args: |
| 166 |
filename = args[0] |
| 167 |
if not os.path.isabs(filename): |
| 168 |
filename = os.path.join(os.getcwd(), filename) |
| 169 |
scriptdir = os.path.dirname(filename) |
| 170 |
space.call_method(space.sys.get('path'), 'insert', |
| 171 |
space.wrap(0), space.wrap(scriptdir)) |
| 172 |
def doit(): |
| 173 |
main.run_file(filename, space=space) |
| 174 |
else: |
| 175 |
def doit(): |
| 176 |
pass |
| 177 |
space.call_method(space.sys.get('argv'), 'append', space.wrap('')) |
| 178 |
go_interactive = 1 |
| 179 |
banner = None |
| 180 |
|
| 181 |
try: |
| 182 |
def do_start(): |
| 183 |
space.startup() |
| 184 |
pypy_init(space, space.wrap(not interactiveconfig.no_site_import)) |
| 185 |
if main.run_toplevel(space, do_start, |
| 186 |
verbose=interactiveconfig.verbose): |
| 187 |
|
| 188 |
if not main.run_toplevel(space, doit, |
| 189 |
verbose=interactiveconfig.verbose): |
| 190 |
exit_status = 1 |
| 191 |
|
| 192 |
|
| 193 |
if go_interactive or os.getenv('PYTHONINSPECT'): |
| 194 |
try: |
| 195 |
import readline |
| 196 |
except: |
| 197 |
pass |
| 198 |
con = interactive.PyPyConsole( |
| 199 |
space, verbose=interactiveconfig.verbose, |
| 200 |
completer=interactiveconfig.completer) |
| 201 |
if banner == '': |
| 202 |
banner = '%s / %s'%(con.__class__.__name__, |
| 203 |
repr(space)) |
| 204 |
con.interact(banner) |
| 205 |
exit_status = 0 |
| 206 |
finally: |
| 207 |
def doit(): |
| 208 |
space.finish() |
| 209 |
main.run_toplevel(space, doit, verbose=interactiveconfig.verbose) |
| 210 |
|
| 211 |
return exit_status |
| 212 |
|
| 213 |
|
| 214 |
if __name__ == '__main__': |
| 215 |
if hasattr(sys, 'setrecursionlimit'): |
| 216 |
|
| 217 |
sys.setrecursionlimit(3000) |
| 218 |
sys.exit(main_(sys.argv)) |