(self)
| 499 | @threading_helper.reap_threads |
| 500 | @threading_helper.requires_working_threading() |
| 501 | def test_current_frames(self): |
| 502 | import threading |
| 503 | import traceback |
| 504 | |
| 505 | # Spawn a thread that blocks at a known place. Then the main |
| 506 | # thread does sys._current_frames(), and verifies that the frames |
| 507 | # returned make sense. |
| 508 | entered_g = threading.Event() |
| 509 | leave_g = threading.Event() |
| 510 | thread_info = [] # the thread's id |
| 511 | |
| 512 | def f123(): |
| 513 | g456() |
| 514 | |
| 515 | def g456(): |
| 516 | thread_info.append(threading.get_ident()) |
| 517 | entered_g.set() |
| 518 | leave_g.wait() |
| 519 | |
| 520 | t = threading.Thread(target=f123) |
| 521 | t.start() |
| 522 | entered_g.wait() |
| 523 | |
| 524 | try: |
| 525 | # At this point, t has finished its entered_g.set(), although it's |
| 526 | # impossible to guess whether it's still on that line or has moved on |
| 527 | # to its leave_g.wait(). |
| 528 | self.assertEqual(len(thread_info), 1) |
| 529 | thread_id = thread_info[0] |
| 530 | |
| 531 | d = sys._current_frames() |
| 532 | for tid in d: |
| 533 | self.assertIsInstance(tid, int) |
| 534 | self.assertGreater(tid, 0) |
| 535 | |
| 536 | main_id = threading.get_ident() |
| 537 | self.assertIn(main_id, d) |
| 538 | self.assertIn(thread_id, d) |
| 539 | |
| 540 | # Verify that the captured main-thread frame is _this_ frame. |
| 541 | frame = d.pop(main_id) |
| 542 | self.assertTrue(frame is sys._getframe()) |
| 543 | |
| 544 | # Verify that the captured thread frame is blocked in g456, called |
| 545 | # from f123. This is a little tricky, since various bits of |
| 546 | # threading.py are also in the thread's call stack. |
| 547 | frame = d.pop(thread_id) |
| 548 | stack = traceback.extract_stack(frame) |
| 549 | for i, (filename, lineno, funcname, sourceline) in enumerate(stack): |
| 550 | if funcname == "f123": |
| 551 | break |
| 552 | else: |
| 553 | self.fail("didn't find f123() on thread's call stack") |
| 554 | |
| 555 | self.assertEqual(sourceline, "g456()") |
| 556 | |
| 557 | # And the next record must be for g456(). |
| 558 | filename, lineno, funcname, sourceline = stack[i+1] |
nothing calls this directly
no test coverage detected