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 aIOChannel
using modemode
. This channel will be closed when the last reference to it is dropped, so there is no need to callclose()
(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 (seeget_charset()
) with theset_encoding()
function. By default, the fd passed will not be closed when the final reference to theIOChannel
data structure is dropped.If you want to read raw binary data without interpretation, then call the
set_encoding()
function withNone
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 aIOChannelError
.
- 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 theIOChannel
. Only the flagsIN
andOUT
may be set.
- 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 asIS_READABLE
.The values of the flags
IS_READABLE
andIS_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 callget_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 ofNone
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 ofIOChannel
).
- read(max_count=-1)#
Reads data from a
IOChannel
.Deprecated since version 2.2: Use
read_chars()
instead.- Parameters:
max_count
- 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 isNORMAL
.
- read_line_string(buffer: String, terminator_pos: int | None = None) IOStatus #
Reads a line from a
IOChannel
, using aString
as a buffer.- Parameters:
buffer – a
String
into which the line will be written. Ifbuffer
already contains data, the old data will be overwritten.terminator_pos – location to store position of line terminator, or
None
- read_unichar() tuple[IOStatus, str] #
Reads a Unicode character from
channel
. This function cannot be called on a channel withNone
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
- 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 typeCUR
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:
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
seek_position()
. (This flushes all the internal buffers.)The current encoding is
None
or UTF-8.One of the (new API) read functions has just returned
EOF
(or, in the case ofread_to_end()
,NORMAL
).- One of the functions
read_chars()
or read_unichar()
has returnedAGAIN
orERROR
. This may be useful in the case ofILLEGAL_SEQUENCE
. Returning one of these statuses fromread_line()
,read_line_string()
, orread_to_end()
does not guarantee that the encoding can be changed.
- One of the functions
Channels which do not meet one of the above conditions cannot call
seek_position()
with an offset ofCUR
, and, if they are “seekable”, cannot callwrite_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
isTrue
. The channel will not be freed until the last reference is dropped usingunref()
.- 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 withNone
encoding.- Parameters:
thechar – a character
- writelines(lines)#
- Parameters:
lines