This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from matthew-brett/master
MRG: Python 3 fixes, and some refactoring
- Loading branch information
Showing
8 changed files
with
169 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pyflakes-vim.zip | ||
*.pyc | ||
.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[submodule "ftplugin/python/pyflakes"] | ||
path = ftplugin/python/pyflakes | ||
url = https://github.com/pyflakes/pyflakes.git | ||
url = https://github.com/PyCQA/pyflakes.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
""" Code for running pyflakes checks in Vim buffer | ||
The main function is ``check``, which runs the pyflakes check on a buffer. | ||
""" | ||
|
||
import sys | ||
import ast | ||
from operator import attrgetter | ||
import re | ||
|
||
from pyflakes import checker, messages | ||
|
||
try: | ||
# Vim module available within vim | ||
import vim | ||
except ImportError: | ||
# Otherwise, mock it up for tests | ||
from mock import Mock | ||
vim = Mock() | ||
|
||
|
||
class loc(object): | ||
|
||
def __init__(self, lineno, col=None): | ||
self.lineno = lineno | ||
self.col_offset = col | ||
|
||
|
||
class SyntaxError(messages.Message): | ||
|
||
message = 'could not compile: %s' | ||
|
||
def __init__(self, filename, lineno, col, message): | ||
messages.Message.__init__(self, filename, loc(lineno, col)) | ||
self.message_args = (message,) | ||
self.lineno = lineno | ||
|
||
|
||
class blackhole(object): | ||
write = flush = lambda *a, **k: None | ||
|
||
|
||
def check(buffer): | ||
filename = buffer.name | ||
contents = buffer[:] | ||
|
||
# shebang usually found at the top of the file, followed by source code encoding marker. | ||
# assume everything else that follows is encoded in the encoding. | ||
for n, line in enumerate(contents): | ||
if n >= 2: | ||
break | ||
elif re.match(r'#.*coding[:=]\s*([-\w.]+)', line): | ||
contents = ['']*(n+1) + contents[n+1:] | ||
break | ||
|
||
contents = '\n'.join(contents) + '\n' | ||
|
||
vimenc = vim.eval('&encoding') | ||
if vimenc and hasattr(contents, 'decode'): | ||
contents = contents.decode(vimenc) | ||
|
||
builtins = set(['__file__']) | ||
try: | ||
builtins.update(set(eval(vim.eval('string(g:pyflakes_builtins)')))) | ||
except Exception: | ||
pass | ||
|
||
try: | ||
# TODO: use warnings filters instead of ignoring stderr | ||
old_stderr, sys.stderr = sys.stderr, blackhole() | ||
try: | ||
tree = ast.parse(contents, filename or '<unknown>') | ||
finally: | ||
sys.stderr = old_stderr | ||
except: | ||
exc_value = sys.exc_info()[1] | ||
try: | ||
lineno, offset, line = exc_value.args[1][1:] | ||
except IndexError: | ||
lineno, offset, line = 1, 0, '' | ||
if line and line.endswith("\n"): | ||
line = line[:-1] | ||
|
||
return [SyntaxError(filename, lineno, offset, str(exc_value))] | ||
else: | ||
# pyflakes looks to _MAGIC_GLOBALS in checker.py to see which | ||
# UndefinedNames to ignore | ||
old_globals = getattr(checker,' _MAGIC_GLOBALS', []) | ||
checker._MAGIC_GLOBALS = set(old_globals) | builtins | ||
|
||
filename = '(none)' if filename is None else filename | ||
w = checker.Checker(tree, filename) | ||
|
||
checker._MAGIC_GLOBALS = old_globals | ||
|
||
w.messages.sort(key = attrgetter('lineno')) | ||
return w.messages | ||
|
||
|
||
def vim_quote(s): | ||
return s.replace("'", "''") |
Submodule pyflakes
updated
24 files
+44 −0 | .appveyor.yml | |
+3 −1 | .gitignore | |
+12 −5 | .travis.yml | |
+4 −1 | AUTHORS | |
+1 −1 | LICENSE | |
+91 −1 | NEWS.txt | |
+57 −5 | README.rst | |
+1 −2 | pyflakes/__init__.py | |
+88 −8 | pyflakes/api.py | |
+765 −255 | pyflakes/checker.py | |
+112 −7 | pyflakes/messages.py | |
+9 −4 | pyflakes/reporter.py | |
+1 −0 | pyflakes/scripts/pyflakes.py | |
+33 −2 | pyflakes/test/harness.py | |
+245 −32 | pyflakes/test/test_api.py | |
+217 −0 | pyflakes/test/test_dict.py | |
+275 −42 | pyflakes/test/test_doctests.py | |
+393 −13 | pyflakes/test/test_imports.py | |
+1,064 −107 | pyflakes/test/test_other.py | |
+34 −0 | pyflakes/test/test_return_with_arguments_inside_generator.py | |
+413 −7 | pyflakes/test/test_undefined_names.py | |
+2 −0 | setup.cfg | |
+3 −5 | setup.py | |
+10 −3 | tox.ini |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" Test flaker module | ||
The flaker module contains Python functions used by the Vim code. | ||
Run with:: | ||
pip install nose pytest | ||
py.test test_flaker.py | ||
""" | ||
|
||
from os.path import dirname, join as pjoin | ||
import sys | ||
|
||
# Put our copy of pyflakes on the PATH | ||
sys.path.insert(0, pjoin(dirname(__file__), 'pyflakes')) | ||
|
||
import flaker | ||
|
||
|
||
class Buffer(object): | ||
|
||
def __init__(self, **kwargs): | ||
self.__dict__.update(kwargs) | ||
|
||
def __getitem__(self, slicer): | ||
return self.contents[slicer] | ||
|
||
|
||
def test_check(): | ||
# Try a syntax error | ||
buffer = Buffer(name='foo', contents = ["First line\n"]) | ||
ret = flaker.check(buffer) | ||
assert len(ret) == 1 | ||
assert isinstance(ret[0], flaker.SyntaxError) | ||
# Code OK, empty list returned | ||
buffer = Buffer(name='foo', contents = ["a = 1\n"]) | ||
assert flaker.check(buffer) == [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pytest | ||
mock |