xonsh.procs.readers

File handle readers and related tools.

class xonsh.procs.readers.BufferedFDParallelReader(fd, buffer=None, chunksize=1024)[source]

Buffered, parallel background thread reader.

Parameters:
fdint

File descriptor from which to read.

bufferbinary file-like or None, optional

A buffer to write bytes into. If None, a new BytesIO object is created.

chunksizeint, optional

The max size of the parallel reads, default 1 kb.

class xonsh.procs.readers.ConsoleParallelReader(fd, buffer=None, chunksize=1024, timeout=None)[source]

Parallel reader for consoles that runs in a background thread. This is only needed, available, and useful on Windows.

Parameters:
fdint

Standard buffer file descriptor, 0 for stdin, 1 for stdout (default), and 2 for stderr.

bufferctypes.c_wchar_p, optional

An existing buffer to (re-)use.

chunksizeint, optional

The max size of the parallel reads, default 1 kb.

timeoutfloat, optional

The queue reading timeout.

close()

close the reader

fileno()

Returns the file descriptor number.

is_fully_read()

Returns whether or not the queue is fully read and the reader is closed.

iterqueue()

Iterates through all remaining chunks in a blocking fashion.

read(size=-1)

Reads bytes from the file.

read_queue()

Reads a single chunk from the queue. This is blocking if the timeout is None and non-blocking otherwise.

static readable()

Returns true, because this object is always readable.

readline(size=-1)

Reads a line, or a partial line from the file descriptor.

readlines(hint=-1)

Reads lines from the file descriptor. This is blocking for negative hints (i.e. read all the remaining lines) and non-blocking otherwise.

class xonsh.procs.readers.NonBlockingFDReader(fd, timeout=None)[source]

A class for reading characters from a file descriptor on a background thread. This has the advantages that the calling thread can close the file and that the reading does not block the calling thread.

Parameters:
fdint

A file descriptor

timeoutfloat or None, optional

The queue reading timeout.

close()

close the reader

fileno()

Returns the file descriptor number.

is_fully_read()

Returns whether or not the queue is fully read and the reader is closed.

iterqueue()

Iterates through all remaining chunks in a blocking fashion.

read(size=-1)

Reads bytes from the file.

read_queue()

Reads a single chunk from the queue. This is blocking if the timeout is None and non-blocking otherwise.

static readable()

Returns true, because this object is always readable.

readline(size=-1)

Reads a line, or a partial line from the file descriptor.

readlines(hint=-1)

Reads lines from the file descriptor. This is blocking for negative hints (i.e. read all the remaining lines) and non-blocking otherwise.

class xonsh.procs.readers.QueueReader(fd, timeout=None)[source]

Provides a file-like interface to reading from a queue.

Parameters:
fdint

A file descriptor

timeoutfloat or None, optional

The queue reading timeout.

close()[source]

close the reader

fileno()[source]

Returns the file descriptor number.

is_fully_read()[source]

Returns whether or not the queue is fully read and the reader is closed.

iterqueue()[source]

Iterates through all remaining chunks in a blocking fashion.

read(size=-1)[source]

Reads bytes from the file.

read_queue()[source]

Reads a single chunk from the queue. This is blocking if the timeout is None and non-blocking otherwise.

static readable()[source]

Returns true, because this object is always readable.

readline(size=-1)[source]

Reads a line, or a partial line from the file descriptor.

readlines(hint=-1)[source]

Reads lines from the file descriptor. This is blocking for negative hints (i.e. read all the remaining lines) and non-blocking otherwise.

xonsh.procs.readers.populate_buffer(reader, fd, buffer, chunksize)[source]

Reads bytes from the file descriptor and copies them into a buffer.

The reads happen in parallel using the pread() syscall; which is only available on POSIX systems. If the read fails for any reason, the reader is flagged as closed.

xonsh.procs.readers.populate_console(reader, fd, buffer, chunksize, queue, expandsize=None)[source]

Reads bytes from the file descriptor and puts lines into the queue. The reads happened in parallel, using xonsh.winutils.read_console_output_character(), and is thus only available on windows. If the read fails for any reason, the reader is flagged as closed.

xonsh.procs.readers.populate_fd_queue(reader, fd, queue)[source]

Reads 1 kb of data from a file descriptor into a queue. If this ends or fails, it flags the calling reader object as closed.

xonsh.procs.readers.safe_fdclose(handle, cache=None)[source]

Closes a file handle in the safest way possible, and potentially storing the result.