Plugin loading. Handle encoding declarations correctly

This commit is contained in:
Kovid Goyal 2010-09-24 11:52:16 -06:00
parent 45ed048612
commit 93685faa2e

View File

@ -67,10 +67,17 @@ def load_plugin(path_to_zip_file): # {{{
if name.lower().endswith('plugin.py'): if name.lower().endswith('plugin.py'):
locals = {} locals = {}
raw = zf.read(name) raw = zf.read(name)
match = re.search(r'coding[:=]\s*([-\w.]+)', raw[:300]) lines, encoding = raw.splitlines(), 'utf-8'
encoding = 'utf-8' cr = re.compile(r'coding[:=]\s*([-\w.]+)')
if match is not None: raw = []
encoding = match.group(1) for l in lines[:2]:
match = cr.search(l)
if match is not None:
encoding = match.group(1)
else:
raw.append(l)
raw += lines[2:]
raw = '\n'.join(raw)
raw = raw.decode(encoding) raw = raw.decode(encoding)
raw = re.sub('\r\n', '\n', raw) raw = re.sub('\r\n', '\n', raw)
exec raw in locals exec raw in locals