| 1 |
|
| 2 |
import codecs |
| 3 |
import pytest |
| 4 |
import sys |
| 5 |
|
| 6 |
def test_stdin_exists(space): |
| 7 |
space.sys.get('stdin') |
| 8 |
space.sys.get('__stdin__') |
| 9 |
|
| 10 |
def test_stdout_exists(space): |
| 11 |
space.sys.get('stdout') |
| 12 |
space.sys.get('__stdout__') |
| 13 |
|
| 14 |
def test_stdout_flush_at_shutdown(space): |
| 15 |
w_sys = space.sys |
| 16 |
w_read = space.appexec([], """(): |
| 17 |
import sys |
| 18 |
from io import BytesIO, TextIOWrapper |
| 19 |
class BadWrite(BytesIO): |
| 20 |
def write(self, data): |
| 21 |
raise IOError |
| 22 |
buf = BytesIO() |
| 23 |
def read(): |
| 24 |
buf.seek(0) |
| 25 |
return buf.read() |
| 26 |
sys.stdout = TextIOWrapper(BadWrite()) |
| 27 |
sys.stderr = TextIOWrapper(buf) |
| 28 |
return read""") |
| 29 |
|
| 30 |
try: |
| 31 |
space.call_method(w_sys.get('stdout'), 'write', space.wrap('x')) |
| 32 |
|
| 33 |
w_sys.flush_std_files(space) |
| 34 |
|
| 35 |
msg = space.bytes_w(space.call_function(w_read)) |
| 36 |
assert 'Exception ignored in:' in msg and '\nOSError' in msg |
| 37 |
finally: |
| 38 |
space.setattr(w_sys, space.wrap('stdout'), w_sys.get('__stdout__')) |
| 39 |
space.setattr(w_sys, space.wrap('stderr'), w_sys.get('__stderr__')) |
| 40 |
|
| 41 |
def test_stdio_missing_at_shutdown(space): |
| 42 |
w_sys = space.sys |
| 43 |
|
| 44 |
try: |
| 45 |
space.delattr(w_sys, space.wrap('stdout')) |
| 46 |
space.delattr(w_sys, space.wrap('stderr')) |
| 47 |
|
| 48 |
w_sys.flush_std_files(space) |
| 49 |
finally: |
| 50 |
space.setattr(w_sys, space.wrap('stdout'), w_sys.get('__stdout__')) |
| 51 |
space.setattr(w_sys, space.wrap('stderr'), w_sys.get('__stderr__')) |
| 52 |
|
| 53 |
class AppTestAppSysTests: |
| 54 |
spaceconfig = { |
| 55 |
"usemodules": ["thread"], |
| 56 |
} |
| 57 |
|
| 58 |
def setup_class(cls): |
| 59 |
cls.w_appdirect = cls.space.wrap(cls.runappdirect) |
| 60 |
|
| 61 |
def test_sys_in_modules(self): |
| 62 |
import sys |
| 63 |
modules = sys.modules |
| 64 |
assert 'sys' in modules, ( "An entry for sys " |
| 65 |
"is not in sys.modules.") |
| 66 |
sys2 = sys.modules['sys'] |
| 67 |
assert sys is sys2, "import sys is not sys.modules[sys]." |
| 68 |
def test_builtin_in_modules(self): |
| 69 |
import sys |
| 70 |
modules = sys.modules |
| 71 |
assert 'builtins' in modules, ( "An entry for builtins " |
| 72 |
"is not in sys.modules.") |
| 73 |
import builtins |
| 74 |
builtin2 = sys.modules['builtins'] |
| 75 |
assert builtins is builtin2, ( "import builtins " |
| 76 |
"is not sys.modules[builtins].") |
| 77 |
|
| 78 |
def test_builtin_module_names(self): |
| 79 |
import sys |
| 80 |
names = sys.builtin_module_names |
| 81 |
assert 'sys' in names, ( |
| 82 |
"sys is not listed as a builtin module.") |
| 83 |
assert 'builtins' in names, ( |
| 84 |
"builtins is not listed as a builtin module.") |
| 85 |
assert 'exceptions' not in names, ( |
| 86 |
"exceptions module shouldn't exist") |
| 87 |
|
| 88 |
def test_sys_exc_info(self): |
| 89 |
try: |
| 90 |
raise Exception |
| 91 |
except Exception as exc: |
| 92 |
e = exc |
| 93 |
import sys |
| 94 |
exc_type,exc_val,tb = sys.exc_info() |
| 95 |
try: |
| 96 |
raise Exception |
| 97 |
except Exception as exc: |
| 98 |
e2 = exc |
| 99 |
exc_type2,exc_val2,tb2 = sys.exc_info() |
| 100 |
assert exc_type ==Exception |
| 101 |
assert exc_val ==e |
| 102 |
assert exc_type2 ==Exception |
| 103 |
assert exc_val2 ==e2 |
| 104 |
assert tb2.tb_lineno - tb.tb_lineno == 6 |
| 105 |
|
| 106 |
def test_exc_info_normalization(self): |
| 107 |
import sys |
| 108 |
try: |
| 109 |
1/0 |
| 110 |
except ZeroDivisionError: |
| 111 |
etype, val, tb = sys.exc_info() |
| 112 |
assert isinstance(val, etype) |
| 113 |
else: |
| 114 |
raise AssertionError("ZeroDivisionError not caught") |
| 115 |
|
| 116 |
def test_io(self): |
| 117 |
import sys, io |
| 118 |
assert isinstance(sys.__stdout__, io.IOBase) |
| 119 |
assert isinstance(sys.__stderr__, io.IOBase) |
| 120 |
assert isinstance(sys.__stdin__, io.IOBase) |
| 121 |
assert sys.__stderr__.errors == 'backslashreplace' |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
if self.appdirect and not isinstance(sys.stdin, io.IOBase): |
| 128 |
return |
| 129 |
|
| 130 |
assert isinstance(sys.stdout, io.IOBase) |
| 131 |
assert isinstance(sys.stderr, io.IOBase) |
| 132 |
assert isinstance(sys.stdin, io.IOBase) |
| 133 |
assert sys.stderr.errors == 'backslashreplace' |
| 134 |
|
| 135 |
def test_getfilesystemencoding(self): |
| 136 |
import sys |
| 137 |
enc = sys.getfilesystemencoding() |
| 138 |
|
| 139 |
assert enc == "utf-8" |
| 140 |
|
| 141 |
def test_float_info(self): |
| 142 |
import sys |
| 143 |
fi = sys.float_info |
| 144 |
assert isinstance(fi.epsilon, float) |
| 145 |
assert isinstance(fi.dig, int) |
| 146 |
assert isinstance(fi.mant_dig, int) |
| 147 |
assert isinstance(fi.max, float) |
| 148 |
assert isinstance(fi.max_exp, int) |
| 149 |
assert isinstance(fi.max_10_exp, int) |
| 150 |
assert isinstance(fi.min, float) |
| 151 |
assert isinstance(fi.min_exp, int) |
| 152 |
assert isinstance(fi.min_10_exp, int) |
| 153 |
assert isinstance(fi.radix, int) |
| 154 |
assert isinstance(fi.rounds, int) |
| 155 |
|
| 156 |
def test_int_info(self): |
| 157 |
import sys |
| 158 |
li = sys.int_info |
| 159 |
assert isinstance(li.bits_per_digit, int) |
| 160 |
assert isinstance(li.sizeof_digit, int) |
| 161 |
|
| 162 |
def test_sys_exit(self): |
| 163 |
import sys |
| 164 |
exc = raises(SystemExit, sys.exit) |
| 165 |
assert exc.value.code is None |
| 166 |
|
| 167 |
exc = raises(SystemExit, sys.exit, 0) |
| 168 |
assert exc.value.code == 0 |
| 169 |
|
| 170 |
exc = raises(SystemExit, sys.exit, 1) |
| 171 |
assert exc.value.code == 1 |
| 172 |
|
| 173 |
exc = raises(SystemExit, sys.exit, (1, 2, 3)) |
| 174 |
assert exc.value.code == (1, 2, 3) |
| 175 |
|
| 176 |
def test_hash_info(self): |
| 177 |
import sys |
| 178 |
li = sys.hash_info |
| 179 |
assert isinstance(li.width, int) |
| 180 |
assert isinstance(li.modulus, int) |
| 181 |
assert isinstance(li.inf, int) |
| 182 |
assert isinstance(li.nan, int) |
| 183 |
assert isinstance(li.imag, int) |
| 184 |
assert isinstance(li.algorithm, str) |
| 185 |
assert str(li).startswith('sys.hash_info(') |
| 186 |
|
| 187 |
def test_sys_flags(self): |
| 188 |
import sys |
| 189 |
|
| 190 |
assert sys.flags.optimize is not None |
| 191 |
|
| 192 |
exc = raises(AttributeError, 'sys.flags.optimize = 3') |
| 193 |
assert 'readonly' in str(exc.value) |
| 194 |
raises(AttributeError, 'sys.flags.not_a_sys_flag = 2') |
| 195 |
|
| 196 |
def test_sys_flags_warn_default_encoding(self): |
| 197 |
import sys |
| 198 |
sys.flags.warn_default_encoding |
| 199 |
|
| 200 |
def test_sys_exit(self): |
| 201 |
import sys |
| 202 |
exc = raises(SystemExit, sys.exit) |
| 203 |
assert exc.value.code is None |
| 204 |
|
| 205 |
exc = raises(SystemExit, sys.exit, 0) |
| 206 |
assert exc.value.code == 0 |
| 207 |
|
| 208 |
exc = raises(SystemExit, sys.exit, 1) |
| 209 |
assert exc.value.code == 1 |
| 210 |
|
| 211 |
exc = raises(SystemExit, sys.exit, (1, 2, 3)) |
| 212 |
assert exc.value.code == (1, 2, 3) |
| 213 |
|
| 214 |
def test_sys_thread_info(self): |
| 215 |
import sys |
| 216 |
info = sys.thread_info |
| 217 |
assert isinstance(info.name, str) |
| 218 |
assert isinstance(info.lock, (str, type(None))) |
| 219 |
assert isinstance(info.version, (str, type(None))) |
| 220 |
|
| 221 |
def test_sys_flags_dev_mode_is_bool(self): |
| 222 |
import sys |
| 223 |
assert type(sys.flags.dev_mode) is bool |
| 224 |
|
| 225 |
def test_multiarch(self): |
| 226 |
import sys |
| 227 |
multiarch = '' |
| 228 |
try: |
| 229 |
multiarch = sys.implementation._multiarch |
| 230 |
except AttributeError: |
| 231 |
assert sys.platform in ('win32',) |
| 232 |
else: |
| 233 |
assert 'linux' in multiarch or 'darwin' in multiarch |
| 234 |
|
| 235 |
def test_audit(self): |
| 236 |
import sys |
| 237 |
sys.audit("os.chdir", "bla", 1, 2, 12) |
| 238 |
|
| 239 |
@pytest.mark.skipif(sys.platform=="win32", reason="platlibdir is posix only") |
| 240 |
def test_platlibdir(self): |
| 241 |
if self.appdirect: |
| 242 |
skip("only relevant for untranslated tests") |
| 243 |
import sys |
| 244 |
assert sys.platlibdir == "lib" |
| 245 |
|
| 246 |
|
| 247 |
@pytest.mark.skipif(sys.platform=="win32", reason="platlibdir is posix only") |
| 248 |
class AppTestPlatlibdirNotDefault: |
| 249 |
spaceconfig = { |
| 250 |
"platlibdir": "lib64" |
| 251 |
} |
| 252 |
def setup_class(cls): |
| 253 |
cls.w_appdirect = cls.space.wrap(cls.runappdirect) |
| 254 |
|
| 255 |
def test_platlibdir(self): |
| 256 |
if self.appdirect: |
| 257 |
skip("only relevant for untranslated tests") |
| 258 |
import sys |
| 259 |
assert sys.platlibdir == "lib64" |
| 260 |
|
| 261 |
|
| 262 |
class AppTestSysModulePortedFromCPython: |
| 263 |
spaceconfig = { |
| 264 |
"usemodules": ["struct"], |
| 265 |
} |
| 266 |
|
| 267 |
def setup_class(cls): |
| 268 |
cls.w_appdirect = cls.space.wrap(cls.runappdirect) |
| 269 |
|
| 270 |
def test_original_displayhook(self): |
| 271 |
import sys, _io, builtins |
| 272 |
savestdout = sys.stdout |
| 273 |
out = _io.StringIO() |
| 274 |
sys.stdout = out |
| 275 |
|
| 276 |
dh = sys.__displayhook__ |
| 277 |
|
| 278 |
raises(TypeError, dh) |
| 279 |
if hasattr(builtins, "_"): |
| 280 |
del builtins._ |
| 281 |
|
| 282 |
dh(None) |
| 283 |
assert out.getvalue() == "" |
| 284 |
assert not hasattr(builtins, "_") |
| 285 |
dh("hello") |
| 286 |
assert out.getvalue() == "'hello'\n" |
| 287 |
assert builtins._ == "hello" |
| 288 |
|
| 289 |
del sys.stdout |
| 290 |
raises(RuntimeError, dh, 42) |
| 291 |
|
| 292 |
sys.stdout = savestdout |
| 293 |
|
| 294 |
def test_original_displayhook_unencodable(self): |
| 295 |
import sys, _io |
| 296 |
out = _io.BytesIO() |
| 297 |
savestdout = sys.stdout |
| 298 |
sys.stdout = _io.TextIOWrapper(out, encoding='ascii') |
| 299 |
|
| 300 |
sys.__displayhook__("a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF") |
| 301 |
assert (out.getvalue() == |
| 302 |
b"'a=\\xe9 b=\\udc80 c=\\U00010000 d=\\U0010ffff'") |
| 303 |
|
| 304 |
sys.stdout = savestdout |
| 305 |
|
| 306 |
def test_lost_displayhook(self): |
| 307 |
import sys |
| 308 |
olddisplayhook = sys.displayhook |
| 309 |
del sys.displayhook |
| 310 |
code = compile("42", "<string>", "single") |
| 311 |
raises(RuntimeError, eval, code) |
| 312 |
sys.displayhook = olddisplayhook |
| 313 |
|
| 314 |
def test_custom_displayhook(self): |
| 315 |
import sys |
| 316 |
olddisplayhook = sys.displayhook |
| 317 |
def baddisplayhook(obj): |
| 318 |
raise ValueError |
| 319 |
sys.displayhook = baddisplayhook |
| 320 |
code = compile("42", "<string>", "single") |
| 321 |
raises(ValueError, eval, code) |
| 322 |
sys.displayhook = olddisplayhook |
| 323 |
|
| 324 |
def test_original_excepthook(self): |
| 325 |
import sys, _io |
| 326 |
savestderr = sys.stderr |
| 327 |
err = _io.StringIO() |
| 328 |
sys.stderr = err |
| 329 |
|
| 330 |
eh = sys.__excepthook__ |
| 331 |
|
| 332 |
raises(TypeError, eh) |
| 333 |
try: |
| 334 |
raise ValueError(42) |
| 335 |
except ValueError as exc: |
| 336 |
eh(*sys.exc_info()) |
| 337 |
assert err.getvalue().endswith("ValueError: 42\n") |
| 338 |
|
| 339 |
eh(1, '1', 1) |
| 340 |
expected = ("TypeError: print_exception(): Exception expected for " |
| 341 |
"value, str found") |
| 342 |
assert expected in err.getvalue() |
| 343 |
|
| 344 |
sys.stderr = savestderr |
| 345 |
|
| 346 |
def test_excepthook_failsafe_path(self): |
| 347 |
import traceback |
| 348 |
original_print_exception = traceback.print_exception |
| 349 |
import sys, _io |
| 350 |
savestderr = sys.stderr |
| 351 |
err = _io.StringIO() |
| 352 |
sys.stderr = err |
| 353 |
try: |
| 354 |
traceback.print_exception = "foo" |
| 355 |
eh = sys.__excepthook__ |
| 356 |
try: |
| 357 |
raise ValueError(42) |
| 358 |
except ValueError as exc: |
| 359 |
eh(*sys.exc_info()) |
| 360 |
finally: |
| 361 |
traceback.print_exception = original_print_exception |
| 362 |
sys.stderr = savestderr |
| 363 |
|
| 364 |
assert "ValueError: 42\n" in err.getvalue() |
| 365 |
|
| 366 |
def test_original_excepthook_pypy_encoding(self): |
| 367 |
import sys |
| 368 |
if '__pypy__' not in sys.builtin_module_names: |
| 369 |
skip("only on PyPy") |
| 370 |
savestderr = sys.stderr |
| 371 |
class MyStringIO(object): |
| 372 |
def __init__(self): |
| 373 |
self.output = [] |
| 374 |
def write(self, s): |
| 375 |
assert isinstance(s, str) |
| 376 |
self.output.append(s) |
| 377 |
def getvalue(self): |
| 378 |
return ''.join(self.output) |
| 379 |
|
| 380 |
for input in ("\u013a", "\u1111"): |
| 381 |
err = MyStringIO() |
| 382 |
err.encoding = 'iso-8859-2' |
| 383 |
sys.stderr = err |
| 384 |
|
| 385 |
eh = sys.__excepthook__ |
| 386 |
try: |
| 387 |
raise ValueError(input) |
| 388 |
except ValueError as exc: |
| 389 |
eh(*sys.exc_info()) |
| 390 |
|
| 391 |
sys.stderr = savestderr |
| 392 |
print(ascii(err.getvalue())) |
| 393 |
assert err.getvalue().endswith("ValueError: %s\n" % input) |
| 394 |
|
| 395 |
def test_excepthook_flushes_stdout(self): r""" |
| 396 |
import sys, io |
| 397 |
savestdout = sys.stdout |
| 398 |
out = io.StringIO() |
| 399 |
sys.stdout = out |
| 400 |
|
| 401 |
eh = sys.__excepthook__ |
| 402 |
|
| 403 |
try: |
| 404 |
raise ValueError(42) |
| 405 |
except ValueError as exc: |
| 406 |
print("hello") # with end-of-line |
| 407 |
eh(*sys.exc_info()) |
| 408 |
try: |
| 409 |
raise ValueError(42) |
| 410 |
except ValueError as exc: |
| 411 |
print(123, 456, end="") # no end-of-line here |
| 412 |
eh(*sys.exc_info()) |
| 413 |
|
| 414 |
sys.stdout = savestdout |
| 415 |
assert out.getvalue() == 'hello\n123 456' # no final \n added in 3.x |
| 416 |
""" |
| 417 |
|
| 418 |
def test_tracebacklimit_excepthook(self): |
| 419 |
import sys |
| 420 |
savestderr = sys.stderr |
| 421 |
assert not hasattr(sys, "tracebacklimit") |
| 422 |
|
| 423 |
eh = sys.__excepthook__ |
| 424 |
def f1(): |
| 425 |
f2() |
| 426 |
def f2(): |
| 427 |
f3() |
| 428 |
def f3(): |
| 429 |
raise ValueError(42) |
| 430 |
|
| 431 |
def get_error_with_tracebacklimit(limit): |
| 432 |
import io |
| 433 |
sys.tracebacklimit = limit |
| 434 |
sys.stderr = err = io.StringIO() |
| 435 |
try: |
| 436 |
f1() |
| 437 |
except ValueError: |
| 438 |
eh(*sys.exc_info()) |
| 439 |
return err.getvalue() |
| 440 |
|
| 441 |
|
| 442 |
msg = get_error_with_tracebacklimit(2) |
| 443 |
assert "f1" not in msg |
| 444 |
assert "f2" in msg |
| 445 |
assert "f3" in msg |
| 446 |
|
| 447 |
msg = get_error_with_tracebacklimit(0) |
| 448 |
assert "Traceback (most recent call last):" not in msg |
| 449 |
assert "ValueError" in msg |
| 450 |
|
| 451 |
msg = get_error_with_tracebacklimit(-1) |
| 452 |
assert "Traceback (most recent call last):" not in msg |
| 453 |
assert "ValueError" in msg |
| 454 |
|
| 455 |
sys.stderr = savestderr |
| 456 |
del sys.tracebacklimit |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
def test_exit(self): |
| 463 |
import sys |
| 464 |
raises(TypeError, sys.exit, 42, 42) |
| 465 |
|
| 466 |
|
| 467 |
try: |
| 468 |
sys.exit(0) |
| 469 |
except SystemExit as exc: |
| 470 |
assert exc.code == 0 |
| 471 |
except: |
| 472 |
raise AssertionError("wrong exception") |
| 473 |
else: |
| 474 |
raise AssertionError("no exception") |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
try: |
| 479 |
sys.exit(42) |
| 480 |
except SystemExit as exc: |
| 481 |
assert exc.code == 42 |
| 482 |
except: |
| 483 |
raise AssertionError("wrong exception") |
| 484 |
else: |
| 485 |
raise AssertionError("no exception") |
| 486 |
|
| 487 |
|
| 488 |
try: |
| 489 |
sys.exit((42,)) |
| 490 |
except SystemExit as exc: |
| 491 |
assert exc.code == 42 |
| 492 |
except: |
| 493 |
raise AssertionError("wrong exception") |
| 494 |
else: |
| 495 |
raise AssertionError("no exception") |
| 496 |
|
| 497 |
|
| 498 |
try: |
| 499 |
sys.exit("exit") |
| 500 |
except SystemExit as exc: |
| 501 |
assert exc.code == "exit" |
| 502 |
except: |
| 503 |
raise AssertionError("wrong exception") |
| 504 |
else: |
| 505 |
raise AssertionError("no exception") |
| 506 |
|
| 507 |
|
| 508 |
try: |
| 509 |
sys.exit((17, 23)) |
| 510 |
except SystemExit as exc: |
| 511 |
assert exc.code == (17, 23) |
| 512 |
except: |
| 513 |
raise AssertionError("wrong exception") |
| 514 |
else: |
| 515 |
raise AssertionError("no exception") |
| 516 |
|
| 517 |
def test_getdefaultencoding(self): |
| 518 |
import sys |
| 519 |
raises(TypeError, sys.getdefaultencoding, 42) |
| 520 |
|
| 521 |
assert isinstance(sys.getdefaultencoding(), str) |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
def test_setcheckinterval(self): |
| 527 |
import sys |
| 528 |
raises(TypeError, sys.setcheckinterval) |
| 529 |
orig = sys.getcheckinterval() |
| 530 |
for n in 0, 100, 120, orig: |
| 531 |
sys.setcheckinterval(n) |
| 532 |
assert sys.getcheckinterval() == n |
| 533 |
|
| 534 |
def test_setswitchinterval(self): |
| 535 |
import sys |
| 536 |
raises(TypeError, sys.setswitchinterval) |
| 537 |
orig = sys.getswitchinterval() |
| 538 |
for n in 1e-6, 0.1, orig: |
| 539 |
sys.setswitchinterval(n) |
| 540 |
assert sys.getswitchinterval() == n |
| 541 |
raises(ValueError, sys.setswitchinterval, 0.0) |
| 542 |
|
| 543 |
def test_recursionlimit(self): |
| 544 |
import sys |
| 545 |
raises(TypeError, sys.getrecursionlimit, 42) |
| 546 |
oldlimit = sys.getrecursionlimit() |
| 547 |
raises(TypeError, sys.setrecursionlimit) |
| 548 |
raises(ValueError, sys.setrecursionlimit, -42) |
| 549 |
sys.setrecursionlimit(10000) |
| 550 |
assert sys.getrecursionlimit() == 10000 |
| 551 |
sys.setrecursionlimit(oldlimit) |
| 552 |
raises(OverflowError, sys.setrecursionlimit, 1<<31) |
| 553 |
|
| 554 |
def test_recursionlimit_toolow(self): |
| 555 |
import sys |
| 556 |
def callatlevel(l): |
| 557 |
if l > 0: |
| 558 |
callatlevel(l - 1) |
| 559 |
else: |
| 560 |
sys.setrecursionlimit(1) |
| 561 |
with raises(RecursionError): |
| 562 |
callatlevel(500) |
| 563 |
|
| 564 |
def test_getwindowsversion(self): |
| 565 |
import sys |
| 566 |
if hasattr(sys, "getwindowsversion"): |
| 567 |
v = sys.getwindowsversion() |
| 568 |
if '__pypy__' in sys.builtin_module_names: |
| 569 |
assert isinstance(v, tuple) |
| 570 |
assert len(v) == 5 |
| 571 |
assert isinstance(v[0], int) |
| 572 |
assert isinstance(v[1], int) |
| 573 |
assert isinstance(v[2], int) |
| 574 |
assert isinstance(v[3], int) |
| 575 |
assert isinstance(v[4], str) |
| 576 |
|
| 577 |
assert v[0] == v.major |
| 578 |
assert v[1] == v.minor |
| 579 |
assert v[2] == v.build |
| 580 |
assert v[3] == v.platform |
| 581 |
assert v[4] == v.service_pack |
| 582 |
|
| 583 |
assert isinstance(v.service_pack_minor, int) |
| 584 |
assert isinstance(v.service_pack_major, int) |
| 585 |
assert isinstance(v.suite_mask, int) |
| 586 |
assert isinstance(v.product_type, int) |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
maj, min, buildno, plat, csd = sys.getwindowsversion() |
| 591 |
|
| 592 |
def test_winver(self): |
| 593 |
import sys |
| 594 |
if hasattr(sys, "winver"): |
| 595 |
assert sys.winver == sys.version[:4] |
| 596 |
|
| 597 |
def test_dllhandle(self): |
| 598 |
import sys |
| 599 |
assert hasattr(sys, 'dllhandle') == (sys.platform == 'win32') |
| 600 |
|
| 601 |
def test_dlopenflags(self): |
| 602 |
import sys |
| 603 |
if not hasattr(sys, "getdlopenflags"): |
| 604 |
skip('{gs}etdlopenflags is not implemented on this platform') |
| 605 |
raises(TypeError, sys.getdlopenflags, 42) |
| 606 |
oldflags = sys.getdlopenflags() |
| 607 |
raises(TypeError, sys.setdlopenflags) |
| 608 |
sys.setdlopenflags(oldflags+1) |
| 609 |
assert sys.getdlopenflags() == oldflags+1 |
| 610 |
sys.setdlopenflags(oldflags) |
| 611 |
|
| 612 |
def test_refcount(self): |
| 613 |
import sys |
| 614 |
if not hasattr(sys, "getrefcount"): |
| 615 |
skip('Reference counting is not implemented.') |
| 616 |
|
| 617 |
raises(TypeError, sys.getrefcount) |
| 618 |
c = sys.getrefcount(None) |
| 619 |
n = None |
| 620 |
assert sys.getrefcount(None) == c+1 |
| 621 |
del n |
| 622 |
assert sys.getrefcount(None) == c |
| 623 |
if hasattr(sys, "gettotalrefcount"): |
| 624 |
assert isinstance(sys.gettotalrefcount(), int) |
| 625 |
|
| 626 |
def test_getframe(self): |
| 627 |
import sys |
| 628 |
raises(TypeError, sys._getframe, 42, 42) |
| 629 |
raises(ValueError, sys._getframe, 2000000000) |
| 630 |
assert sys._getframe().f_code.co_name == 'test_getframe' |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
def test_getframe_in_returned_func(self): |
| 637 |
import sys |
| 638 |
def f(): |
| 639 |
return g() |
| 640 |
def g(): |
| 641 |
return sys._getframe(0) |
| 642 |
frame = f() |
| 643 |
assert frame.f_code.co_name == 'g' |
| 644 |
assert frame.f_back.f_code.co_name == 'f' |
| 645 |
assert frame.f_back.f_back.f_code.co_name == 'test_getframe_in_returned_func' |
| 646 |
|
| 647 |
def test_attributes(self): |
| 648 |
import sys |
| 649 |
assert sys.__name__ == 'sys' |
| 650 |
assert isinstance(sys.modules, dict) |
| 651 |
assert isinstance(sys.path, list) |
| 652 |
assert isinstance(sys.api_version, int) |
| 653 |
assert isinstance(sys.argv, list) |
| 654 |
assert sys.byteorder in ("little", "big") |
| 655 |
assert isinstance(sys.builtin_module_names, tuple) |
| 656 |
assert isinstance(sys.copyright, str) |
| 657 |
|
| 658 |
|
| 659 |
assert isinstance(sys.hexversion, int) |
| 660 |
assert isinstance(sys.maxsize, int) |
| 661 |
assert isinstance(sys.maxunicode, int) |
| 662 |
assert isinstance(sys.platform, str) |
| 663 |
|
| 664 |
assert isinstance(sys.version, str) |
| 665 |
assert isinstance(sys.warnoptions, list) |
| 666 |
vi = sys.version_info |
| 667 |
if '__pypy__' in sys.builtin_module_names: |
| 668 |
assert isinstance(vi, tuple) |
| 669 |
assert len(vi) == 5 |
| 670 |
assert isinstance(vi[0], int) |
| 671 |
assert isinstance(vi[1], int) |
| 672 |
assert isinstance(vi[2], int) |
| 673 |
assert vi[3] in ("alpha", "beta", "candidate", "final") |
| 674 |
assert isinstance(vi[4], int) |
| 675 |
assert isinstance(sys._framework, str) |
| 676 |
|
| 677 |
def test_implementation(self): |
| 678 |
import sys |
| 679 |
assert sys.implementation.name == 'pypy' |
| 680 |
|
| 681 |
|
| 682 |
levels = {'alpha': 0xA, 'beta': 0xB, 'candidate': 0xC, 'final': 0xF} |
| 683 |
|
| 684 |
assert sys.implementation.version |
| 685 |
assert sys.implementation.hexversion |
| 686 |
assert sys.implementation.cache_tag |
| 687 |
|
| 688 |
version = sys.implementation.version |
| 689 |
assert version[:2] == (version.major, version.minor) |
| 690 |
|
| 691 |
hexversion = (version.major << 24 | version.minor << 16 | |
| 692 |
version.micro << 8 | levels[version.releaselevel] << 4 | |
| 693 |
version.serial << 0) |
| 694 |
assert sys.implementation.hexversion == hexversion |
| 695 |
|
| 696 |
|
| 697 |
assert sys.implementation.name == sys.implementation.name.lower() |
| 698 |
|
| 699 |
ns1 = type(sys.implementation)(x=1, y=2, w=3) |
| 700 |
assert repr(ns1) == "namespace(w=3, x=1, y=2)" |
| 701 |
|
| 702 |
def test_simplenamespace(self): |
| 703 |
import sys |
| 704 |
SimpleNamespace = type(sys.implementation) |
| 705 |
ns = SimpleNamespace(x=1, y=2, w=3) |
| 706 |
|
| 707 |
ns.z = 4 |
| 708 |
assert ns.__dict__ == dict(x=1, y=2, w=3, z=4) |
| 709 |
|
| 710 |
raises(AttributeError, "del ns.spam") |
| 711 |
del ns.y |
| 712 |
|
| 713 |
assert ns == SimpleNamespace(z=4, x=1, w=3) |
| 714 |
assert (ns != SimpleNamespace(z=4, x=1, w=3)) is False |
| 715 |
assert (ns == SimpleNamespace(z=4, x=2, w=3)) is False |
| 716 |
assert ns != SimpleNamespace(z=4, x=2, w=3) |
| 717 |
|
| 718 |
class Foo(SimpleNamespace): |
| 719 |
pass |
| 720 |
assert ns == Foo(z=4, x=1, w=3) |
| 721 |
assert (ns != Foo(z=4, x=1, w=3)) is False |
| 722 |
assert (ns == Foo(z=4, x=2, w=3)) is False |
| 723 |
assert ns != Foo(z=4, x=2, w=3) |
| 724 |
|
| 725 |
class Other: |
| 726 |
def __init__(self, x, z, w): |
| 727 |
self.x = x |
| 728 |
self.z = z |
| 729 |
self.w = w |
| 730 |
assert (ns == Other(z=4, x=1, w=3)) is False |
| 731 |
assert ns != Other(z=4, x=1, w=3) |
| 732 |
assert (Foo(z=4, x=1, w=3) == Other(z=4, x=1, w=3)) is False |
| 733 |
assert Foo(z=4, x=1, w=3) != Other(z=4, x=1, w=3) |
| 734 |
|
| 735 |
class Fake: |
| 736 |
__class__ = SimpleNamespace |
| 737 |
assert isinstance(Fake(), SimpleNamespace) |
| 738 |
assert not issubclass(Fake, SimpleNamespace) |
| 739 |
assert (Fake() == SimpleNamespace()) is False |
| 740 |
assert SimpleNamespace() != Fake() |
| 741 |
|
| 742 |
def test_pickle_simplenamespace(self): |
| 743 |
import _pickle as pickle, sys |
| 744 |
SimpleNamespace = type(sys.implementation) |
| 745 |
ns = SimpleNamespace(x=1, y=2, w=3) |
| 746 |
copy = pickle.loads(pickle.dumps(ns)) |
| 747 |
assert copy == ns |
| 748 |
|
| 749 |
def test_reload_doesnt_override_sys_executable(self): |
| 750 |
import sys |
| 751 |
from imp import reload |
| 752 |
if not hasattr(sys, 'executable'): |
| 753 |
sys.executable = 'from_test_sysmodule' |
| 754 |
previous = sys.executable |
| 755 |
reload(sys) |
| 756 |
assert sys.executable == previous |
| 757 |
|
| 758 |
def test_settrace(self): |
| 759 |
import sys |
| 760 |
counts = [] |
| 761 |
def trace(x, y, z): |
| 762 |
counts.append(None) |
| 763 |
|
| 764 |
def x(): |
| 765 |
pass |
| 766 |
sys.settrace(trace) |
| 767 |
try: |
| 768 |
x() |
| 769 |
assert sys.gettrace() is trace |
| 770 |
finally: |
| 771 |
sys.settrace(None) |
| 772 |
assert len(counts) == 1 |
| 773 |
|
| 774 |
def test_pypy_attributes(self): |
| 775 |
import sys |
| 776 |
if '__pypy__' not in sys.builtin_module_names: |
| 777 |
skip("only on PyPy") |
| 778 |
assert isinstance(sys.pypy_objspaceclass, str) |
| 779 |
vi = sys.pypy_version_info |
| 780 |
assert isinstance(vi, tuple) |
| 781 |
assert len(vi) == 5 |
| 782 |
assert isinstance(vi[0], int) |
| 783 |
assert isinstance(vi[1], int) |
| 784 |
assert isinstance(vi[2], int) |
| 785 |
assert vi[3] in ("alpha", "beta", "candidate", "dev", "final") |
| 786 |
assert isinstance(vi[4], int) |
| 787 |
|
| 788 |
def test_allattributes(self): |
| 789 |
import sys |
| 790 |
sys.__dict__ |
| 791 |
|
| 792 |
def test_float_repr_style(self): |
| 793 |
import sys |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
assert sys.float_repr_style == "short" |
| 798 |
|
| 799 |
def test_is_finalizing(self): |
| 800 |
import sys |
| 801 |
assert not sys.is_finalizing() |
| 802 |
|
| 803 |
|
| 804 |
def test_asyncgen_hooks(self): |
| 805 |
import sys |
| 806 |
old = sys.get_asyncgen_hooks() |
| 807 |
assert old.firstiter is None |
| 808 |
assert old.finalizer is None |
| 809 |
|
| 810 |
firstiter = lambda *a: None |
| 811 |
sys.set_asyncgen_hooks(firstiter=firstiter) |
| 812 |
hooks = sys.get_asyncgen_hooks() |
| 813 |
assert hooks.firstiter is firstiter |
| 814 |
assert hooks[0] is firstiter |
| 815 |
assert hooks.finalizer is None |
| 816 |
assert hooks[1] is None |
| 817 |
|
| 818 |
finalizer = lambda *a: None |
| 819 |
sys.set_asyncgen_hooks(finalizer=finalizer) |
| 820 |
hooks = sys.get_asyncgen_hooks() |
| 821 |
assert hooks.firstiter is firstiter |
| 822 |
assert hooks[0] is firstiter |
| 823 |
assert hooks.finalizer is finalizer |
| 824 |
assert hooks[1] is finalizer |
| 825 |
|
| 826 |
sys.set_asyncgen_hooks(*old) |
| 827 |
cur = sys.get_asyncgen_hooks() |
| 828 |
assert cur.firstiter is None |
| 829 |
assert cur.finalizer is None |
| 830 |
|
| 831 |
def test_coroutine_origin_tracking_depth(self): |
| 832 |
import sys |
| 833 |
depth = sys.get_coroutine_origin_tracking_depth() |
| 834 |
assert depth == 0 |
| 835 |
try: |
| 836 |
sys.set_coroutine_origin_tracking_depth(6) |
| 837 |
depth = sys.get_coroutine_origin_tracking_depth() |
| 838 |
assert depth == 6 |
| 839 |
with raises(ValueError): |
| 840 |
sys.set_coroutine_origin_tracking_depth(-5) |
| 841 |
finally: |
| 842 |
sys.set_coroutine_origin_tracking_depth(0) |
| 843 |
|
| 844 |
class AppTestSysSettracePortedFromCpython(object): |
| 845 |
def test_sys_settrace(self): |
| 846 |
import sys |
| 847 |
|
| 848 |
class Tracer: |
| 849 |
def __init__(self): |
| 850 |
self.events = [] |
| 851 |
def trace(self, frame, event, arg): |
| 852 |
self.events.append((frame.f_lineno, event)) |
| 853 |
return self.trace |
| 854 |
def traceWithGenexp(self, frame, event, arg): |
| 855 |
(o for o in [1]) |
| 856 |
self.events.append((frame.f_lineno, event)) |
| 857 |
return self.trace |
| 858 |
|
| 859 |
def compare_events(line_offset, events, expected_events): |
| 860 |
events = [(l - line_offset, e) for (l, e) in events] |
| 861 |
length = len(expected_events) |
| 862 |
assert len(events) >= length |
| 863 |
|
| 864 |
assert events[-length:] == expected_events |
| 865 |
|
| 866 |
def run_test2(func): |
| 867 |
tracer = Tracer() |
| 868 |
func(tracer.trace) |
| 869 |
sys.settrace(None) |
| 870 |
compare_events(func.__code__.co_firstlineno, |
| 871 |
tracer.events, func.events) |
| 872 |
|
| 873 |
|
| 874 |
def _settrace_and_return(tracefunc): |
| 875 |
sys.settrace(tracefunc) |
| 876 |
sys._getframe().f_back.f_trace = tracefunc |
| 877 |
def settrace_and_return(tracefunc): |
| 878 |
_settrace_and_return(tracefunc) |
| 879 |
|
| 880 |
|
| 881 |
def _settrace_and_raise(tracefunc): |
| 882 |
sys.settrace(tracefunc) |
| 883 |
sys._getframe().f_back.f_trace = tracefunc |
| 884 |
raise RuntimeError |
| 885 |
def settrace_and_raise(tracefunc): |
| 886 |
try: |
| 887 |
_settrace_and_raise(tracefunc) |
| 888 |
except RuntimeError as exc: |
| 889 |
pass |
| 890 |
|
| 891 |
settrace_and_raise.events = [(2, 'exception'), |
| 892 |
(3, 'line'), |
| 893 |
(4, 'line'), |
| 894 |
(4, 'return')] |
| 895 |
|
| 896 |
settrace_and_return.events = [(1, 'return')] |
| 897 |
run_test2(settrace_and_return) |
| 898 |
run_test2(settrace_and_raise) |
| 899 |
|
| 900 |
|
| 901 |
class AppTestCurrentFramesWithThread: |
| 902 |
|
| 903 |
def test_current_frames(self): |
| 904 |
import sys |
| 905 |
import _thread |
| 906 |
if sys.platform == 'darwin': |
| 907 |
skip('test can hang on macos') |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
sys._current_frames() |
| 913 |
|
| 914 |
thread_id = _thread.get_ident() |
| 915 |
def other_thread(): |
| 916 |
|
| 917 |
lock2.release() |
| 918 |
lock1.acquire() |
| 919 |
lock1 = _thread.allocate_lock() |
| 920 |
lock2 = _thread.allocate_lock() |
| 921 |
lock1.acquire() |
| 922 |
lock2.acquire() |
| 923 |
_thread.start_new_thread(other_thread, ()) |
| 924 |
|
| 925 |
def f(): |
| 926 |
lock2.acquire() |
| 927 |
return sys._current_frames() |
| 928 |
|
| 929 |
frames = f() |
| 930 |
lock1.release() |
| 931 |
thisframe = frames.pop(thread_id) |
| 932 |
assert thisframe.f_code.co_name in ('f', '?') |
| 933 |
|
| 934 |
assert len(frames) == 1 |
| 935 |
_, other_frame = frames.popitem() |
| 936 |
assert other_frame.f_code.co_name in ('other_thread', '?') |
| 937 |
|
| 938 |
def test_current_exceptions(self): |
| 939 |
import sys |
| 940 |
import _thread |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
sys._current_frames() |
| 946 |
|
| 947 |
thread_id = _thread.get_ident() |
| 948 |
def other_thread(): |
| 949 |
|
| 950 |
lock2.release() |
| 951 |
try: |
| 952 |
raise ValueError("oops") |
| 953 |
except ValueError: |
| 954 |
lock1.acquire() |
| 955 |
lock1 = _thread.allocate_lock() |
| 956 |
lock2 = _thread.allocate_lock() |
| 957 |
lock1.acquire() |
| 958 |
lock2.acquire() |
| 959 |
_thread.start_new_thread(other_thread, ()) |
| 960 |
|
| 961 |
def f(): |
| 962 |
lock2.acquire() |
| 963 |
return sys._current_exceptions() |
| 964 |
|
| 965 |
exc = f() |
| 966 |
lock1.release() |
| 967 |
thisexc = exc.pop(thread_id) |
| 968 |
assert thisexc == (None, None, None) |
| 969 |
|
| 970 |
assert len(exc) == 1 |
| 971 |
key, values = exc.popitem() |
| 972 |
exc_type, exc_value, exc_tb = values |
| 973 |
assert str(exc_value) == "oops" |
| 974 |
|
| 975 |
def test_intern(self): |
| 976 |
from sys import intern |
| 977 |
raises(TypeError, intern) |
| 978 |
raises(TypeError, intern, 1) |
| 979 |
class S(str): |
| 980 |
pass |
| 981 |
raises(TypeError, intern, S("hello")) |
| 982 |
s = "never interned before" |
| 983 |
s2 = intern(s) |
| 984 |
assert s == s2 |
| 985 |
s3 = s.swapcase() |
| 986 |
assert s3 != s2 |
| 987 |
s4 = s3.swapcase() |
| 988 |
assert intern(s4) is s2 |
| 989 |
s5 = "\ud800" |
| 990 |
|
| 991 |
assert intern(s5) == s5 |
| 992 |
|
| 993 |
|
| 994 |
class AppTestSysExcInfoDirect: |
| 995 |
|
| 996 |
def setup_method(self, meth): |
| 997 |
self.checking = not self.runappdirect |
| 998 |
if self.checking: |
| 999 |
self.seen = [] |
| 1000 |
from pypy.module.sys import vm |
| 1001 |
def exc_info_with_tb(*args): |
| 1002 |
self.seen.append("n") |
| 1003 |
return self.old[0](*args) |
| 1004 |
def exc_info_without_tb(*args): |
| 1005 |
self.seen.append("y") |
| 1006 |
return self.old[1](*args) |
| 1007 |
self.old = [vm.exc_info_with_tb, vm.exc_info_without_tb] |
| 1008 |
vm.exc_info_with_tb = exc_info_with_tb |
| 1009 |
vm.exc_info_without_tb = exc_info_without_tb |
| 1010 |
|
| 1011 |
from rpython.rlib import jit |
| 1012 |
self.old2 = [jit.we_are_jitted] |
| 1013 |
jit.we_are_jitted = lambda: True |
| 1014 |
|
| 1015 |
def teardown_method(self, meth): |
| 1016 |
if self.checking: |
| 1017 |
from pypy.module.sys import vm |
| 1018 |
from rpython.rlib import jit |
| 1019 |
vm.exc_info_with_tb = self.old[0] |
| 1020 |
vm.exc_info_without_tb = self.old[1] |
| 1021 |
jit.we_are_jitted = self.old2[0] |
| 1022 |
|
| 1023 |
assert ''.join(self.seen) == meth.expected |
| 1024 |
|
| 1025 |
def test_returns_none(self): |
| 1026 |
import sys |
| 1027 |
assert sys.exc_info() == (None, None, None) |
| 1028 |
assert sys.exc_info()[0] is None |
| 1029 |
assert sys.exc_info()[1] is None |
| 1030 |
assert sys.exc_info()[2] is None |
| 1031 |
assert sys.exc_info()[:2] == (None, None) |
| 1032 |
assert sys.exc_info()[:3] == (None, None, None) |
| 1033 |
assert sys.exc_info()[0:2] == (None, None) |
| 1034 |
assert sys.exc_info()[2:4] == (None,) |
| 1035 |
test_returns_none.expected = 'nnnnnnnn' |
| 1036 |
|
| 1037 |
def test_returns_subscr(self): |
| 1038 |
import sys |
| 1039 |
e = KeyError("boom") |
| 1040 |
try: |
| 1041 |
raise e |
| 1042 |
except: |
| 1043 |
assert sys.exc_info()[0] is KeyError |
| 1044 |
assert sys.exc_info()[1] is e |
| 1045 |
assert sys.exc_info()[2] is not None |
| 1046 |
assert sys.exc_info()[-3] is KeyError |
| 1047 |
assert sys.exc_info()[-2] is e |
| 1048 |
assert sys.exc_info()[-1] is not None |
| 1049 |
test_returns_subscr.expected = 'yynyyn' |
| 1050 |
|
| 1051 |
def test_returns_slice_2(self): |
| 1052 |
import sys |
| 1053 |
e = KeyError("boom") |
| 1054 |
try: |
| 1055 |
raise e |
| 1056 |
except: |
| 1057 |
foo = sys.exc_info() |
| 1058 |
assert sys.exc_info()[:0] == () |
| 1059 |
assert sys.exc_info()[:1] == foo[:1] |
| 1060 |
assert sys.exc_info()[:2] == foo[:2] |
| 1061 |
assert sys.exc_info()[:3] == foo |
| 1062 |
assert sys.exc_info()[:4] == foo |
| 1063 |
assert sys.exc_info()[:-1] == foo[:2] |
| 1064 |
assert sys.exc_info()[:-2] == foo[:1] |
| 1065 |
assert sys.exc_info()[:-3] == () |
| 1066 |
test_returns_slice_2.expected = 'nyyynnyyy' |
| 1067 |
|
| 1068 |
def test_returns_slice_3(self): |
| 1069 |
import sys |
| 1070 |
e = KeyError("boom") |
| 1071 |
try: |
| 1072 |
raise e |
| 1073 |
except: |
| 1074 |
foo = sys.exc_info() |
| 1075 |
assert sys.exc_info()[2:2] == () |
| 1076 |
assert sys.exc_info()[0:1] == foo[:1] |
| 1077 |
assert sys.exc_info()[1:2] == foo[1:2] |
| 1078 |
assert sys.exc_info()[0:3] == foo |
| 1079 |
assert sys.exc_info()[2:4] == foo[2:] |
| 1080 |
assert sys.exc_info()[0:-1] == foo[:2] |
| 1081 |
assert sys.exc_info()[0:-2] == foo[:1] |
| 1082 |
assert sys.exc_info()[5:-3] == () |
| 1083 |
test_returns_slice_3.expected = 'nyyynnyyy' |
| 1084 |
|
| 1085 |
def test_strange_invocation(self): |
| 1086 |
import sys |
| 1087 |
e = KeyError("boom") |
| 1088 |
try: |
| 1089 |
raise e |
| 1090 |
except: |
| 1091 |
a = []; k = {} |
| 1092 |
assert sys.exc_info(*a)[:0] == () |
| 1093 |
assert sys.exc_info(**k)[:0] == () |
| 1094 |
test_strange_invocation.expected = 'nn' |
| 1095 |
|
| 1096 |
def test_call_in_subfunction(self): |
| 1097 |
import sys |
| 1098 |
def g(): |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
return sys.exc_info()[1] |
| 1105 |
e = KeyError("boom") |
| 1106 |
try: |
| 1107 |
raise e |
| 1108 |
except: |
| 1109 |
assert g() is e |
| 1110 |
test_call_in_subfunction.expected = 'y' |