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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
from pydevd import PyDB
import pytest
from tests_python.debugger_unittest import IS_CPYTHON
import threading
from _pydevd_bundle.pydevd_constants import PYDEVD_USE_SYS_MONITORING
DEBUG = False
pytestmark = pytest.mark.skipif(PYDEVD_USE_SYS_MONITORING, reason="The tests in this module requires tracing.")
class DummyTopLevelFrame(object):
__slots__ = ["f_code", "f_back", "f_lineno", "f_trace"]
def __init__(self, method):
self.f_code = method.__code__
self.f_back = None
self.f_lineno = method.__code__.co_firstlineno
class DummyWriter(object):
__slots__ = ["commands", "command_meanings"]
def __init__(self):
self.commands = []
self.command_meanings = []
def add_command(self, cmd):
from _pydevd_bundle.pydevd_comm import ID_TO_MEANING
meaning = ID_TO_MEANING[str(cmd.id)]
if DEBUG:
print(meaning)
self.command_meanings.append(meaning)
if DEBUG:
print(cmd._as_bytes.decode("utf-8"))
self.commands.append(cmd)
class DummyPyDb(PyDB):
def __init__(self):
PyDB.__init__(self, set_as_global=False)
def do_wait_suspend(self, thread, frame, event, arg, *args, **kwargs):
from _pydevd_bundle.pydevd_constants import STATE_RUN
info = thread.additional_info
info.pydev_original_step_cmd = -1
info.pydev_step_cmd = -1
info.pydev_step_stop = None
info.pydev_state = STATE_RUN
return PyDB.do_wait_suspend(self, thread, frame, event, arg, *args, **kwargs)
class _TraceTopLevel(object):
def __init__(self):
self.py_db = DummyPyDb()
self.py_db.writer = DummyWriter()
def set_target_func(self, target_func):
self.frame = DummyTopLevelFrame(target_func)
self.target_func = target_func
def get_exception_arg(self):
import sys
try:
raise AssertionError()
except:
arg = sys.exc_info()
return arg
def create_add_exception_breakpoint_with_policy(
self, exception, notify_on_handled_exceptions, notify_on_unhandled_exceptions, ignore_libraries
):
return "\t".join(str(x) for x in [exception, notify_on_handled_exceptions, notify_on_unhandled_exceptions, ignore_libraries])
def add_unhandled_exception_breakpoint(self):
from _pydevd_bundle.pydevd_process_net_command import process_net_command
from tests_python.debugger_unittest import CMD_ADD_EXCEPTION_BREAK
for exc_name in ("AssertionError", "RuntimeError"):
process_net_command(
self.py_db,
CMD_ADD_EXCEPTION_BREAK,
1,
self.create_add_exception_breakpoint_with_policy(exc_name, "0", "1", "0"),
)
def assert_last_commands(self, *commands):
assert self.py_db.writer.command_meanings[-len(commands) :] == list(commands)
def assert_no_commands(self, *commands):
for command in commands:
assert command not in self.py_db.writer.command_meanings
def trace_dispatch(self, event, arg):
from _pydevd_bundle import pydevd_trace_dispatch_regular
self.new_trace_func = pydevd_trace_dispatch_regular.trace_dispatch(self.py_db, self.frame, event, arg)
return self.new_trace_func
def call_trace_dispatch(self, line):
self.frame.f_lineno = line
return self.trace_dispatch("call", None)
def exception_trace_dispatch(self, line, arg):
self.frame.f_lineno = line
self.new_trace_func = self.new_trace_func(self.frame, "exception", arg)
def return_trace_dispatch(self, line):
self.frame.f_lineno = line
self.new_trace_func = self.new_trace_func(self.frame, "return", None)
def assert_paused(self):
self.assert_last_commands("CMD_THREAD_SUSPEND", "CMD_THREAD_RUN")
def assert_not_paused(self):
self.assert_no_commands("CMD_THREAD_SUSPEND", "CMD_THREAD_RUN")
@pytest.fixture
def trace_top_level():
# Note: we trace with a dummy frame with no f_back to simulate the issue in a remote attach.
yield _TraceTopLevel()
threading.current_thread().additional_info = None
@pytest.fixture
def trace_top_level_unhandled(trace_top_level):
trace_top_level.add_unhandled_exception_breakpoint()
return trace_top_level
_expected_functions_to_test = 0
def mark_handled(func):
global _expected_functions_to_test
_expected_functions_to_test += 1
func.__handled__ = True
return func
def mark_unhandled(func):
global _expected_functions_to_test
_expected_functions_to_test += 1
func.__handled__ = False
return func
# ------------------------------------------------------------------------------------------- Handled
@mark_handled
def raise_handled_exception():
try:
raise AssertionError()
except:
pass
@mark_handled
def raise_handled_exception2():
try:
raise AssertionError()
except AssertionError:
pass
@mark_handled
def raise_handled_exception3():
try:
try:
raise AssertionError()
except RuntimeError:
pass
except AssertionError:
pass
@mark_handled
def raise_handled_exception3a():
try:
try:
raise AssertionError()
except AssertionError:
pass
except RuntimeError:
pass
@mark_handled
def raise_handled_exception4():
try:
try:
raise AssertionError()
except RuntimeError:
pass
except (RuntimeError, AssertionError):
pass
@mark_handled
def raise_handled():
try:
try:
raise AssertionError()
except RuntimeError:
pass
except (RuntimeError, AssertionError):
pass
@mark_handled
def raise_handled2():
try:
raise AssertionError()
except (RuntimeError, AssertionError):
pass
try:
raise RuntimeError()
except (RuntimeError, AssertionError):
pass
@mark_handled
def raise_handled9():
for i in range(2):
try:
raise AssertionError()
except AssertionError:
if i == 1:
try:
raise
except:
pass
@mark_handled
def raise_handled10():
for i in range(2):
try:
raise AssertionError()
except AssertionError:
if i == 1:
try:
raise
except:
pass
_foo = 10
# ----------------------------------------------------------------------------------------- Unhandled
@mark_unhandled
def raise_unhandled_exception():
raise AssertionError()
@mark_unhandled
def raise_unhandled_exception_not_in_except_clause():
try:
raise AssertionError()
except RuntimeError:
pass
@mark_unhandled
def raise_unhandled():
try:
try:
raise AssertionError()
except RuntimeError:
pass
except (RuntimeError, AssertionError):
raise
@mark_unhandled
def raise_unhandled2():
try:
raise AssertionError()
except AssertionError:
pass
raise AssertionError()
@mark_unhandled
def raise_unhandled3():
try:
raise AssertionError()
except AssertionError:
raise AssertionError()
@mark_unhandled
def raise_unhandled4():
try:
raise AssertionError()
finally:
_a = 10
@mark_unhandled
def raise_unhandled5():
try:
raise AssertionError()
finally:
raise RuntimeError()
@mark_unhandled
def raise_unhandled6():
try:
raise AssertionError()
finally:
raise RuntimeError("in another" "line")
@mark_unhandled
def raise_unhandled7():
try:
raise AssertionError()
except AssertionError:
try:
raise AssertionError()
except RuntimeError:
pass
@mark_unhandled
def raise_unhandled8():
for i in range(2):
def get_exc_to_treat():
if i == 0:
return AssertionError
return RuntimeError
try:
raise AssertionError()
except get_exc_to_treat():
pass
@mark_unhandled
def raise_unhandled9():
for i in range(2):
def get_exc_to_treat():
if i == 0:
return AssertionError
return RuntimeError
try:
raise AssertionError()
except get_exc_to_treat():
try:
raise
except:
pass
@mark_unhandled
def raise_unhandled10():
for i in range(2):
try:
raise AssertionError()
except AssertionError:
if i == 1:
try:
raise
except RuntimeError:
pass
@mark_unhandled
def raise_unhandled11():
try:
raise_unhandled10()
finally:
if True:
pass
@mark_unhandled
def raise_unhandled12():
try:
raise AssertionError()
except:
pass
try:
raise AssertionError()
finally:
if True:
pass
@mark_unhandled
def reraise_handled_exception():
try:
raise AssertionError() # Should be considered unhandled (because it's reraised).
except:
raise
def _collect_events(func):
collected = []
def events_collector(frame, event, arg):
if frame.f_code.co_name == func.__name__:
collected.append((event, frame.f_lineno, arg))
return events_collector
import sys
sys.settrace(events_collector)
try:
func()
except:
import traceback
traceback.print_exc()
finally:
sys.settrace(None)
return collected
def _replay_events(collected, trace_top_level_unhandled):
for event, lineno, arg in collected:
if event == "call":
# Notify only unhandled
new_trace_func = trace_top_level_unhandled.call_trace_dispatch(lineno)
# Check that it's dealing with the top-level event.
if hasattr(new_trace_func, "get_method_object"):
new_trace_func = new_trace_func.get_method_object()
assert new_trace_func.__name__ == "trace_dispatch_and_unhandled_exceptions"
elif event == "exception":
trace_top_level_unhandled.exception_trace_dispatch(lineno, arg)
elif event == "return":
trace_top_level_unhandled.return_trace_dispatch(lineno)
elif event == "line":
pass
else:
raise AssertionError("Unexpected: %s" % (event,))
def _collect_target_functions():
# return [raise_unhandled10]
ret = []
for _key, method in sorted(dict(globals()).items()):
if hasattr(method, "__handled__"):
ret.append(method)
assert len(ret) == _expected_functions_to_test
return ret
@pytest.mark.skipif(not IS_CPYTHON, reason="try..except info only available on CPython")
@pytest.mark.parametrize("func", _collect_target_functions())
def test_tracing_on_top_level_unhandled(trace_top_level_unhandled, func):
trace_top_level_unhandled.set_target_func(func)
collected_events = _collect_events(func)
# print([(x[0], x[1], x[2].__class__.__name__) for x in collected_events])
_replay_events(collected_events, trace_top_level_unhandled)
if func.__handled__:
trace_top_level_unhandled.assert_not_paused() # handled exception
else:
trace_top_level_unhandled.assert_paused()
|