'Test roundtrip for `untokenize`. `f` is an open file or a string.
The source code in f is tokenized, converted back to source code
via tokenize.untokenize(), and tokenized again from the latter.
The test fails if the second tokenization doesn\'t match the first.'

def check_roundtrip(self, f):

    if isinstance(f, str):
        f = StringIO(f)
    token_list = list(generate_tokens(f.readline))
    f.close()
    tokens1 = [tok[:2] for tok in token_list]
    new_text = untokenize(tokens1)
    readline = iter(new_text.splitlines(1)).next
    tokens2 = [tok[:2] for tok in generate_tokens(readline)]
    self.assertEqual(tokens2, tokens1)
