Skip to content

ENH: Improve the hashing logic #43

Open
@HaoZeke

Description

@HaoZeke

The current default hash is sensitive to whitespace, which is not great, and ought to be replaced with something better... OTOH this might work:

def robust_hash_python_code_tokenize(code):
    try:
        tokens = []
        g = tokenize.generate_tokens(io.StringIO(code).readline)
        for toknum, tokval, _, _, _ in g:
            if toknum not in (tokenize.COMMENT, tokenize.NL, tokenize.ENDMARKER, tokenize.DEDENT, tokenize.INDENT):
                tokens.append((toknum, tokval))
        token_string = str(tokens).encode('utf-8')
        return hashlib.sha256(token_string).hexdigest()
    except tokenize.TokenError:
        return None

See also airspeed-velocity/asv#1456 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions