Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 5359f7f

Browse files
committed
Python checker: new option g:syntastic_python_python_use_codec (@delfick).
The new option tells the checket to load the file in memory and look for a comment line "# coding: <codec>". If found, the named codec is used to decode the file before checking. This makes sense only if said codec doesn't change the line numbers of the initial file. An example of such codec is "spec" provided by the "Nose of Yeti" library: https://github.com/delfick/nose-of-yeti
1 parent 4d1f2e2 commit 5359f7f

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

plugin/syntastic.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if has('reltime')
1919
lockvar! g:_SYNTASTIC_START
2020
endif
2121

22-
let g:_SYNTASTIC_VERSION = '3.5.0-72'
22+
let g:_SYNTASTIC_VERSION = '3.5.0-146'
2323
lockvar g:_SYNTASTIC_VERSION
2424

2525
" Sanity checks {{{1

syntax_checkers/python/codec.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
from __future__ import print_function
4+
from sys import argv, exit
5+
6+
import codecs
7+
import re
8+
import os
9+
10+
11+
if len(argv) != 2:
12+
exit(1)
13+
14+
try:
15+
with open(argv[1]) as fle:
16+
text = fle.readlines()
17+
18+
if text:
19+
match = re.match(r"#\s*coding\s*:\s*(?P<coding>\w+)", text[0])
20+
if match:
21+
text = codecs.lookup(match.groupdict()["coding"]).incrementaldecoder().decode(
22+
''.join(text).encode('utf-8')).encode('utf-8')
23+
24+
if isinstance(text, list):
25+
text = ''.join(text).encode('utf-8')
26+
27+
compile(text, argv[1], 'exec', 0, 1)
28+
except SyntaxError as err:
29+
print('%s:%s:%s: %s' % (err.filename, err.lineno, err.offset, err.msg))
30+
except Exception as err:
31+
print('%s:%s:%s: %s' % (os.path.abspath(argv[1]), 1, 0, err))

syntax_checkers/python/python.vim

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ if exists("g:loaded_syntastic_python_python_checker")
1515
endif
1616
let g:loaded_syntastic_python_python_checker = 1
1717

18+
if !exists('g:syntastic_python_python_use_codec')
19+
let g:syntastic_python_python_use_codec = 0
20+
endif
21+
1822
let s:save_cpo = &cpo
1923
set cpo&vim
2024

21-
let s:compiler = expand('<sfile>:p:h', 1) . syntastic#util#Slash() . 'compile.py'
25+
let s:base_path = expand('<sfile>:p:h', 1) . syntastic#util#Slash()
2226

2327
function! SyntaxCheckers_python_python_IsAvailable() dict
2428
if !executable(self.getExec())
@@ -32,7 +36,9 @@ function! SyntaxCheckers_python_python_IsAvailable() dict
3236
endfunction
3337

3438
function! SyntaxCheckers_python_python_GetLocList() dict
35-
let makeprg = self.makeprgBuild({ 'exe': [self.getExec(), s:compiler] })
39+
let compiler = s:base_path . (g:syntastic_python_python_use_codec ? 'codec.py' : 'compile.py')
40+
call self.log('using compiler script', compiler)
41+
let makeprg = self.makeprgBuild({ 'exe': [self.getExec(), compiler] })
3642

3743
let errorformat = '%E%f:%l:%c: %m'
3844

0 commit comments

Comments
 (0)