Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-141999: handle KeyboardInterrupt when sampling and print the result#142000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
yihong0618 wants to merge3 commits intopython:main
base:main
Choose a base branch
Loading
fromyihong0618:hy/handle_ctrl_c_in_sampling
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
fix: add test for it
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
  • Loading branch information
@yihong0618
yihong0618 committedNov 28, 2025
commitf825938ccbcc350bcc2d981fbac7563a10f3ab91
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -224,6 +224,46 @@ def test_sample_profiler_missed_samples_warning(self):
self.assertIn("Warning: missed", result)
self.assertIn("samples from the expected total", result)

def test_sample_profiler_keyboard_interrupt(self):
mock_unwinder = mock.MagicMock()
mock_unwinder.get_stack_trace.side_effect = [
[
(
1,
[
mock.MagicMock(
filename="test.py", lineno=10, funcname="test_func"
)
],
)
],
KeyboardInterrupt(),
]

with mock.patch(
"_remote_debugging.RemoteUnwinder"
) as mock_unwinder_class:
mock_unwinder_class.return_value = mock_unwinder
profiler = SampleProfiler(
pid=12345, sample_interval_usec=10000, all_threads=False
)
mock_collector = mock.MagicMock()
times = [0.0, 0.01, 0.02, 0.03, 0.04]
with mock.patch("time.perf_counter", side_effect=times):
with io.StringIO() as output:
with mock.patch("sys.stdout", output):
try:
profiler.sample(mock_collector, duration_sec=1.0)
except KeyboardInterrupt:
self.fail(
"KeyboardInterrupt was not handled by the profiler"
)
result = output.getvalue()
self.assertIn("Interrupted by user.", result)
self.assertIn("Captured", result)
self.assertIn("samples", result)
self.assertNotIn("Warning: missed", result)


@force_not_colorized_test_class
class TestPrintSampledStats(unittest.TestCase):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
Correctly allow :exc:`KeyboardInterrupt` to stop the process when using
:mod:`profiling.sampling`.
:mod:`!profiling.sampling`.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp