Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions Lib/profiling/sampling/_sync_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,15 @@ 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()

# 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

try:
# Compile and execute the script
code = compile(source_code, script_path, 'exec')
exec(code, {'__name__': '__main__', '__file__': script_path})
except SyntaxError as e:
raise TargetError(f"Syntax error in script {script_path}: {e}") from e
except SystemExit:
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_profiling/test_sampling_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,22 @@ 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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`profiling.sampling.sample` incorrectly handling a
:exc:`FileNotFoundError` or :exc:`PermissionError`.
Loading