gh-132859: Run debugger scripts in their own namespaces (#132860)

Run debugger scripts in their own namespaces

Previously scripts injected by `sys.remote_exec` were run with the
globals of the `__main__` module. Instead, run each injected script
with an empty set of globals. If someone really wants to use the
`__main__` module's namespace, they can always `import __main__`.
This commit is contained in:
Matt Wozniski 2025-04-23 19:40:24 -04:00 committed by GitHub
parent 402dba2928
commit a94c7528b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 13 deletions

View file

@ -2125,6 +2125,19 @@ raise Exception("Remote script exception")
self.assertIn(b"Remote script exception", stderr)
self.assertEqual(stdout.strip(), b"Target process running...")
def test_new_namespace_for_each_remote_exec(self):
"""Test that each remote_exec call gets its own namespace."""
script = textwrap.dedent(
"""
assert globals() is not __import__("__main__").__dict__
print("Remote script executed successfully!")
"""
)
returncode, stdout, stderr = self._run_remote_exec_test(script)
self.assertEqual(returncode, 0)
self.assertEqual(stderr, b"")
self.assertIn(b"Remote script executed successfully", stdout)
def test_remote_exec_disabled_by_env(self):
"""Test remote exec is disabled when PYTHON_DISABLE_REMOTE_DEBUG is set"""
env = os.environ.copy()