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 ()

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 errno 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 a GLib.IOChannelError.

classmethod error_quark()[source]
Return type:

int

classmethod new_file(filename, mode)[source]
Parameters:
  • filename (str) – A string containing the name of a file

  • mode (str) – One of “r”, “w”, “a”, “r+”, “w+”, “a+”. These have the same meaning as in fopen()

Raises:

GLib.Error

Returns:

A GLib.IOChannel on success, None on failure.

Return type:

GLib.IOChannel

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 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.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 (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.IOChannel data structure is dropped.

If you want to read raw binary data without interpretation, then call the GLib.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.

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.IOCondition depending 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.IN and GLib.IOCondition.OUT may be set.

get_buffer_size()[source]
Returns:

the size of the buffer.

Return type:

int

Gets the buffer size.

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 by GLib.IOChannel.new_file (), and False 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 as GLib.IOFlags.IS_READABLE.

The values of the flags GLib.IOFlags.IS_READABLE and GLib.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 GLib.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:

(str, length: int)

This returns the string that GLib.IOChannel uses to determine where in the file a line break occurs. A value of None 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 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 the GLib.IOChannel

  • bytes_read (int) – returns the number of bytes actually read

Returns:

GLib.IOError.NONE if the operation was successful.

Return type:

GLib.IOError

Reads data from a GLib.IOChannel.

Deprecated since version 2.2: Use GLib.IOChannel.read_chars() instead.

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 None 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 is GLib.IOStatus.NORMAL.

read_line_string(buffer, terminator_pos)[source]
Parameters:
  • buffer (GLib.String) – a GLib.String into which the line will be written. If buffer already contains data, the old data will be overwritten.

  • terminator_pos (int or None) – location to store position of line terminator, or None

Raises:

GLib.Error

Returns:

the status of the operation.

Return type:

GLib.IOStatus

Reads a line from a GLib.IOChannel, using a GLib.String as a buffer.

read_to_end()[source]
Raises:

GLib.Error

Returns:

GLib.IOStatus.NORMAL on 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 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:
Returns:

GLib.IOError.NONE if the operation was successful.

Return type:

GLib.IOError

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:
Raises:

GLib.Error

Returns:

the status of the operation.

Return type:

GLib.IOStatus

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 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.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 the GLib.IOChannel data structure.

Whether to close the channel on the final unref of the GLib.IOChannel data structure. The default value of this is True for channels created by GLib.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 GLib.IOChannel is dropped.

set_encoding(encoding)[source]
Parameters:

encoding (str or None) – the encoding type

Raises:

GLib.Error

Returns:

GLib.IOStatus.NORMAL if 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 None is safe to use with binary data.

The encoding can only be set if one of the following conditions is true:

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 (str or None) – The line termination string. Use None 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:

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:
  • buf (str) – the buffer containing the data to write

  • count (int) – the number of bytes to write

  • bytes_written (int) – the number of bytes actually written

Returns:

GLib.IOError.NONE if the operation was successful.

Return type:

GLib.IOError

Writes data to a GLib.IOChannel.

Deprecated since version 2.2: Use GLib.IOChannel.write_chars() instead.

write_chars(buf, count)[source]
Parameters:
  • buf (bytes) – a buffer to write data from

  • count (int) – the size of the buffer. If -1, the buffer is taken to be a nul-terminated string.

Raises:

GLib.Error

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.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 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 None encoding.

writelines(lines)