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) – an- errnoerror number, e.g.- EINVAL- Returns: - a - GLib.IOChannelErrorerror number, e.g.- GLib.IOChannelError.INVAL.- Return type: - GLib.IOChannelError- Converts an - errnoerror number to a- GLib.IOChannelError.
 - 
classmethod new_file(filename, mode)[source]¶
- Parameters: - Raises: - Returns: - A - GLib.IOChannelon success,- Noneon failure.- Return type: - Open a file filename as a - GLib.IOChannelusing mode mode. This channel will be closed when the last reference to it is dropped, so there is no need to call- GLib.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.IOChannelgiven a file descriptor. On UNIX systems this works for plain files, pipes, and sockets.- The returned - GLib.IOChannelhas a reference count of 1.- The default encoding for - GLib.IOChannelis 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- GLib.get_charset()) with the- GLib.IOChannel.set_encoding() function. By default, the fd passed will not be closed when the final reference to the- GLib.IOChanneldata structure is dropped.- If you want to read raw binary data without interpretation, then call the - GLib.IOChannel.set_encoding() function with- Nonefor 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, or- GLib.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.IOConditiondepending on whether there is data to be read/space to write data in the internal buffers in the- GLib.IOChannel. Only the flags- GLib.IOCondition.INand- GLib.IOCondition.OUTmay be set.
 - 
get_buffered()[source]¶
- Returns: - Trueif the self is buffered.- Return type: - bool- Returns whether self is buffered. 
 - 
get_close_on_unref()[source]¶
- Returns: - Trueif the channel will be closed,- Falseotherwise.- 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 - Truefor channels created by- GLib.IOChannel.new_file(), and- Falsefor 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 - Nonemakes 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 as- GLib.IOFlags.IS_READABLE.- The values of the flags - GLib.IOFlags.IS_READABLEand- GLib.IOFlags.IS_WRITABLEare 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- GLib.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 terminator- Returns: - The line termination string. This value is owned by GLib and must not be freed. - Return type: - str- This returns the string that - GLib.IOChanneluses to determine where in the file a line break occurs. A value of- Noneindicates autodetection.
 - 
init()[source]¶
- Initializes a - GLib.IOChannelstruct.- 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 of- GLib.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.NONEif 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 with- GLib.free() when no longer needed. This is a nul-terminated string. If a length of zero is returned, this will be- Noneinstead.- 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.IOChannelinto a newly-allocated string. str_return will contain allocated memory if the return is- GLib.IOStatus.NORMAL.
 - 
read_line_string(buffer, terminator_pos)[source]¶
- Parameters: - buffer (GLib.String) – aGLib.Stringinto which the line will be written. If buffer already contains data, the old data will be overwritten.
- terminator_pos (intorNone) – 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 a- GLib.Stringas a buffer.
- buffer (
 - 
read_to_end()[source]¶
- Raises: - GLib.Error- Returns: - GLib.IOStatus.NORMALon success. This function never returns- GLib.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 with- GLib.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 - Noneencoding.
 - 
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.NONEif 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.CURis 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 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.EOFnot require such a flush. For write-only channels, a call to- GLib.IOChannel.flush() is sufficient. For all other channels, the buffers may be flushed by a call to- GLib.IOChannel.seek_position(). This includes the possibility of seeking with seek type- GLib.SeekType.CURand 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 the- GLib.IOChanneldata structure.- Whether to close the channel on the final unref of the - GLib.IOChanneldata structure. The default value of this is- Truefor channels created by- GLib.IOChannel.new_file(), and- Falsefor all other channels.- Setting this flag to - Truefor a channel you have already closed can cause problems when the final reference to the- GLib.IOChannelis dropped.
 - 
set_encoding(encoding)[source]¶
- Parameters: - encoding ( - stror- None) – the encoding type- Raises: - GLib.Error- Returns: - GLib.IOStatus.NORMALif the encoding was successfully set- Return 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 - Noneis 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 Noneor 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.AGAINorGLib.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 of- GLib.SeekType.CUR, and, if they are “seekable”, cannot call- GLib.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: - 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 (strorNone) – 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 (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.IOChanneluses to determine where in the file a line break occurs.
- line_term (
 - 
shutdown(flush)[source]¶
- Parameters: - flush ( - bool) – if- True, flush pending- Raises: - 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 using- GLib.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.NONEif 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 is- GLib.IOStatus.NORMALand 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 - Noneor UTF-8, generic mixing of reading and writing is not allowed. A call to- GLib.IOChannel.write_chars() may only be made on a channel from which data has been read in the cases described in the documentation for- GLib.IOChannel.set_encoding().
 - 
write_unichar(thechar)[source]¶
- Parameters: - thechar ( - str) – a character- Raises: - 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 - Noneencoding.
 - 
writelines(lines)¶
 
- 
classmethod