From f2de8d42ddf4a8d51b2d627338beb52a59b78f47 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Wed, 29 Oct 2025 09:40:29 +0000 Subject: [PATCH 1/3] Commit --- Lib/profiling/sampling/_sync_coordinator.py | 10 ++++++---- .../test_profiling/test_sampling_profiler.py | 17 +++++++++++++++++ ...25-10-29-09-40-10.gh-issue-140741.L13UCV.rst | 2 ++ 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-10-29-09-40-10.gh-issue-140741.L13UCV.rst diff --git a/Lib/profiling/sampling/_sync_coordinator.py b/Lib/profiling/sampling/_sync_coordinator.py index 79e8858ca17529..aab1aae0598d37 100644 --- a/Lib/profiling/sampling/_sync_coordinator.py +++ b/Lib/profiling/sampling/_sync_coordinator.py @@ -175,14 +175,16 @@ def _execute_script(script_path: str, script_args: List[str], cwd: str) -> None: try: with open(script_path, 'rb') as f: source_code = f.read() + except FileNotFoundError as e: + raise TargetError(f"Script file not found: {script_path}") from e + except PermissionError as e: + raise TargetError( + f"Permission denied reading script: {script_path}") from e + try: # Compile and execute the script code = compile(source_code, script_path, 'exec') exec(code, {'__name__': '__main__', '__file__': script_path}) - except FileNotFoundError as e: - raise TargetError(f"Script file not found: {script_path}") from e - except PermissionError as e: - raise TargetError(f"Permission denied reading script: {script_path}") from e except SyntaxError as e: raise TargetError(f"Syntax error in script {script_path}: {e}") from e except SystemExit: diff --git a/Lib/test/test_profiling/test_sampling_profiler.py b/Lib/test/test_profiling/test_sampling_profiler.py index 59bc18b9bcf14d..2fc7a416d70167 100644 --- a/Lib/test/test_profiling/test_sampling_profiler.py +++ b/Lib/test/test_profiling/test_sampling_profiler.py @@ -2080,6 +2080,23 @@ def test_valid_output_formats(self): # Expected errors - we just want to test format validation pass + def test_script_error_treatment(self): + script_file = tempfile.NamedTemporaryFile("w", delete=False, suffix=".py") + script_file.write("open('nonexistent_file.txt')\n") + script_file.close() + self.addCleanup(os.unlink, script_file.name) + + result = subprocess.run( + [sys.executable, "-m", "profiling.sampling.sample", "-d", "1", + script_file.name], + capture_output=True, + text=True, + ) + output = result.stdout + result.stderr + + self.assertNotIn("Script file not found", output) + self.assertIn("No such file or directory: 'nonexistent_file.txt'", output) + class TestSampleProfilerCLI(unittest.TestCase): def _setup_sync_mocks(self, mock_socket, mock_popen): diff --git a/Misc/NEWS.d/next/Library/2025-10-29-09-40-10.gh-issue-140741.L13UCV.rst b/Misc/NEWS.d/next/Library/2025-10-29-09-40-10.gh-issue-140741.L13UCV.rst new file mode 100644 index 00000000000000..69ca3e7efce17f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-29-09-40-10.gh-issue-140741.L13UCV.rst @@ -0,0 +1,2 @@ +Fix :mod:`!profiling.sampling.sample` incorrectly handling a +:exc:`FileNotFoundError` or :exc:`PermissionError`. From 2de695206918f4013462a0376ffbb811c5bf4307 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Wed, 29 Oct 2025 09:51:44 +0000 Subject: [PATCH 2/3] Commit --- .../next/Library/2025-10-29-09-40-10.gh-issue-140741.L13UCV.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-10-29-09-40-10.gh-issue-140741.L13UCV.rst b/Misc/NEWS.d/next/Library/2025-10-29-09-40-10.gh-issue-140741.L13UCV.rst index 69ca3e7efce17f..9fa8c561a03c78 100644 --- a/Misc/NEWS.d/next/Library/2025-10-29-09-40-10.gh-issue-140741.L13UCV.rst +++ b/Misc/NEWS.d/next/Library/2025-10-29-09-40-10.gh-issue-140741.L13UCV.rst @@ -1,2 +1,2 @@ -Fix :mod:`!profiling.sampling.sample` incorrectly handling a +Fix :func:`profiling.sampling.sample` incorrectly handling a :exc:`FileNotFoundError` or :exc:`PermissionError`. From 88b8cdc717e941370021597762f620d93a52add4 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Wed, 29 Oct 2025 09:53:29 +0000 Subject: [PATCH 3/3] Bloody auto-wrap --- Lib/profiling/sampling/_sync_coordinator.py | 3 +-- Lib/test/test_profiling/test_sampling_profiler.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/profiling/sampling/_sync_coordinator.py b/Lib/profiling/sampling/_sync_coordinator.py index aab1aae0598d37..8716e654104791 100644 --- a/Lib/profiling/sampling/_sync_coordinator.py +++ b/Lib/profiling/sampling/_sync_coordinator.py @@ -178,8 +178,7 @@ def _execute_script(script_path: str, script_args: List[str], cwd: str) -> None: except FileNotFoundError as e: raise TargetError(f"Script file not found: {script_path}") from e except PermissionError as e: - raise TargetError( - f"Permission denied reading script: {script_path}") from e + raise TargetError(f"Permission denied reading script: {script_path}") from e try: # Compile and execute the script diff --git a/Lib/test/test_profiling/test_sampling_profiler.py b/Lib/test/test_profiling/test_sampling_profiler.py index 2fc7a416d70167..cbfb21d3512eee 100644 --- a/Lib/test/test_profiling/test_sampling_profiler.py +++ b/Lib/test/test_profiling/test_sampling_profiler.py @@ -2087,8 +2087,7 @@ def test_script_error_treatment(self): self.addCleanup(os.unlink, script_file.name) result = subprocess.run( - [sys.executable, "-m", "profiling.sampling.sample", "-d", "1", - script_file.name], + [sys.executable, "-m", "profiling.sampling.sample", "-d", "1", script_file.name], capture_output=True, text=True, )