import ast s = 'from __future__ import division' # This works fine. a = ast.parse(s) c = compile(a, '', 'exec') assert isinstance(a.body[0], ast.ImportFrom) assert a.body[0].module == '__future__' assert a.body[0].module is '__future__' # Both are the interned string. # Now we get the string '__future__' that's not interned. x = 'x' * 1024 + '__future__' y = x[-10:] assert y == '__future__' assert y is not '__future__' # Not the interned string. # Inject this into the AST and watch it fail. a.body[0].module = y assert a.body[0].module == '__future__' c = compile(a, '', 'exec') # This raises SyntaxError but should not