Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to pyflakes 1.6.0 #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/pyflakes/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.0'
__version__ = '1.6.0'
44 changes: 40 additions & 4 deletions contrib/pyflakes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import sys
import os
import re
import _ast

from pyflakes import checker, __version__
Expand All @@ -13,6 +14,9 @@
__all__ = ['check', 'checkPath', 'checkRecursive', 'iterSourceCode', 'main']


PYTHON_SHEBANG_REGEX = re.compile(br'^#!.*\bpython[23w]?\b\s*$')


def check(codeString, filename, reporter=None):
"""
Check the Python source given by C{codeString} for flakes.
Expand Down Expand Up @@ -41,6 +45,18 @@ def check(codeString, filename, reporter=None):

(lineno, offset, text) = value.lineno, value.offset, value.text

if checker.PYPY:
if text is None:
lines = codeString.splitlines()
if len(lines) >= lineno:
text = lines[lineno - 1]
if sys.version_info >= (3, ) and isinstance(text, bytes):
try:
text = text.decode('ascii')
except UnicodeDecodeError:
text = None
offset -= 1

# If there's an encoding problem with the file, the text is None.
if text is None:
# Avoid using msg, since for the only known case, it contains a
Expand Down Expand Up @@ -96,6 +112,25 @@ def checkPath(filename, reporter=None):
return check(codestr, filename, reporter)


def isPythonFile(filename):
"""Return True if filename points to a Python file."""
if filename.endswith('.py'):
return True

max_bytes = 128

try:
with open(filename, 'rb') as f:
text = f.read(max_bytes)
if not text:
return False
except IOError:
return False

first_line = text.splitlines()[0]
return PYTHON_SHEBANG_REGEX.match(first_line)


def iterSourceCode(paths):
"""
Iterate over all Python source files in C{paths}.
Expand All @@ -108,8 +143,9 @@ def iterSourceCode(paths):
if os.path.isdir(path):
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
if filename.endswith('.py'):
yield os.path.join(dirpath, filename)
full_path = os.path.join(dirpath, filename)
if isPythonFile(full_path):
yield full_path
else:
yield path

Expand Down Expand Up @@ -157,7 +193,7 @@ def handler(sig, f):
pass


def main(prog=None):
def main(prog=None, args=None):
"""Entry point for the script "pyflakes"."""
import optparse

Expand All @@ -166,7 +202,7 @@ def main(prog=None):
_exitOnSignal('SIGPIPE', 1)

parser = optparse.OptionParser(prog=prog, version=__version__)
(__, args) = parser.parse_args()
(__, args) = parser.parse_args(args=args)
reporter = modReporter._makeDefaultReporter()
if args:
warnings = checkRecursive(args, reporter)
Expand Down
Loading