def set_stdio_encodings(ignore_environment): import os readenv = not ignore_environment io_encoding = readenv and os.getenv("PYTHONIOENCODING") if io_encoding: errors = None if ":" in io_encoding: io_encoding, errors = io_encoding.split(":", 1) set_io_encoding(io_encoding, io_encoding, errors, True) else: if IS_WINDOWS: import __pypy__ io_encoding, io_encoding_output = __pypy__.get_console_cp() else: io_encoding = io_encoding_output = sys.getfilesystemencoding() if io_encoding: set_io_encoding(io_encoding, io_encoding_output, None, False)
def run_command_line(interactive, inspect, run_command, no_site, run_module, run_stdin, warnoptions, unbuffered, ignore_environment, **ignored): # with PyPy in top of CPython we can only have around 100 # but we need more in the translated PyPy for the compiler package if '__pypy__' not in sys.builtin_module_names: sys.setrecursionlimit(5000) if unbuffered: set_unbuffered_io() elif not sys.stdout.isatty(): set_fully_buffered_io() mainmodule = type(sys)('__main__') sys.modules['__main__'] = mainmodule if not no_site: try: import site except: print >> sys.stderr, "'import site' failed" readenv = not ignore_environment io_encoding = readenv and os.getenv("PYTHONIOENCODING") if io_encoding: errors = None if ":" in io_encoding: io_encoding, errors = io_encoding.split(":", 1) set_io_encoding(io_encoding, io_encoding, errors, True) else: if IS_WINDOWS: import __pypy__ io_encoding, io_encoding_output = __pypy__.get_console_cp() else: io_encoding = io_encoding_output = sys.getfilesystemencoding() if io_encoding: set_io_encoding(io_encoding, io_encoding_output, None, False) pythonwarnings = readenv and os.getenv('PYTHONWARNINGS') if pythonwarnings: warnoptions.extend(pythonwarnings.split(',')) if warnoptions: sys.warnoptions[:] = warnoptions from warnings import _processoptions _processoptions(sys.warnoptions) # set up the Ctrl-C => KeyboardInterrupt signal handler, if the # signal module is available try: import signal except ImportError: pass else: signal.signal(signal.SIGINT, signal.default_int_handler) if hasattr(signal, "SIGPIPE"): signal.signal(signal.SIGPIPE, signal.SIG_IGN) if hasattr(signal, 'SIGXFZ'): signal.signal(signal.SIGXFZ, signal.SIG_IGN) if hasattr(signal, 'SIGXFSZ'): signal.signal(signal.SIGXFSZ, signal.SIG_IGN) def inspect_requested(): # We get an interactive prompt in one of the following three cases: # # * interactive=True, from the "-i" option # or # * inspect=True and stdin is a tty # or # * PYTHONINSPECT is set and stdin is a tty. # return (interactive or ((inspect or (readenv and os.getenv('PYTHONINSPECT'))) and sys.stdin.isatty())) success = True try: if run_command != 0: # handle the "-c" command # Put '' on sys.path sys.path.insert(0, '') def run_it(): exec run_command in mainmodule.__dict__ success = run_toplevel(run_it) elif run_module: # handle the "-m" command # '' on sys.path is required also here sys.path.insert(0, '') import runpy success = run_toplevel(runpy._run_module_as_main, sys.argv[0]) elif run_stdin: # handle the case where no command/filename/module is specified # on the command-line. # update sys.path *after* loading site.py, in case there is a # "site.py" file in the script's directory. Only run this if we're # executing the interactive prompt, if we're running a script we # put it's directory on sys.path sys.path.insert(0, '') if interactive or sys.stdin.isatty(): # If stdin is a tty or if "-i" is specified, we print # a banner and run $PYTHONSTARTUP. print_banner() python_startup = readenv and os.getenv('PYTHONSTARTUP') if python_startup: try: f = open(python_startup) startup = f.read() f.close() except IOError, e: print >> sys.stderr, "Could not open PYTHONSTARTUP" print >> sys.stderr, "IOError:", e else: def run_it(): co_python_startup = compile( startup, python_startup, 'exec') exec co_python_startup in mainmodule.__dict__ run_toplevel(run_it) # Then we need a prompt. inspect = True else: # If not interactive, just read and execute stdin normally. def run_it(): co_stdin = compile(sys.stdin.read(), '<stdin>', 'exec') exec co_stdin in mainmodule.__dict__ mainmodule.__file__ = '<stdin>' success = run_toplevel(run_it) else:
def run_command_line(interactive, inspect, run_command, no_site, run_module, run_stdin, warnoptions, unbuffered, ignore_environment, **ignored): # with PyPy in top of CPython we can only have around 100 # but we need more in the translated PyPy for the compiler package if '__pypy__' not in sys.builtin_module_names: sys.setrecursionlimit(5000) if unbuffered: set_unbuffered_io() elif not sys.stdout.isatty(): set_fully_buffered_io() mainmodule = type(sys)('__main__') sys.modules['__main__'] = mainmodule if not no_site: try: import site except: print >> sys.stderr, "'import site' failed" readenv = not ignore_environment io_encoding = readenv and os.getenv("PYTHONIOENCODING") if io_encoding: errors = None if ":" in io_encoding: io_encoding, errors = io_encoding.split(":", 1) set_io_encoding(io_encoding, io_encoding, errors, True) else: if IS_WINDOWS: import __pypy__ io_encoding, io_encoding_output = __pypy__.get_console_cp() else: io_encoding = io_encoding_output = sys.getfilesystemencoding() if io_encoding: set_io_encoding(io_encoding, io_encoding_output, None, False) pythonwarnings = readenv and os.getenv('PYTHONWARNINGS') if pythonwarnings: warnoptions.extend(pythonwarnings.split(',')) if warnoptions: sys.warnoptions[:] = warnoptions from warnings import _processoptions _processoptions(sys.warnoptions) # set up the Ctrl-C => KeyboardInterrupt signal handler, if the # signal module is available try: import signal except ImportError: pass else: signal.signal(signal.SIGINT, signal.default_int_handler) if hasattr(signal, "SIGPIPE"): signal.signal(signal.SIGPIPE, signal.SIG_IGN) if hasattr(signal, 'SIGXFZ'): signal.signal(signal.SIGXFZ, signal.SIG_IGN) if hasattr(signal, 'SIGXFSZ'): signal.signal(signal.SIGXFSZ, signal.SIG_IGN) def inspect_requested(): # We get an interactive prompt in one of the following three cases: # # * interactive=True, from the "-i" option # or # * inspect=True and stdin is a tty # or # * PYTHONINSPECT is set and stdin is a tty. # return (interactive or ((inspect or (readenv and os.getenv('PYTHONINSPECT'))) and sys.stdin.isatty())) success = True try: if run_command != 0: # handle the "-c" command # Put '' on sys.path sys.path.insert(0, '') def run_it(): exec run_command in mainmodule.__dict__ success = run_toplevel(run_it) elif run_module: # handle the "-m" command # '' on sys.path is required also here sys.path.insert(0, '') import runpy success = run_toplevel(runpy._run_module_as_main, sys.argv[0]) elif run_stdin: # handle the case where no command/filename/module is specified # on the command-line. # update sys.path *after* loading site.py, in case there is a # "site.py" file in the script's directory. Only run this if we're # executing the interactive prompt, if we're running a script we # put it's directory on sys.path sys.path.insert(0, '') if interactive or sys.stdin.isatty(): # If stdin is a tty or if "-i" is specified, we print # a banner and run $PYTHONSTARTUP. print_banner() python_startup = readenv and os.getenv('PYTHONSTARTUP') if python_startup: try: f = open(python_startup) startup = f.read() f.close() except IOError, e: print >> sys.stderr, "Could not open PYTHONSTARTUP" print >> sys.stderr, "IOError:", e else: def run_it(): co_python_startup = compile(startup, python_startup, 'exec') exec co_python_startup in mainmodule.__dict__ run_toplevel(run_it) # Then we need a prompt. inspect = True else: # If not interactive, just read and execute stdin normally. def run_it(): co_stdin = compile(sys.stdin.read(), '<stdin>', 'exec') exec co_stdin in mainmodule.__dict__ mainmodule.__file__ = '<stdin>' success = run_toplevel(run_it) else: