1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
# NOT_RPYTHON
"""
The 'sys' module.
"""
from _structseq import structseqtype, structseqfield, SimpleNamespace
import sys
import _imp
from __pypy__.os import _get_multiarch
def excepthook(exctype, value, traceback):
"""Handle an exception by displaying it with a traceback on sys.stderr."""
if not isinstance(value, BaseException):
sys.stderr.write("TypeError: print_exception(): Exception expected for "
"value, {} found\n".format(type(value).__name__))
return
# Flush stdout as well, both files may refer to the same file
try:
sys.stdout.flush()
except:
pass
try:
from traceback import TracebackException
from _colorize import can_colorize
limit = getattr(sys, 'tracebacklimit', None)
format_exc_only = False
if limit is not None:
# ok, this is bizarre, but, the meaning of sys.tracebacklimit is
# understood differently in the traceback module than in
# PyTraceBack_Print in CPython, see
# https://bugs.python.org/issue38197
# one is counting from the top, the other from the bottom of the
# stack. so reverse polarity here
if limit > 0:
if limit > sys.maxsize:
limit = sys.maxsize
limit = -limit
else:
# the limit is 0 or negative. PyTraceBack_Print does not print
# Traceback (most recent call last):
# because there is indeed no traceback.
# the traceback module don't care
traceback = None
limit = None
format_exc_only = True
tb_exc = TracebackException(
exctype,
value,
traceback,
limit=limit,
)
tb_exc._colorize = can_colorize()
if format_exc_only:
line_generator = tb_exc.format_exception_only()
else:
line_generator = tb_exc.format()
for line in line_generator:
print(line, file=sys.stderr, end="")
except BaseException as e:
if not excepthook_failsafe(exctype, value):
raise
def excepthook_failsafe(exctype, value):
# This version carefully tries to handle all bad cases (e.g. an
# ImportError looking for traceback.py), but may still raise.
# If it does, we get "Error calling sys.excepthook" from app_main.py.
try:
# first try to print the exception's class name
stderr = sys.stderr
stderr.write(str(getattr(exctype, '__name__', exctype)))
# then attempt to get the str() of the exception
try:
s = str(value)
except:
s = '<failure of str() on the exception instance>'
# then print it
if s:
stderr.write(': %s\n' % (s,))
else:
stderr.write('\n')
return True # successfully printed at least the class and value
except:
return False # got an exception again... ignore, report the original
def breakpointhook(*args, **kwargs):
"""This hook function is called by built-in breakpoint()."""
import importlib, os, warnings
hookname = os.getenv('PYTHONBREAKPOINT')
if hookname is None or len(hookname) == 0:
hookname = 'pdb.set_trace'
elif hookname == '0':
return None
modname, dot, funcname = hookname.rpartition('.')
if dot == '':
modname = 'builtins'
try:
module = importlib.import_module(modname)
hook = getattr(module, funcname)
except:
warnings.warn(
'Ignoring unimportable $PYTHONBREAKPOINT: "{}"'.format(hookname),
RuntimeWarning)
return None
return hook(*args, **kwargs)
def exit(exitcode=None):
"""Exit the interpreter by raising SystemExit(exitcode).
If the exitcode is omitted or None, it defaults to zero (i.e., success).
If the exitcode is numeric, it will be used as the system exit status.
If it is another kind of object, it will be printed and the system
exit status will be one (i.e., failure)."""
# note that we cannot simply use SystemExit(exitcode) here.
# in the default branch, we use "raise SystemExit, exitcode",
# which leads to an extra de-tupelizing
# in normalize_exception, which is exactly like CPython's.
if isinstance(exitcode, tuple):
raise SystemExit(*exitcode)
raise SystemExit(exitcode)
#import __builtin__
def callstats():
"""Not implemented."""
return None
copyright_str = """
Copyright 2003-2021 PyPy development team.
All Rights Reserved.
For further information, see <http://pypy.org>
Portions Copyright (c) 2001-2021 Python Software Foundation.
All Rights Reserved.
Portions Copyright (c) 2000 BeOpen.com.
All Rights Reserved.
Portions Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.
Portions Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
"""
# Keep synchronized with pypy.interpreter.app_main.sys_flags and
# pypy.module.cpyext._flags
# This is tested in test_app_main.py
class sysflags(metaclass=structseqtype):
name = "sys.flags"
debug = structseqfield(0)
inspect = structseqfield(1)
interactive = structseqfield(2)
optimize = structseqfield(3)
dont_write_bytecode = structseqfield(4)
no_user_site = structseqfield(5)
no_site = structseqfield(6)
ignore_environment = structseqfield(7)
verbose = structseqfield(8)
bytes_warning = structseqfield(9)
quiet = structseqfield(10)
hash_randomization = structseqfield(11)
isolated = structseqfield(12)
dev_mode = structseqfield(13)
utf8_mode = structseqfield(14)
warn_default_encoding = structseqfield(15)
int_max_str_digits = structseqfield(16)
safe_path = structseqfield(17)
# The real flags are set in app_main, which is not used in untranslated tests.
# Set reasonable defaults for testing, in particular set utf8_mode to 1
# no clue why some have to be a bool, but CPython has tests
# for that. Also see default_otions in app_main
null_sysflags = sysflags((0,)*13 + (False, 1, 0, -1, False))
null__xoptions = {}
# copied from version.py
def tuple2hex(ver):
levels = {'alpha': 0xA,
'beta': 0xB,
'candidate': 0xC,
'final': 0xF,
}
subver = ver[4]
if not (0 <= subver <= 9):
subver = 0
return (ver[0] << 24 |
ver[1] << 16 |
ver[2] << 8 |
levels[ver[3]] << 4 |
subver)
implementation_dict = {
'name': 'pypy',
'version': sys.pypy_version_info,
'hexversion': tuple2hex(sys.pypy_version_info),
'cache_tag': _imp.get_tag(),
}
multiarch = _get_multiarch()
if multiarch:
implementation_dict['_multiarch'] = multiarch
implementation = SimpleNamespace(**implementation_dict)
def sys_stdout():
import sys
try:
return sys.stdout
except AttributeError:
raise RuntimeError("lost sys.stdout")
def print_item_to(x, stream):
# give to write() an argument which is either a string or a unicode
# (and let it deals itself with unicode handling). The check "is
# unicode" should not use isinstance() at app-level, because that
# could be fooled by strange objects, so it is done at interp-level.
try:
stream.write(x)
except UnicodeEncodeError:
print_unencodable_to(x, stream)
def print_unencodable_to(x, stream):
encoding = stream.encoding
encoded = x.encode(encoding, 'backslashreplace')
buffer = getattr(stream, 'buffer', None)
if buffer is not None:
buffer.write(encoded)
else:
escaped = encoded.decode(encoding, 'strict')
stream.write(escaped)
def print_newline_to(stream):
stream.write("\n")
def displayhook(obj):
"""Print an object to sys.stdout and also save it in builtins._"""
import builtins
if obj is not None:
builtins._ = obj
# NB. this is slightly more complicated in CPython,
# see e.g. the difference with >>> print 5,; 8
print_item_to(repr(obj), sys_stdout())
print_newline_to(sys_stdout())
__displayhook__ = displayhook # this is exactly like in CPython
|