Skip to content
Open
20 changes: 20 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,26 @@ always available. Unless explicitly noted otherwise, all variables are read-only
interpreter is pre-release (alpha, beta, or release candidate) then the
local and remote interpreters must be the same exact version.

Note that the remote process must be able to read the temporary script file in terms
of permissions. If the caller is running under different user than the process,
the caller should grant permissions (typically ``os.fchmod(filename, 0o644)``).

Callers should adjust permissions before calling, for example::

import os
import sys
import tempfile

with tempfile.NamedTemporaryFile(
mode='w',
suffix='.py',
delete_on_close=False,
Copy link
Member

@picnixz picnixz Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I am sorry about this suggestion. I thought it would have been enough but I think we need to do revert to your original suggestion with os.unlink(). However, we may need a try-finally + catch OSError for the unlink failure:

file = tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False)
try:
    with file:
        ...
finally:
    try:
        os.unlink(file.name)
    except OSError:
        pass

Copy link
Member

@picnixz picnixz Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember that sometimes on Windows we have issues with automatic deletion. But altenratively, we could just have a context manager and keep the defaults:

with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as f:
    ...

) as f:
f.write("print('Hello from remote!')")
f.flush()
os.chmod(f.name, 0o644) # Readable by group/other
sys.remote_exec(pid, f.name)

See :ref:`remote-debugging` for more information about the remote debugging
mechanism.

Expand Down
Loading