GLib.IOChannel¶
record (struct)
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
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 IOChannel.new_file.
Once a GIOChannel has been created, it can be used in a generic
manner with the functions IOChannel.read_chars,
IOChannel.write_chars, IOChannel.seek_position,
and IOChannel.shutdown.
To add a GIOChannel to the main event loop, use io_add_watch or
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.
IOChannel.ref and 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
IOChannel.new_file.) Using io_add_watch or
GLib.io_add_watch_full increments a channel’s reference count.
The new functions IOChannel.read_chars,
IOChannel.read_line, IOChannel.read_line_string,
IOChannel.read_to_end, IOChannel.write_chars,
IOChannel.seek_position, and IOChannel.flush
should not be mixed with the deprecated functions
IOChannel.read, IOChannel.write, and
IOChannel.seek on the same channel.
Constructors¶
new_file¶
@classmethod
def new_file(cls, filename: str | bytes | os.PathLike[str] | os.PathLike[bytes], 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 IOChannel.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 filemode— One of "r", "w", "a", "r+", "w+", "a+". These have the same meaning as in fopen()
unix_new¶
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 IOChannel.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 IOChannel.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¶
close¶
:::warning Deprecated since 2.2 This API is deprecated. :::
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 IOChannel.unref.
flush¶
Flushes the write buffer for the GIOChannel.
get_buffer_condition¶
This function returns a GObject.IOCondition depending on whether there
is data to be read/space to write data in the internal buffers in
the IOChannel. Only the flags GObject.IOCondition.IN and GObject.IOCondition.OUT may be set.
get_buffer_size¶
Gets the buffer size.
get_buffered¶
Returns whether channel is buffered.
get_close_on_unref¶
Returns whether the file/socket/whatever associated with channel
will be closed when channel receives its final unref and is
destroyed. The default value of this is True for channels created
by IOChannel.new_file, and False for all other channels.
get_encoding¶
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¶
Gets the current flags for a IOChannel, including read-only
flags such as IOFlags.IS_READABLE.
The values of the flags IOFlags.IS_READABLE and 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 IOChannel.get_flags to update
the internal values of these flags.
get_line_term¶
This returns the string that IOChannel uses to determine
where in the file a line break occurs. A value of None
indicates autodetection. Since 2.84, the return value is always
nul-terminated.
init¶
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¶
:::warning Deprecated since 2.2 This API is deprecated. :::
Reads data from a IOChannel.
Parameters:
buf— a buffer to read the data into (which should be at least count bytes long)count— the number of bytes to read from theIOChannelbytes_read— returns the number of bytes actually read
read_chars¶
Replacement for IOChannel.read with the new API.
read_line¶
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 IOStatus.NORMAL.
read_line_string¶
Reads a line from a IOChannel, using a String as a buffer.
Parameters:
buffer— aStringinto which the line will be written. Ifbufferalready contains data, the old data will be overwritten.terminator_pos— location to store position of line terminator, orNone
read_to_end¶
Reads all the remaining data from the file.
read_unichar¶
Reads a Unicode character from channel.
This function cannot be called on a channel with None encoding.
ref¶
Increments the reference count of a IOChannel.
seek¶
:::warning Deprecated since 2.2 This API is deprecated. :::
Sets the current position in the IOChannel, similar to the standard
library function fseek().
Parameters:
offset— an offset, in bytes, which is added to the position specified bytypetype— the position in the file, which can beSeekType.CUR(the current position),SeekType.SET(the start of the file), orSeekType.END(the end of the file)
seek_position¶
Replacement for IOChannel.seek with the new API.
Parameters:
offset— The offset in bytes from the position specified bytypetype— aSeekType. The typeSeekType.CURis only allowed in those cases where a call toIOChannel.set_encodingis allowed. See the documentation forIOChannel.set_encodingfor details.
set_buffer_size¶
Sets the buffer size.
Parameters:
size— the size of the buffer, or 0 to let GLib pick a good size
set_buffered¶
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 IOStatus.EOF
not require such a flush. For write-only channels, a call to
IOChannel.flush is sufficient. For all other channels,
the buffers may be flushed by a call to IOChannel.seek_position.
This includes the possibility of seeking with seek type 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.
Parameters:
buffered— whether to set the channel buffered or unbuffered
set_close_on_unref¶
Whether to close the channel on the final unref of the IOChannel
data structure. The default value of this is True for channels
created by IOChannel.new_file, and False for all other channels.
Setting this flag to True for a channel you have already closed
can cause problems when the final reference to the IOChannel is dropped.
Parameters:
do_close— Whether to close the channel on the final unref of the GIOChannel data structure.
set_encoding¶
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:
-
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
IOChannel.seek_position. (This flushes all the internal buffers.) -
The current encoding is
Noneor UTF-8. -
One of the (new API) read functions has just returned
IOStatus.EOF(or, in the case ofIOChannel.read_to_end,IOStatus.NORMAL). -
One of the functions
IOChannel.read_charsorIOChannel.read_unicharhas returnedIOStatus.AGAINorIOStatus.ERROR. This may be useful in the case ofConvertError.ILLEGAL_SEQUENCE. Returning one of these statuses fromIOChannel.read_line,IOChannel.read_line_string, orIOChannel.read_to_enddoes not guarantee that the encoding can be changed.
Channels which do not meet one of the above conditions cannot call
IOChannel.seek_position with an offset of SeekType.CUR, and, if
they are "seekable", cannot call IOChannel.write_chars after
calling one of the API "read" functions.
Parameters:
encoding— the encoding type
set_flags¶
Sets the (writeable) flags in channel to (flags & IOFlags.SET_MASK).
Parameters:
flags— the flags to set on the IO channel
set_line_term¶
This sets the string that IOChannel uses to determine
where in the file a line break occurs.
Parameters:
line_term— The line termination string. UseNonefor 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.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¶
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 IOChannel.unref.
Parameters:
flush— ifTrue, flush pending
unix_get_fd¶
Returns the file descriptor of the IOChannel.
On Windows this function returns the file descriptor or socket of
the IOChannel.
unref¶
Decrements the reference count of a IOChannel.
write¶
:::warning Deprecated since 2.2 This API is deprecated. :::
Writes data to a IOChannel.
Parameters:
buf— the buffer containing the data to writecount— the number of bytes to writebytes_written— the number of bytes actually written
write_chars¶
Replacement for IOChannel.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 IOChannel.write_chars
may only be made on a channel from which data has been read in the
cases described in the documentation for IOChannel.set_encoding.
Parameters:
buf— a buffer to write data fromcount— the size of the buffer. If -1, the buffer is taken to be a nul-terminated string.
write_unichar¶
Writes a Unicode character to channel.
This function cannot be called on a channel with None encoding.
Parameters:
thechar— a character
Static functions¶
error_from_errno¶
Converts an errno error number to a IOChannelError.
Parameters:
en— anerrnoerror number, e.g.EINVAL