提交 50ed3d49 编写于 作者: X Xavier Fernandez

Check for PEP263 headers in req files

上级 5589ff28
import codecs
import locale
import re
BOMS = [
......@@ -12,6 +13,8 @@ BOMS = [
(codecs.BOM_UTF32_LE, 'utf32-le'),
]
ENCODING_RE = re.compile('coding[:=]\s*([-\w.]+)')
def auto_decode(data):
"""Check a bytes string for a BOM to correctly detect the encoding
......@@ -20,4 +23,9 @@ def auto_decode(data):
for bom, encoding in BOMS:
if data.startswith(bom):
return data[len(bom):].decode(encoding)
# Lets check the first two lines as in PEP263
for line in data.splitlines()[:2]:
if line.startswith('#') and ENCODING_RE.search(line):
encoding = ENCODING_RE.search(line).groups()[0]
return data.decode(encoding)
return data.decode(locale.getpreferredencoding(False))
......@@ -464,3 +464,7 @@ class TestEncoding(object):
def test_auto_decode_no_bom(self):
assert auto_decode(b'foobar') == u'foobar'
def test_auto_decode_pep263_headers(self):
latin1_req = u'# coding=latin1\n# Pas trop de café'
assert auto_decode(latin1_req.encode('latin1')) == latin1_req
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册