IOChannel#

class IOChannel(*args, **kwargs)#

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 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 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 new_file.

Once a GIOChannel has been created, it can be used in a generic manner with the functions read_chars, write_chars, seek_position, and shutdown.

To add a GIOChannel to the main event loop, use io_add_watch or 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. ref and 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 new_file.) Using io_add_watch or io_add_watch_full increments a channel’s reference count.

The new functions read_chars, read_line, read_line_string, read_to_end, write_chars, seek_position, and flush should not be mixed with the deprecated functions read, write, and seek on the same channel.

Constructors#

class IOChannel
classmethod new_file(filename: str, mode: str) IOChannel#

Open a file filename as a IOChannel using mode mode. This channel will be closed when the last reference to it is dropped, so there is no need to call close() (though doing so will not cause problems, as long as no attempt is made to access the channel after it is closed).

Parameters:
  • filename – A string containing the name of a file

  • mode – One of “r”, “w”, “a”, “r+”, “w+”, “a+”. These have the same meaning as in fopen()

classmethod unix_new(fd: int) IOChannel#

Creates a new IOChannel given a file descriptor. On UNIX systems this works for plain files, pipes, and sockets.

The returned IOChannel has a reference count of 1.

The default encoding for 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 get_charset()) with the set_encoding() function. By default, the fd passed will not be closed when the final reference to the IOChannel data structure is dropped.

If you want to read raw binary data without interpretation, then call the set_encoding() function with 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.

Parameters:

fd – a file descriptor.

Methods#

class IOChannel
add_watch(condition, callback, *user_data, priority=0)#
Parameters:
  • condition

  • callback

  • user_data

  • priority

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 unref().

Deprecated since version 2.2: Use shutdown() instead.

Returns:

True on success, False if there was an error.

classmethod error_from_errno() IOChannelError#

Converts an errno error number to a IOChannelError.

classmethod error_quark() int#
flush() IOStatus#

Flushes the write buffer for the GIOChannel.

get_buffer_condition() IOCondition#

This function returns a IOCondition depending on whether there is data to be read/space to write data in the internal buffers in the IOChannel. Only the flags IN and OUT may be set.

get_buffer_size() int#

Gets the buffer size.

get_buffered() bool#

Returns whether channel is buffered.

get_encoding() str#

Gets the encoding for the input/output of the channel. The internal encoding is always UTF-8. The encoding None makes the channel safe for binary data.

get_flags() IOFlags#

Gets the current flags for a IOChannel, including read-only flags such as IS_READABLE.

The values of the flags IS_READABLE and 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 get_flags() to update the internal values of these flags.

get_line_term() tuple[str, int]#

This returns the string that IOChannel uses to determine where in the file a line break occurs. A value of None indicates autodetection.

init() None#

Initializes a IOChannel struct.

This is called by each of the above functions when creating a IOChannel, and so is not often needed by the application programmer (unless you are creating a new type of IOChannel).

read(max_count=-1)#

Reads data from a IOChannel.

Deprecated since version 2.2: Use read_chars() instead.

Parameters:

max_count

read_chars() tuple[IOStatus, list[int], int]#

Replacement for read() with the new API.

read_line() tuple[IOStatus, str, int, int]#

Reads a line, including the terminating character(s), from a IOChannel into a newly-allocated string. str_return will contain allocated memory if the return is NORMAL.

read_line_string(buffer: String, terminator_pos: int | None = None) IOStatus#

Reads a line from a IOChannel, using a String as a buffer.

Parameters:
  • buffer – a String into which the line will be written. If buffer already contains data, the old data will be overwritten.

  • terminator_pos – location to store position of line terminator, or None

read_to_end() tuple[IOStatus, list[int]]#

Reads all the remaining data from the file.

read_unichar() tuple[IOStatus, str]#

Reads a Unicode character from channel. This function cannot be called on a channel with None encoding.

readline(size_hint=-1)#
Parameters:

size_hint

readlines(size_hint=-1)#
Parameters:

size_hint

seek(offset, whence=0)#

Sets the current position in the IOChannel, similar to the standard library function fseek().

Deprecated since version 2.2: Use seek_position() instead.

Parameters:
  • offset – an offset, in bytes, which is added to the position specified by type

  • whence

seek_position(offset: int, type: SeekType) IOStatus#

Replacement for seek() with the new API.

Parameters:
  • offset – The offset in bytes from the position specified by type

  • type – a SeekType. The type 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.

set_buffer_size(size: int) None#

Sets the buffer size.

Parameters:

size – the size of the buffer, or 0 to let GLib pick a good size

set_buffered(buffered: bool) None#

The buffering state can only be set if the channel’s encoding is 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 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 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.

Parameters:

buffered – whether to set the channel buffered or unbuffered

set_encoding(encoding: str | None = None) 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 None is safe to use with binary data.

The encoding can only be set if one of the following conditions is true:

Channels which do not meet one of the above conditions cannot call seek_position() with an offset of CUR, and, if they are “seekable”, cannot call write_chars() after calling one of the API “read” functions.

Parameters:

encoding – the encoding type

set_flags(flags: IOFlags) IOStatus#

Sets the (writeable) flags in channel to (flags & SET_MASK).

Parameters:

flags – the flags to set on the IO channel

set_line_term(line_term: str | None, length: int) None#

This sets the string that IOChannel uses to determine where in the file a line break occurs.

Parameters:
  • line_term – The line termination string. Use None for autodetect. Autodetection breaks on “n”, “rn”, “r”, “0”, and the Unicode paragraph separator. Autodetection should not be used for anything other than file-based channels.

  • 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.

shutdown(flush: bool) IOStatus#

Close an IO channel. Any pending data to be written will be flushed if flush is True. The channel will not be freed until the last reference is dropped using unref().

Parameters:

flush – if True, flush pending

unix_get_fd() int#

Returns the file descriptor of the IOChannel.

On Windows this function returns the file descriptor or socket of the IOChannel.

write(buf, buflen=-1)#

Writes data to a IOChannel.

Deprecated since version 2.2: Use write_chars() instead.

Parameters:
  • buf – the buffer containing the data to write

  • buflen

write_chars(buf: list[int], count: int) tuple[IOStatus, int]#

Replacement for write() with the new API.

On seekable channels with encodings other than 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 ().

Parameters:
  • buf – a buffer to write data from

  • count – the size of the buffer. If -1, the buffer is taken to be a nul-terminated string.

write_unichar(thechar: str) IOStatus#

Writes a Unicode character to channel. This function cannot be called on a channel with None encoding.

Parameters:

thechar – a character

writelines(lines)#
Parameters:

lines

Fields#

class IOChannel
buf_size#
close_on_unref#
do_encode#
encoded_read_buf#
encoding#
funcs#
is_readable#
is_seekable#
is_writeable#
line_term#
line_term_len#
partial_write_buf#
read_buf#
read_cd#
ref_count#
reserved1#
reserved2#
use_buffer#
write_buf#
write_cd#