Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Improve the hashing logic #43

Open
HaoZeke opened this issue Jan 6, 2025 · 0 comments
Open

ENH: Improve the hashing logic #43

HaoZeke opened this issue Jan 6, 2025 · 0 comments

Comments

@HaoZeke
Copy link
Member

HaoZeke commented Jan 6, 2025

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant