Commands Cache (xonsh.commands_cache)

Amalgamation of xonsh package, made up of the following modules, in order:

  • cli_utils

  • contexts

  • lazyasd

  • lazyjson

  • platform

  • pretty

  • xontribs_meta

  • codecache

  • lazyimps

  • parser

  • tokenize

  • tools

  • ast

  • color_tools

  • commands_cache

  • completer

  • events

  • foreign_shells

  • jobs

  • jsonutils

  • lexer

  • openpy

  • xontribs

  • ansi_colors

  • diff_history

  • dirstack

  • execer

  • shell

  • style_tools

  • timings

  • xonfig

  • base_shell

  • environ

  • imphooks

  • inspectors

  • aliases

  • main

  • readline_shell

  • tracer

  • dumb_shell

exception xonsh.commands_cache.TokenError[source]

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class xonsh.commands_cache.TokenInfo(type, string, start, end, line)[source]

Create new instance of TokenInfo(type, string, start, end, line)

count(value, /)

Return number of occurrences of value.

index(value, start=0, stop=sys.maxsize, /)

Return first index of value.

Raises ValueError if the value is not present.


Alias for field number 3

property exact_type

Alias for field number 4


Alias for field number 2


Alias for field number 1


Alias for field number 0


The detect_encoding() function is used to detect the encoding that should be used to decode a Python source file. It requires one argument, readline, in the same way as the tokenize() generator.

It will call readline a maximum of twice, and return the encoding used (as a string) and a list of any lines (left as bytes) it has read in.

It detects the encoding from the presence of a utf-8 bom or an encoding cookie as specified in pep-0263. If both a bom and a cookie are present, but disagree, a SyntaxError will be raised. If the encoding cookie is an invalid charset, raise a SyntaxError. Note that if a utf-8 bom is found, ‘utf-8-sig’ is returned.

If no encoding is specified, then the default of ‘utf-8’ will be returned.

xonsh.commands_cache.tokenize(readline, tolerant=False)[source]

The tokenize() generator requires one argument, readline, which must be a callable object which provides the same interface as the readline() method of built-in file objects. Each call to the function should return one line of input as bytes. Alternately, readline can be a callable function terminating with StopIteration:

readline = open(myfile, ‘rb’).__next__ # Example of alternate readline

The generator produces 5-tuples with these members: the token type; the token string; a 2-tuple (srow, scol) of ints specifying the row and column where the token begins in the source; a 2-tuple (erow, ecol) of ints specifying the row and column where the token ends in the source; and the line on which the token was found. The line passed is the logical line; continuation lines are included.

The first token sequence will always be an ENCODING token which tells you which encoding was used to decode the bytes stream.

If tolerant is True, yield ERRORTOKEN with the erroneous string instead of throwing an exception when encountering an error.


Transform tokens back into Python source code. It returns a bytes object, encoded using the ENCODING token, which is the first token sequence output by tokenize.

Each element returned by the iterable must be a token sequence with at least two elements, a token number and token value. If only two tokens are passed, the resulting output is poor.

Round-trip invariant for full input:

Untokenized source will match input source exactly

Round-trip invariant for limited intput:

# Output bytes will tokenize the back to the input t1 = [tok[:2] for tok in tokenize(f.readline)] newcode = untokenize(t1) readline = BytesIO(newcode).readline t2 = [tok[:2] for tok in tokenize(readline)] assert t1 == t2