[svn r37739] Made that only the first two lines of a source file are examined for the
encoding. --HG-- branch : trunk
This commit is contained in:
parent
ec734026cc
commit
d9572239a8
|
@ -261,8 +261,15 @@ def get_module_encoding(path):
|
||||||
if path[-1] in ['c', 'o']:
|
if path[-1] in ['c', 'o']:
|
||||||
path = path[:-1]
|
path = path[:-1]
|
||||||
fpath = py.path.local(path)
|
fpath = py.path.local(path)
|
||||||
code = fpath.read()
|
fp = fpath.open()
|
||||||
match = _reg_enc.search(code)
|
lines = []
|
||||||
|
try:
|
||||||
|
# encoding is only allowed in the first two lines
|
||||||
|
for i in range(2):
|
||||||
|
lines.append(fp.readline())
|
||||||
|
finally:
|
||||||
|
fp.close()
|
||||||
|
match = _reg_enc.search('\n'.join(lines))
|
||||||
if match:
|
if match:
|
||||||
return match.group(1)
|
return match.group(1)
|
||||||
return 'ISO-8859-1'
|
return 'ISO-8859-1'
|
||||||
|
|
|
@ -177,3 +177,14 @@ def test_get_encoding_for_real():
|
||||||
""")))
|
""")))
|
||||||
assert get_module_encoding(fpath.strpath) == 'UTF-8'
|
assert get_module_encoding(fpath.strpath) == 'UTF-8'
|
||||||
|
|
||||||
|
def test_get_encoding_matching_pattern_elsewhere():
|
||||||
|
temp = py.test.ensuretemp('test_get_encoding')
|
||||||
|
fpath = temp.join('matching_pattern.py')
|
||||||
|
fpath.write(str(py.code.Source("""\
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
def foo(coding=None):
|
||||||
|
pass
|
||||||
|
""")))
|
||||||
|
assert get_module_encoding(fpath.strpath) == 'ISO-8859-1'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue