:right-sidebar: True IOChannel =================================================================== .. currentmodule:: gi.repository.GLib .. class:: IOChannel(*args, **kwargs) :no-contents-entry: The ``GIOChannel`` data type aims to provide a portable method for using file descriptors, pipes, and sockets, and integrating them into the main event loop (see :obj:`~gi.repository.GLib.MainContext`\). Currently, full support is available on UNIX platforms; support for Windows is only partially complete. To create a new ``GIOChannel`` on UNIX systems use :obj:`~gi.repository.GLib.IOChannel.unix_new`\. This works for plain file descriptors, pipes and sockets. Alternatively, a channel can be created for a file in a system independent manner using :obj:`~gi.repository.GLib.IOChannel.new_file`\. Once a ``GIOChannel`` has been created, it can be used in a generic manner with the functions :obj:`~gi.repository.GLib.IOChannel.read_chars`\, :obj:`~gi.repository.GLib.IOChannel.write_chars`\, :obj:`~gi.repository.GLib.IOChannel.seek_position`\, and :obj:`~gi.repository.GLib.IOChannel.shutdown`\. To add a ``GIOChannel`` to the main event loop, use :obj:`~gi.repository.GLib.io_add_watch` or :obj:`~gi.repository.GLib.io_add_watch_full`\. Here you specify which events you are interested in on the ``GIOChannel``\, and provide a function to be called whenever these events occur. ``GIOChannel`` instances are created with an initial reference count of 1. :obj:`~gi.repository.GLib.IOChannel.ref` and :obj:`~gi.repository.GLib.IOChannel.unref` can be used to increment or decrement the reference count respectively. When the reference count falls to 0, the ``GIOChannel`` is freed. (Though it isn’t closed automatically, unless it was created using :obj:`~gi.repository.GLib.IOChannel.new_file`\.) Using :obj:`~gi.repository.GLib.io_add_watch` or :obj:`~gi.repository.GLib.io_add_watch_full` increments a channel’s reference count. The new functions :obj:`~gi.repository.GLib.IOChannel.read_chars`\, :obj:`~gi.repository.GLib.IOChannel.read_line`\, :obj:`~gi.repository.GLib.IOChannel.read_line_string`\, :obj:`~gi.repository.GLib.IOChannel.read_to_end`\, :obj:`~gi.repository.GLib.IOChannel.write_chars`\, :obj:`~gi.repository.GLib.IOChannel.seek_position`\, and :obj:`~gi.repository.GLib.IOChannel.flush` should not be mixed with the deprecated functions :obj:`~gi.repository.GLib.IOChannel.read`\, :obj:`~gi.repository.GLib.IOChannel.write`\, and :obj:`~gi.repository.GLib.IOChannel.seek` on the same channel. Constructors ------------ .. rst-class:: interim-class .. class:: IOChannel :no-index: .. classmethod:: new_file(filename: str, mode: str) -> ~gi.repository.GLib.IOChannel Open a file ``filename`` as a :obj:`~gi.repository.GLib.IOChannel` using mode ``mode``\. This channel will be closed when the last reference to it is dropped, so there is no need to call :func:`~gi.repository.GLib.IOChannel.close` (though doing so will not cause problems, as long as no attempt is made to access the channel after it is closed). :param filename: A string containing the name of a file :param mode: One of "r", "w", "a", "r+", "w+", "a+". These have the same meaning as in fopen() .. classmethod:: unix_new(fd: int) -> ~gi.repository.GLib.IOChannel Creates a new :obj:`~gi.repository.GLib.IOChannel` given a file descriptor. On UNIX systems this works for plain files, pipes, and sockets. The returned :obj:`~gi.repository.GLib.IOChannel` has a reference count of 1. The default encoding for :obj:`~gi.repository.GLib.IOChannel` is UTF-8. If your application is reading output from a command using via pipe, you may need to set the encoding to the encoding of the current locale (see :func:`~gi.repository.GLib.get_charset`) with the :func:`~gi.repository.GLib.IOChannel.set_encoding` function. By default, the fd passed will not be closed when the final reference to the :obj:`~gi.repository.GLib.IOChannel` data structure is dropped. If you want to read raw binary data without interpretation, then call the :func:`~gi.repository.GLib.IOChannel.set_encoding` function with :const:`None` for the encoding argument. This function is available in GLib on Windows, too, but you should avoid using it on Windows. The domain of file descriptors and sockets overlap. There is no way for GLib to know which one you mean in case the argument you pass to this function happens to be both a valid file descriptor and socket. If that happens a warning is issued, and GLib assumes that it is the file descriptor you mean. :param fd: a file descriptor. Methods ------- .. rst-class:: interim-class .. class:: IOChannel :no-index: .. method:: add_watch(condition, callback, *user_data, priority=0) :param condition: :param callback: :param user_data: :param priority: .. method:: close() -> None Close an IO channel. Any pending data to be written will be flushed, ignoring errors. The channel will not be freed until the last reference is dropped using :func:`~gi.repository.GLib.IOChannel.unref`. .. deprecated:: 2.2 Use :func:`~gi.repository.GLib.IOChannel.shutdown` instead. :return: :const:`True` on success, :const:`False` if there was an error. .. classmethod:: error_from_errno() -> ~gi.repository.GLib.IOChannelError Converts an ``errno`` error number to a :obj:`~gi.repository.GLib.IOChannelError`\. .. classmethod:: error_quark() -> int .. method:: flush() -> ~gi.repository.GLib.IOStatus Flushes the write buffer for the GIOChannel. .. method:: get_buffer_condition() -> ~gi.repository.GLib.IOCondition This function returns a :obj:`~gi.repository.GLib.IOCondition` depending on whether there is data to be read/space to write data in the internal buffers in the :obj:`~gi.repository.GLib.IOChannel`\. Only the flags :const:`~gi.repository.GLib.IOCondition.IN` and :const:`~gi.repository.GLib.IOCondition.OUT` may be set. .. method:: get_buffer_size() -> int Gets the buffer size. .. method:: get_buffered() -> bool Returns whether ``channel`` is buffered. .. method:: get_encoding() -> str Gets the encoding for the input/output of the channel. The internal encoding is always UTF-8. The encoding :const:`None` makes the channel safe for binary data. .. method:: get_flags() -> ~gi.repository.GLib.IOFlags Gets the current flags for a :obj:`~gi.repository.GLib.IOChannel`\, including read-only flags such as :const:`~gi.repository.GLib.IOFlags.IS_READABLE`. The values of the flags :const:`~gi.repository.GLib.IOFlags.IS_READABLE` and :const:`~gi.repository.GLib.IOFlags.IS_WRITABLE` are cached for internal use by the channel when it is created. If they should change at some later point (e.g. partial shutdown of a socket with the UNIX shutdown() function), the user should immediately call :func:`~gi.repository.GLib.IOChannel.get_flags` to update the internal values of these flags. .. method:: get_line_term() -> tuple[str, int] This returns the string that :obj:`~gi.repository.GLib.IOChannel` uses to determine where in the file a line break occurs. A value of :const:`None` indicates autodetection. .. method:: init() -> None Initializes a :obj:`~gi.repository.GLib.IOChannel` struct. This is called by each of the above functions when creating a :obj:`~gi.repository.GLib.IOChannel`\, and so is not often needed by the application programmer (unless you are creating a new type of :obj:`~gi.repository.GLib.IOChannel`\). .. method:: read(max_count=-1) Reads data from a :obj:`~gi.repository.GLib.IOChannel`\. .. deprecated:: 2.2 Use :func:`~gi.repository.GLib.IOChannel.read_chars` instead. :param max_count: .. method:: read_chars() -> tuple[~gi.repository.GLib.IOStatus, list[int], int] Replacement for :func:`~gi.repository.GLib.IOChannel.read` with the new API. .. method:: read_line() -> tuple[~gi.repository.GLib.IOStatus, str, int, int] Reads a line, including the terminating character(s), from a :obj:`~gi.repository.GLib.IOChannel` into a newly-allocated string. ``str_return`` will contain allocated memory if the return is :const:`~gi.repository.GLib.IOStatus.NORMAL`. .. method:: read_line_string(buffer: ~gi.repository.GLib.String, terminator_pos: int | None = None) -> ~gi.repository.GLib.IOStatus Reads a line from a :obj:`~gi.repository.GLib.IOChannel`\, using a :obj:`~gi.repository.GLib.String` as a buffer. :param buffer: a :obj:`~gi.repository.GLib.String` into which the line will be written. If ``buffer`` already contains data, the old data will be overwritten. :param terminator_pos: location to store position of line terminator, or :const:`None` .. method:: read_to_end() -> tuple[~gi.repository.GLib.IOStatus, list[int]] Reads all the remaining data from the file. .. method:: read_unichar() -> tuple[~gi.repository.GLib.IOStatus, str] Reads a Unicode character from ``channel``\. This function cannot be called on a channel with :const:`None` encoding. .. method:: readline(size_hint=-1) :param size_hint: .. method:: readlines(size_hint=-1) :param size_hint: .. method:: seek(offset, whence=0) Sets the current position in the :obj:`~gi.repository.GLib.IOChannel`\, similar to the standard library function fseek(). .. deprecated:: 2.2 Use :func:`~gi.repository.GLib.IOChannel.seek_position` instead. :param offset: an offset, in bytes, which is added to the position specified by ``type`` :param whence: .. method:: seek_position(offset: int, type: ~gi.repository.GLib.SeekType) -> ~gi.repository.GLib.IOStatus Replacement for :func:`~gi.repository.GLib.IOChannel.seek` with the new API. :param offset: The offset in bytes from the position specified by ``type`` :param type: a :obj:`~gi.repository.GLib.SeekType`\. The type :const:`~gi.repository.GLib.SeekType.CUR` is only allowed in those cases where a call to g_io_channel_set_encoding () is allowed. See the documentation for g_io_channel_set_encoding () for details. .. method:: set_buffer_size(size: int) -> None Sets the buffer size. :param size: the size of the buffer, or 0 to let GLib pick a good size .. method:: set_buffered(buffered: bool) -> None The buffering state can only be set if the channel's encoding is :const:`None`. For any other encoding, the channel must be buffered. A buffered channel can only be set unbuffered if the channel's internal buffers have been flushed. Newly created channels or channels which have returned :const:`~gi.repository.GLib.IOStatus.EOF` not require such a flush. For write-only channels, a call to g_io_channel_flush () is sufficient. For all other channels, the buffers may be flushed by a call to g_io_channel_seek_position (). This includes the possibility of seeking with seek type :const:`~gi.repository.GLib.SeekType.CUR` and an offset of zero. Note that this means that socket-based channels cannot be set unbuffered once they have had data read from them. On unbuffered channels, it is safe to mix read and write calls from the new and old APIs, if this is necessary for maintaining old code. The default state of the channel is buffered. :param buffered: whether to set the channel buffered or unbuffered .. method:: set_encoding(encoding: str | None = None) -> ~gi.repository.GLib.IOStatus Sets the encoding for the input/output of the channel. The internal encoding is always UTF-8. The default encoding for the external file is UTF-8. The encoding :const:`None` is safe to use with binary data. The encoding can only be set if one of the following conditions is true: - The channel was just created, and has not been written to or read from yet. - The channel is write-only. - The channel is a file, and the file pointer was just repositioned by a call to :func:`~gi.repository.GLib.IOChannel.seek_position`. (This flushes all the internal buffers.) - The current encoding is :const:`None` or UTF-8. - One of the (new API) read functions has just returned :const:`~gi.repository.GLib.IOStatus.EOF` (or, in the case of :func:`~gi.repository.GLib.IOChannel.read_to_end`, :const:`~gi.repository.GLib.IOStatus.NORMAL`). - One of the functions :func:`~gi.repository.GLib.IOChannel.read_chars` or :func:`~gi.repository.GLib.IOChannel.read_unichar` has returned :const:`~gi.repository.GLib.IOStatus.AGAIN` or :const:`~gi.repository.GLib.IOStatus.ERROR`. This may be useful in the case of :const:`~gi.repository.GLib.ConvertError.ILLEGAL_SEQUENCE`. Returning one of these statuses from :func:`~gi.repository.GLib.IOChannel.read_line`, :func:`~gi.repository.GLib.IOChannel.read_line_string`, or :func:`~gi.repository.GLib.IOChannel.read_to_end` does not guarantee that the encoding can be changed. Channels which do not meet one of the above conditions cannot call :func:`~gi.repository.GLib.IOChannel.seek_position` with an offset of :const:`~gi.repository.GLib.SeekType.CUR`, and, if they are "seekable", cannot call :func:`~gi.repository.GLib.IOChannel.write_chars` after calling one of the API "read" functions. :param encoding: the encoding type .. method:: set_flags(flags: ~gi.repository.GLib.IOFlags) -> ~gi.repository.GLib.IOStatus Sets the (writeable) flags in ``channel`` to (``flags`` & :const:`~gi.repository.GLib.IOFlags.SET_MASK`). :param flags: the flags to set on the IO channel .. method:: set_line_term(line_term: str | None, length: int) -> None This sets the string that :obj:`~gi.repository.GLib.IOChannel` uses to determine where in the file a line break occurs. :param line_term: The line termination string. Use :const:`None` for autodetect. Autodetection breaks on "\n", "\r\n", "\r", "\0", and the Unicode paragraph separator. Autodetection should not be used for anything other than file-based channels. :param length: The length of the termination string. If -1 is passed, the string is assumed to be nul-terminated. This option allows termination strings with embedded nuls. .. method:: shutdown(flush: bool) -> ~gi.repository.GLib.IOStatus Close an IO channel. Any pending data to be written will be flushed if ``flush`` is :const:`True`. The channel will not be freed until the last reference is dropped using :func:`~gi.repository.GLib.IOChannel.unref`. :param flush: if :const:`True`, flush pending .. method:: unix_get_fd() -> int Returns the file descriptor of the :obj:`~gi.repository.GLib.IOChannel`\. On Windows this function returns the file descriptor or socket of the :obj:`~gi.repository.GLib.IOChannel`\. .. method:: write(buf, buflen=-1) Writes data to a :obj:`~gi.repository.GLib.IOChannel`\. .. deprecated:: 2.2 Use :func:`~gi.repository.GLib.IOChannel.write_chars` instead. :param buf: the buffer containing the data to write :param buflen: .. method:: write_chars(buf: list[int], count: int) -> tuple[~gi.repository.GLib.IOStatus, int] Replacement for :func:`~gi.repository.GLib.IOChannel.write` with the new API. On seekable channels with encodings other than :const:`None` or UTF-8, generic mixing of reading and writing is not allowed. A call to g_io_channel_write_chars () may only be made on a channel from which data has been read in the cases described in the documentation for g_io_channel_set_encoding (). :param buf: a buffer to write data from :param count: the size of the buffer. If -1, the buffer is taken to be a nul-terminated string. .. method:: write_unichar(thechar: str) -> ~gi.repository.GLib.IOStatus Writes a Unicode character to ``channel``\. This function cannot be called on a channel with :const:`None` encoding. :param thechar: a character .. method:: writelines(lines) :param lines: Fields ------ .. rst-class:: interim-class .. class:: IOChannel :no-index: .. attribute:: buf_size .. attribute:: close_on_unref .. attribute:: do_encode .. attribute:: encoded_read_buf .. attribute:: encoding .. attribute:: funcs .. attribute:: is_readable .. attribute:: is_seekable .. attribute:: is_writeable .. attribute:: line_term .. attribute:: line_term_len .. attribute:: partial_write_buf .. attribute:: read_buf .. attribute:: read_cd .. attribute:: ref_count .. attribute:: reserved1 .. attribute:: reserved2 .. attribute:: use_buffer .. attribute:: write_buf .. attribute:: write_cd