GLib.IOChannel¶
Fields¶
Name |
Type |
Access |
Description |
---|---|---|---|
buf_size |
r |
||
close_on_unref |
r |
||
do_encode |
r |
||
encoded_read_buf |
r |
||
encoding |
r |
||
funcs |
r |
||
is_readable |
r |
||
is_seekable |
r |
||
is_writeable |
r |
||
line_term |
r |
||
line_term_len |
r |
||
partial_write_buf |
[ |
r |
|
read_buf |
r |
||
read_cd |
r |
||
ref_count |
r |
||
reserved1 |
r |
||
reserved2 |
r |
||
use_buffer |
r |
||
write_buf |
r |
||
write_cd |
r |
Methods¶
class |
|
class |
|
class |
|
class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Details¶
- class GLib.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 [struct`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 [ctor`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 [ctor`GLib`.IOChannel.new_file].Once a
GIOChannel
has been created, it can be used in a generic manner with the functions [method`GLib`.IOChannel.read_chars], [method`GLib`.IOChannel.write_chars], [method`GLib`.IOChannel.seek_position], and [method`GLib`.IOChannel.shutdown].To add a
GIOChannel
to the main event loop, use [func`GLib`.io_add_watch] or [func`GLib`.io_add_watch_full]. Here you specify which events you are interested in on theGIOChannel
, and provide a function to be called whenever these events occur.GIOChannel
instances are created with an initial reference count of 1. [method`GLib`.IOChannel.ref] and [method`GLib`.IOChannel.unref] can be used to increment or decrement the reference count respectively. When the reference count falls to 0, theGIOChannel
is freed. (Though it isn’t closed automatically, unless it was created using [ctor`GLib`.IOChannel.new_file].) Using [func`GLib`.io_add_watch] or [func`GLib`.io_add_watch_full] increments a channel’s reference count.The new functions [method`GLib`.IOChannel.read_chars], [method`GLib`.IOChannel.read_line], [method`GLib`.IOChannel.read_line_string], [method`GLib`.IOChannel.read_to_end], [method`GLib`.IOChannel.write_chars], [method`GLib`.IOChannel.seek_position], and [method`GLib`.IOChannel.flush] should not be mixed with the deprecated functions [method`GLib`.IOChannel.read], [method`GLib`.IOChannel.write], and [method`GLib`.IOChannel.seek] on the same channel.
- classmethod error_from_errno(en)[source]¶
- Parameters:
en (
int
) – anerrno
error number, e.g.EINVAL
- Returns:
a
GLib.IOChannelError
error number, e.g.GLib.IOChannelError.INVAL
.- Return type:
Converts an
errno
error number to aGLib.IOChannelError
.
- classmethod new_file(filename, mode)[source]¶
- Parameters:
- Raises:
- Returns:
A
GLib.IOChannel
on success,None
on failure.- Return type:
Open a file filename as a
GLib.IOChannel
using mode mode. This channel will be closed when the last reference to it is dropped, so there is no need to callGLib.IOChannel.close
() (though doing so will not cause problems, as long as no attempt is made to access the channel after it is closed).
- classmethod unix_new(fd)[source]¶
- Parameters:
fd (
int
) – a file descriptor.- Returns:
a new
GLib.IOChannel
.- Return type:
Creates a new
GLib.IOChannel
given a file descriptor. On UNIX systems this works for plain files, pipes, and sockets.The returned
GLib.IOChannel
has a reference count of 1.The default encoding for
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 (seeGLib.get_charset
()) with theGLib.IOChannel.set_encoding
() function. By default, the fd passed will not be closed when the final reference to theGLib.IOChannel
data structure is dropped.If you want to read raw binary data without interpretation, then call the
GLib.IOChannel.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.
- add_watch(condition, callback, *user_data, **kwargs)¶
- close()[source]¶
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
GLib.IOChannel.unref
().Deprecated since version 2.2: Use
GLib.IOChannel.shutdown
() instead.
- flush()[source]¶
- Raises:
- Returns:
the status of the operation: One of
GLib.IOStatus.NORMAL
,GLib.IOStatus.AGAIN
, orGLib.IOStatus.ERROR
.- Return type:
Flushes the write buffer for the
GLib.IOChannel
.
- get_buffer_condition()[source]¶
- Returns:
- Return type:
This function returns a
GLib.IOCondition
depending on whether there is data to be read/space to write data in the internal buffers in theGLib.IOChannel
. Only the flagsGLib.IOCondition.IN
andGLib.IOCondition.OUT
may be set.
- get_close_on_unref()[source]¶
-
Returns whether the file/socket/whatever associated with self will be closed when self receives its final unref and is destroyed. The default value of this is
True
for channels created byGLib.IOChannel.new_file
(), andFalse
for all other channels.
- get_encoding()[source]¶
- Returns:
A string containing the encoding, this string is owned by GLib and must not be freed.
- Return type:
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()[source]¶
- Returns:
the flags which are set on the channel
- Return type:
Gets the current flags for a
GLib.IOChannel
, including read-only flags such asGLib.IOFlags.IS_READABLE
.The values of the flags
GLib.IOFlags.IS_READABLE
andGLib.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 callGLib.IOChannel.get_flags
() to update the internal values of these flags.
- get_line_term()[source]¶
- Returns:
The line termination string. This value is owned by GLib and must not be freed.
- length:
a location to return the length of the line terminator
- Return type:
This returns the string that
GLib.IOChannel
uses to determine where in the file a line break occurs. A value ofNone
indicates autodetection.
- init()[source]¶
Initializes a
GLib.IOChannel
struct.This is called by each of the above functions when creating a
GLib.IOChannel
, and so is not often needed by the application programmer (unless you are creating a new type ofGLib.IOChannel
).
- next()¶
- read(buf, count, bytes_read)[source]¶
- Parameters:
buf (
str
) – a buffer to read the data into (which should be at least count bytes long)count (
int
) – the number of bytes to read from theGLib.IOChannel
bytes_read (
int
) – returns the number of bytes actually read
- Returns:
GLib.IOError.NONE
if the operation was successful.- Return type:
Reads data from a
GLib.IOChannel
.Deprecated since version 2.2: Use
GLib.IOChannel.read_chars
() instead.
- read_chars()[source]¶
- Raises:
- Returns:
the status of the operation.
- buf:
a buffer to read data into
- bytes_read:
The number of bytes read. This may be zero even on success if count < 6 and the channel’s encoding is non-
None
. This indicates that the next UTF-8 character is too wide for the buffer.
- Return type:
(
GLib.IOStatus
, buf:bytes
, bytes_read:int
)
Replacement for
GLib.IOChannel.read
() with the new API.
- read_line()[source]¶
- Raises:
- Returns:
the status of the operation.
- str_return:
The line read from the
GLib.IOChannel
, including the line terminator. This data should be freed withGLib.free
() when no longer needed. This is a nul-terminated string. If a length of zero is returned, this will beNone
instead.- length:
location to store length of the read data, or
None
- terminator_pos:
location to store position of line terminator, or
None
- Return type:
(
GLib.IOStatus
, str_return:str
, length:int
, terminator_pos:int
)
Reads a line, including the terminating character(s), from a
GLib.IOChannel
into a newly-allocated string. str_return will contain allocated memory if the return isGLib.IOStatus.NORMAL
.
- read_line_string(buffer, terminator_pos)[source]¶
- Parameters:
buffer (
GLib.String
) – aGLib.String
into which the line will be written. If buffer already contains data, the old data will be overwritten.terminator_pos (
int
orNone
) – location to store position of line terminator, orNone
- Raises:
- Returns:
the status of the operation.
- Return type:
Reads a line from a
GLib.IOChannel
, using aGLib.String
as a buffer.
- read_to_end()[source]¶
- Raises:
- Returns:
GLib.IOStatus.NORMAL
on success. This function never returnsGLib.IOStatus.EOF
.- str_return:
Location to store a pointer to a string holding the remaining data in the
GLib.IOChannel
. This data should be freed withGLib.free
() when no longer needed. This data is terminated by an extra nul character, but there may be other nuls in the intervening data.
- Return type:
(
GLib.IOStatus
, str_return:bytes
)
Reads all the remaining data from the file.
- read_unichar()[source]¶
- Raises:
- Returns:
-
- thechar:
a location to return a character
- Return type:
(
GLib.IOStatus
, thechar:str
)
Reads a Unicode character from self. This function cannot be called on a channel with
None
encoding.
- readline(size_hint=-1)¶
- readlines(size_hint=-1)¶
- ref()[source]¶
- Returns:
the self that was passed in (since 2.6)
- Return type:
Increments the reference count of a
GLib.IOChannel
.
- seek(offset, type)[source]¶
- Parameters:
offset (
int
) – an offset, in bytes, which is added to the position specified by typetype (
GLib.SeekType
) – the position in the file, which can beGLib.SeekType.CUR
(the current position),GLib.SeekType.SET
(the start of the file), orGLib.SeekType.END
(the end of the file)
- Returns:
GLib.IOError.NONE
if the operation was successful.- Return type:
Sets the current position in the
GLib.IOChannel
, similar to the standard library function fseek().Deprecated since version 2.2: Use
GLib.IOChannel.seek_position
() instead.
- seek_position(offset, type)[source]¶
- Parameters:
offset (
int
) – The offset in bytes from the position specified by typetype (
GLib.SeekType
) – aGLib.SeekType
. The typeGLib.SeekType.CUR
is only allowed in those cases where a call toGLib.IOChannel.set_encoding
() is allowed. See the documentation forGLib.IOChannel.set_encoding
() for details.
- Raises:
- Returns:
the status of the operation.
- Return type:
Replacement for
GLib.IOChannel.seek
() with the new API.
- set_buffer_size(size)[source]¶
- Parameters:
size (
int
) – the size of the buffer, or 0 to let GLib pick a good size
Sets the buffer size.
- set_buffered(buffered)[source]¶
- Parameters:
buffered (
bool
) – whether to set the channel buffered or unbuffered
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
GLib.IOStatus.EOF
not require such a flush. For write-only channels, a call toGLib.IOChannel.flush
() is sufficient. For all other channels, the buffers may be flushed by a call toGLib.IOChannel.seek_position
(). This includes the possibility of seeking with seek typeGLib.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.
- set_close_on_unref(do_close)[source]¶
- Parameters:
do_close (
bool
) – Whether to close the channel on the final unref of theGLib.IOChannel
data structure.
Whether to close the channel on the final unref of the
GLib.IOChannel
data structure. The default value of this isTrue
for channels created byGLib.IOChannel.new_file
(), andFalse
for all other channels.Setting this flag to
True
for a channel you have already closed can cause problems when the final reference to theGLib.IOChannel
is dropped.
- set_encoding(encoding)[source]¶
- Parameters:
- Raises:
- Returns:
GLib.IOStatus.NORMAL
if the encoding was successfully set- Return type:
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
GLib.IOChannel.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
GLib.IOStatus.EOF
(or, in the case ofGLib.IOChannel.read_to_end
(),GLib.IOStatus.NORMAL
).One of the functions
GLib.IOChannel.read_chars
() orGLib.IOChannel.read_unichar
() has returnedGLib.IOStatus.AGAIN
orGLib.IOStatus.ERROR
. This may be useful in the case ofGLib.ConvertError.ILLEGAL_SEQUENCE
. Returning one of these statuses fromGLib.IOChannel.read_line
(),GLib.IOChannel.read_line_string
(), orGLib.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
GLib.IOChannel.seek_position
() with an offset ofGLib.SeekType.CUR
, and, if they are “seekable”, cannot callGLib.IOChannel.write_chars
() after calling one of the API “read” functions.
- set_flags(flags)[source]¶
- Parameters:
flags (
GLib.IOFlags
) – the flags to set on the IO channel- Raises:
- Returns:
the status of the operation.
- Return type:
Sets the (writeable) flags in self to (flags &
GLib.IOFlags.SET_MASK
).
- set_line_term(line_term, length)[source]¶
- Parameters:
line_term (
str
orNone
) – The line termination string. UseNone
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.length (
int
) – 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.
This sets the string that
GLib.IOChannel
uses to determine where in the file a line break occurs.
- shutdown(flush)[source]¶
- Parameters:
- Raises:
- Returns:
the status of the operation.
- Return type:
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 usingGLib.IOChannel.unref
().
- unix_get_fd()[source]¶
- Returns:
the file descriptor of the
GLib.IOChannel
.- Return type:
Returns the file descriptor of the
GLib.IOChannel
.On Windows this function returns the file descriptor or socket of the
GLib.IOChannel
.
- unref()[source]¶
Decrements the reference count of a
GLib.IOChannel
.
- write(buf, count, bytes_written)[source]¶
- Parameters:
- Returns:
GLib.IOError.NONE
if the operation was successful.- Return type:
Writes data to a
GLib.IOChannel
.Deprecated since version 2.2: Use
GLib.IOChannel.write_chars
() instead.
- write_chars(buf, count)[source]¶
- Parameters:
- Raises:
- Returns:
the status of the operation.
- bytes_written:
The number of bytes written. This can be nonzero even if the return value is not
GLib.IOStatus.NORMAL
. If the return value isGLib.IOStatus.NORMAL
and the channel is blocking, this will always be equal to count if count >= 0.
- Return type:
(
GLib.IOStatus
, bytes_written:int
)
Replacement for
GLib.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 toGLib.IOChannel.write_chars
() may only be made on a channel from which data has been read in the cases described in the documentation forGLib.IOChannel.set_encoding
().
- write_unichar(thechar)[source]¶
- Parameters:
thechar (
str
) – a character- Raises:
- Returns:
- Return type:
Writes a Unicode character to self. This function cannot be called on a channel with
None
encoding.
- writelines(lines)¶