diff options
author | Aarni Koskela <akx@iki.fi> | 2017-08-21 16:25:51 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-21 16:25:51 +0300 |
commit | 231d337b569b15b9437cfefee6584ad8d54d0cd3 (patch) | |
tree | 141b07954798ee16a819475cfa458664a0d76a61 /babel | |
parent | 1da04fd0af20162fe4e2b503a5fcae8f25267010 (diff) | |
download | babel-231d337b569b15b9437cfefee6584ad8d54d0cd3.tar.gz |
Parse multi-line __future__ imports better (#519)
Augments #426
Augments #427
Augments #510
Diffstat (limited to 'babel')
-rw-r--r-- | babel/util.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/babel/util.py b/babel/util.py index 996f902..af8c762 100644 --- a/babel/util.py +++ b/babel/util.py @@ -109,10 +109,23 @@ def parse_future_flags(fp, encoding='latin-1'): flags = 0 try: body = fp.read().decode(encoding) + + # Fix up the source to be (hopefully) parsable by regexpen. + # This will likely do untoward things if the source code itself is broken. + + # (1) Fix `import (\n...` to be `import (...`. + body = re.sub(r'import\s*\([\r\n]+', 'import (', body) + # (2) Join line-ending commas with the next line. + body = re.sub(r',\s*[\r\n]+', ', ', body) + # (3) Remove backslash line continuations. + body = re.sub(r'\\\s*[\r\n]+', ' ', body) + for m in PYTHON_FUTURE_IMPORT_re.finditer(body): - names = [x.strip() for x in m.group(1).split(',')] + names = [x.strip().strip('()') for x in m.group(1).split(',')] for name in names: - flags |= getattr(__future__, name).compiler_flag + feature = getattr(__future__, name, None) + if feature: + flags |= feature.compiler_flag finally: fp.seek(pos) return flags |