[New-bugs-announce] [issue38953] Untokenize and retokenize does not round-trip
report at bugs.python.org
Mon Dec 2 06:06:14 EST 2019
New submission from Zac Hatfield-Dodds <zac.hatfield.dodds at gmail.com>:
I've been working on a tool called Hypothesmith - https://github.com/Zac-HD/hypothesmith - to generate arbitrary Python source code, inspired by CSmith's success in finding C compiler bugs. It's based on the grammar but ultimately only generates strings which `compile` accepts; this is the only way I know to answer the question "is the string valid Python"!
I should be clear that I don't think the minimal examples are representative of real problems that users may encounter! However, fuzzing is very effective at finding important bugs if we can get these apparently-trivial ones out of the way by changing either the code or the test :-)
tokens = list(tokenize.generate_tokens(io.StringIO(source_code).readline))
outstring = tokenize.untokenize(tokens) # may have changed whitespace from source
output = tokenize.generate_tokens(io.StringIO(outstring).readline)
assert [(t.type, t.string) for t in tokens] == [(t.type, t.string) for t in output]
Each of the `@example` cases are accepted by `compile` but fail the test; the `@given` case describes how to generate more such strings. You can read more details in the Hypothesmith repo if interested.
I think these are real and probably unimportant bugs, but I'd love to start a conversation about what properties should *always* hold for functions dealing with Python source code - and how best to report research results if I can demonstrate that they don't!
(for example, lib2to3 has many similar failures but I don't want to open a long list of low-value issues)
components: Library (Lib)
nosy: Zac Hatfield-Dodds, meador.inge
title: Untokenize and retokenize does not round-trip
versions: Python 3.8
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce