GLib.IOChannel¶
Fields¶
Name | Type | Access | Description |
---|---|---|---|
buf_size | int |
r | |
close_on_unref | int |
r | |
do_encode | int |
r | |
encoded_read_buf | GLib.String |
r | |
encoding | str |
r | |
funcs | GLib.IOFuncs |
r | |
is_readable | int |
r | |
is_seekable | int |
r | |
is_writeable | int |
r | |
line_term | str |
r | |
line_term_len | int |
r | |
partial_write_buf | [int ] |
r | |
read_buf | GLib.String |
r | |
read_cd | object |
r | |
ref_count | int |
r | |
reserved1 | object |
r | |
reserved2 | object |
r | |
use_buffer | int |
r | |
write_buf | GLib.String |
r | |
write_cd | object |
r |
Methods¶
class | error_from_errno (en) |
class | error_quark () |
class | new_file (filename, mode) |
class | unix_new (fd) |
add_watch (condition, callback, *user_data, **kwargs) |
|
close () |
|
flush () |
|
get_buffer_condition () |
|
get_buffer_size () |
|
get_buffered () |
|
get_close_on_unref () |
|
get_encoding () |
|
get_flags () |
|
get_line_term (length) |
|
init () |
|
next () |
|
read (buf, count, bytes_read) |
|
read_chars () |
|
read_line () |
|
read_line_string (buffer, terminator_pos) |
|
read_to_end () |
|
read_unichar () |
|
readline (size_hint=-1) |
|
readlines (size_hint=-1) |
|
ref () |
|
seek (offset, type) |
|
seek_position (offset, type) |
|
set_buffer_size (size) |
|
set_buffered (buffered) |
|
set_close_on_unref (do_close) |
|
set_encoding (encoding) |
|
set_flags (flags) |
|
set_line_term (line_term, length) |
|
shutdown (flush) |
|
unix_get_fd () |
|
unref () |
|
write (buf, count, bytes_written) |
|
write_chars (buf, count) |
|
write_unichar (thechar) |
|
writelines (lines) |
Details¶
-
class
GLib.
IOChannel
(*args, **kwargs)¶ A data structure representing an IO Channel. The fields should be considered private and should only be accessed with the following functions.
-
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: GLib.IOChannelError
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: GLib.IOChannel
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: GLib.Error
Returns: the status of the operation: One of GLib.IOStatus.NORMAL
,GLib.IOStatus.AGAIN
, orGLib.IOStatus.ERROR
.Return type: GLib.IOStatus
Flushes the write buffer for the
GLib.IOChannel
.
-
get_buffer_condition
()[source]¶ Returns: A GLib.IOCondition
Return type: GLib.IOCondition
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_buffered
()[source]¶ Returns: True
if the self is buffered.Return type: bool
Returns whether self is buffered.
-
get_close_on_unref
()[source]¶ Returns: True
if the channel will be closed,False
otherwise.Return type: bool
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: 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
()[source]¶ Returns: the flags which are set on the channel Return type: GLib.IOFlags
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
(length)[source]¶ Parameters: length ( int
) – a location to return the length of the line terminatorReturns: The line termination string. This value is owned by GLib and must not be freed. Return type: str
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.- buf (
-
read_chars
()[source]¶ Raises: GLib.Error
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: GLib.Error
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.- buffer (
-
read_to_end
()[source]¶ Raises: GLib.Error
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: GLib.Error
Returns: a GLib.IOStatus
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: GLib.IOChannel
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 type - type (
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.- offset (
-
seek_position
(offset, type)[source]¶ Parameters: - offset (
int
) – The offset in bytes from the position specified by type - type (
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.- offset (
-
set_buffer_size
(size)[source]¶ Parameters: size ( int
) – the size of the buffer, or 0 to let GLib pick a good sizeSets the buffer size.
-
set_buffered
(buffered)[source]¶ Parameters: buffered ( bool
) – whether to set the channel buffered or unbufferedThe 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: encoding ( str
orNone
) – the encoding typeRaises: GLib.Error
Returns: GLib.IOStatus.NORMAL
if the encoding was successfully setReturn type: 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
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 channelRaises: GLib.Error
Returns: the status of the operation. Return type: GLib.IOStatus
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.- line_term (
-
shutdown
(flush)[source]¶ Parameters: flush ( bool
) – ifTrue
, flush pendingRaises: GLib.Error
Returns: the status of the operation. Return type: GLib.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 usingGLib.IOChannel.unref
().
-
unix_get_fd
()[source]¶ Returns: the file descriptor of the GLib.IOChannel
.Return type: int
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 characterRaises: GLib.Error
Returns: a GLib.IOStatus
Return type: GLib.IOStatus
Writes a Unicode character to self. This function cannot be called on a channel with
None
encoding.
-
writelines
(lines)¶
-
classmethod