Skip to content

Soft deprecate os.spawn*(), os.popen() and os.system() functions #120743

Closed
@vstinner

Description

@vstinner

See the discussion for the rationale: Is time to remove os module spawn* functions?

See also the discussion: How to deal with unsafe/broken os.spawn* arg handling behavior on Windows (Nov 2022).

Advantages of the subprocess module:

  • subprocess restores the signal handlers (reliable behavior).
  • subprocess closes all file descriptors (more secure).
  • subprocess doesn't use a shell by default, shell=True must be used explicitly.
  • subprocess is the defacto standard way to spawn processes in Python.

Examples of os functions issues:

  • os.popen() and os.system() always use shell=True, it cannot be disabled. (higher risk of shell code injection)
  • os.popen().close() return value depends on the platform. (not portable)
  • os.popen().close() return value is a "wait status", not an exit code: waitstatus_to_exitcode() must be used to get a return code.
  • There are 8 os.spawn*() functions, it's more error prone and harder to use than subprocess APIs.
  • os.spawn*() functions return an exit code 1 if the program is not found instead of raising an exception. It's harder to distinguish if the program exists and has an exit code of 1, or if it doesn't exist.
  • The os.spawn*() functions are not safe for use in multithreaded programs on POSIX systems as they use fork()+exec() from Python there.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions