# Buggy code
def PyParseIntCast(unused_string, unused_location, tokens):
    """Return an integer from a string.

    This is a pyparsing callback method that converts the matched
    string into an integer.

    The method modifies the content of the tokens list and converts
    them all to an integer value.

    Args:
      unused_string: The original parsed string.
      unused_location: The location within the string where the match was made.
      tokens: A list of extracted tokens (where the string to be converted is
      stored).
    """
    # Cast the regular tokens.
    for index, token in enumerate(tokens):
        try:
            tokens[index] = int(token)
        except ValueError:
            logging.error(
                "Unable to cast [{0:s}] to an int, setting to 0".format(token)
            )
            tokens[index] = 0

    # We also need to cast the dictionary built tokens.
    for key in tokens.keys():
        try:
            tokens[key] = int(tokens[key], 10)
        except ValueError:
            logging.error(
                "Unable to cast [{0:s} = {1:d}] to an int, setting to 0".format(
                    key, tokens[key]
                )
            )
            tokens[key] = 0


# Traceback (UnicodeDecodeError)
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/plaso/engine/worker.py", line 169, in _ParseFileEntryWithParser
parser_object.UpdateChainAndParse(self._parser_mediator)
File "/usr/lib/python2.7/dist-packages/plaso/parsers/interface.py", line 72, in UpdateChainAndParse
self.Parse(parser_mediator, **kwargs)
File "/usr/lib/python2.7/dist-packages/plaso/parsers/interface.py", line 256, in Parse
self.ParseFileObject(parser_mediator, file_object, **kwargs)
File "/usr/lib/python2.7/dist-packages/plaso/parsers/text_parser.py", line 160, in ParseFileObject
_ = self.NextToken()
File "/usr/lib/python2.7/dist-packages/plaso/lib/lexer.py", line 191, in NextToken
return Lexer.NextToken(self)
File "/usr/lib/python2.7/dist-packages/plaso/lib/lexer.py", line 99, in NextToken
possible_next_state = callback(string=m.group(0), match=m)
File "/usr/lib/python2.7/dist-packages/plaso/parsers/text_parser.py", line 119, in ParseIncomplete
self.attributes['body'] += match.group(0)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

# Fixed code
def PyParseIntCast(unused_string, unused_location, tokens):
    """Return an integer from a string.

    This is a pyparsing callback method that converts the matched
    string into an integer.

    The method modifies the content of the tokens list and converts
    them all to an integer value.

    Args:
      unused_string: the original parsed string.
      unused_location: the location within the string where the match was made.
      tokens: A list of extracted tokens (where the string to be converted is
      stored).
    """
    # Cast the regular tokens.
    for index, token in enumerate(tokens):
        try:
            tokens[index] = int(token)
        except ValueError:
            logging.error(
                "Unable to cast [{0:s}] to an int, setting to 0".format(token)
            )
            tokens[index] = 0

    # We also need to cast the dictionary built tokens.
    for key in tokens.keys():
        try:
            tokens[key] = int(tokens[key], 10)
        except ValueError:
            logging.error(
                "Unable to cast [{0:s} = {1:d}] to an int, setting to 0".format(
                    key, tokens[key]
                )
            )
            tokens[key] = 0