Pty

class Pty(**properties: Any)

Superclasses: Object

Implemented Interfaces: Initable

Constructors:

Pty(**properties)
new_foreign_sync(fd:int, cancellable:Gio.Cancellable=None) -> Vte.Pty
new_sync(flags:Vte.PtyFlags, cancellable:Gio.Cancellable=None) -> Vte.Pty

Constructors

class Pty
classmethod new_foreign_sync(fd: int, cancellable: Cancellable | None = None) Pty

Creates a new Pty for the PTY master fd.

No entry will be made in the lastlog, utmp or wtmp system files.

Note that the newly created Pty will take ownership of fd and close it on finalize.

Parameters:
  • fd – a file descriptor to the PTY

  • cancellable – a Cancellable, or None

classmethod new_sync(flags: PtyFlags, cancellable: Cancellable | None = None) Pty

Allocates a new pseudo-terminal.

You can later use fork() or the spawn_async() family of functions to start a process on the PTY.

If using fork(), you MUST call child_setup() in the child.

If using spawn_async() and friends, you MUST either use child_setup() directly as the child setup function, or call child_setup() from your own child setup function supplied.

When using spawn_sync() with a custom child setup function, child_setup() will be called before the supplied function; you must not call it again.

Also, you MUST pass the %G_SPAWN_DO_NOT_REAP_CHILD flag.

Note also that %G_SPAWN_STDOUT_TO_DEV_NULL, %G_SPAWN_STDERR_TO_DEV_NULL, and %G_SPAWN_CHILD_INHERITS_STDIN are not supported, since stdin, stdout and stderr of the child process will always be connected to the PTY.

Note that you should set the PTY’s size using set_size() before spawning the child process, so that the child process has the correct size from the start instead of starting with a default size and then shortly afterwards receiving a <literal>SIGWINCH</literal> signal. You should prefer using pty_new_sync() which does this automatically.

Parameters:

Methods

class Pty
child_setup() None
close() None

Since 0.42 this is a no-op.

Deprecated since version 0.42: Please do not use it in newly written code

get_fd() int
get_size() tuple[bool, int, int]

Reads the pseudo terminal’s window size.

If getting the window size failed, error will be set to a IOError.

set_size(rows: int, columns: int) bool

Attempts to resize the pseudo terminal’s window size. If successful, the OS kernel will send <literal>SIGWINCH</literal> to the child process group.

If setting the window size failed, error will be set to a IOError.

Parameters:
  • rows – the desired number of rows

  • columns – the desired number of columns

set_utf8(utf8: bool) bool

Tells the kernel whether the terminal is UTF-8 or not, in case it can make use of the info. Linux 2.6.5 or so defines IUTF8 to make the line discipline do multibyte backspace correctly.

Parameters:

utf8 – whether or not the pty is in UTF-8 mode

spawn_async(working_directory: str | None, argv: Sequence[str], envv: Sequence[str] | None, spawn_flags: SpawnFlags, child_setup: Callable[[...], None] | None, timeout: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

Like spawn_with_fds_async(), except that this function does not allow passing file descriptors to the child process. See spawn_with_fds_async() for more information.

Added in version 0.48.

Parameters:
  • working_directory – the name of a directory the command should start in, or None to use the current working directory

  • argv – child’s argument vector

  • envv – a list of environment variables to be added to the environment before starting the process, or None

  • spawn_flags – flags from SpawnFlags

  • child_setup – an extra child setup function to run in the child just before exec(), or None

  • timeout – a timeout value in ms, -1 for the default timeout, or G_MAXINT to wait indefinitely

  • cancellable – a Cancellable, or None

  • callback – a AsyncReadyCallback, or None

  • user_data – user data for callback

spawn_finish(result: AsyncResult) tuple[bool, int]

Added in version 0.48.

Parameters:

result – a AsyncResult

spawn_with_fds_async(working_directory: str | None, argv: Sequence[str], envv: Sequence[str] | None, fds: Sequence[int] | None, map_fds: Sequence[int] | None, spawn_flags: SpawnFlags, child_setup: Callable[[...], None] | None, timeout: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

Starts the specified command under the pseudo-terminal pty. The argv and envv lists should be None-terminated. The “TERM” environment variable is automatically set to a default value, but can be overridden from envv. pty_flags controls logging the session to the specified system log files.

Note also that %G_SPAWN_STDOUT_TO_DEV_NULL, %G_SPAWN_STDERR_TO_DEV_NULL, and %G_SPAWN_CHILD_INHERITS_STDIN are not supported in spawn_flags, since stdin, stdout and stderr of the child process will always be connected to the PTY. Also %G_SPAWN_LEAVE_DESCRIPTORS_OPEN is not supported; and %G_SPAWN_DO_NOT_REAP_CHILD will always be added to spawn_flags.

If fds is not None, the child process will map the file descriptors from fds according to map_fds; n_map_fds must be less or equal to n_fds. This function will take ownership of the file descriptors in fds; you must not use or close them after this call. All file descriptors in fds must have the FD_CLOEXEC flag set on them; it will be unset in the child process before calling man:execve(2). Note also that no file descriptor may be mapped to stdin, stdout, or stderr (file descriptors 0, 1, or 2), since these will be assigned to the PTY. All open file descriptors apart from those mapped as above will be closed when execve() is called.

Beginning with 0.60, and on linux only, and unless SPAWN_NO_SYSTEMD_SCOPE is passed in spawn_flags, the newly created child process will be moved to its own systemd user scope; and if SPAWN_REQUIRE_SYSTEMD_SCOPE is passed, and creation of the systemd user scope fails, the whole spawn will fail. You can override the options used for the systemd user scope by providing a systemd override file for ‘vte-spawn-.scope’ unit. See man:systemd.unit(5) for further information.

See vte_pty_new(), and watch_child() for more information.

Added in version 0.62.

Parameters:
  • working_directory – the name of a directory the command should start in, or None to use the current working directory

  • argv – child’s argument vector

  • envv – a list of environment variables to be added to the environment before starting the process, or None

  • fds – an array of file descriptors, or None

  • map_fds – an array of integers, or None

  • spawn_flags – flags from SpawnFlags

  • child_setup – an extra child setup function to run in the child just before exec(), or None

  • timeout – a timeout value in ms, -1 for the default timeout, or G_MAXINT to wait indefinitely

  • cancellable – a Cancellable, or None

  • callback – a AsyncReadyCallback, or None

  • user_data – user data for callback

Properties

class Pty
props.fd: int

The file descriptor of the PTY master.

props.flags: PtyFlags

Flags.