From aaa14dc4e17efb7b220c1140e3b4728d32cd21c2 Mon Sep 17 00:00:00 2001 From: Tarek Ziade Date: Fri, 27 Jul 2012 11:52:37 +0200 Subject: [PATCH] fixed the stdin, thanks to zeeg --- flake8/pep8.py | 7 ++++++- flake8/run.py | 17 +++++++++++++++-- setup.py | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/flake8/pep8.py b/flake8/pep8.py index 7150fb1..a09fcdb 100644 --- a/flake8/pep8.py +++ b/flake8/pep8.py @@ -1127,6 +1127,9 @@ class Checker(object): if filename is None: self.filename = 'stdin' self.lines = lines or [] + elif hasattr(filename, 'readlines'): + self.lines = filename.readlines() + self.filename = 'stdin' elif lines is None: self.lines = readlines(filename) else: @@ -1644,8 +1647,10 @@ def process_options(arglist=None): options.repeat = False if options.testsuite: args.append(options.testsuite) + if not args and not options.doctest: - parser.error('input not specified') + pass + options.prog = os.path.basename(sys.argv[0]) options.exclude = options.exclude.split(',') for index, value in enumerate(options.exclude): diff --git a/flake8/run.py b/flake8/run.py index cd3a1ee..649f7b9 100644 --- a/flake8/run.py +++ b/flake8/run.py @@ -6,6 +6,11 @@ import sys import os import os.path from subprocess import PIPE, Popen +import select +try: + from StringIO import StringIO # NOQA +except ImportError: + from io import StringIO # NOQA from flake8.util import skip_file from flake8 import pep8 @@ -22,7 +27,8 @@ def check_file(path, complexity=-1): def check_code(code, complexity=-1): - warnings = pyflakes.check(code, '') + warnings = pyflakes.check(code, 'stdin') + warnings += pep8.input_file(StringIO(code)) if complexity > -1: warnings += mccabe.get_code_complexity(code, complexity) return warnings @@ -53,10 +59,17 @@ def main(): if builtins: orig_builtins = set(pyflakes._MAGIC_GLOBALS) pyflakes._MAGIC_GLOBALS = orig_builtins | builtins - if args: + + if args and options.filename is not None: for path in _get_python_files(args): warnings += check_file(path, complexity) else: + # wait for 1 second on the stdin fd + reads, __, __ = select.select([sys.stdin], [], [], 1.) + if reads == []: + print('input not specified') + raise SystemExit(1) + stdin = sys.stdin.read() warnings += check_code(stdin, complexity) diff --git a/setup.py b/setup.py index 4c036bf..dee5365 100755 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ import sys ispy3 = sys.version_info[0] == 3 if ispy3: - from distutils.core import setup + from distutils.core import setup # NOQA else: try: from setuptools import setup # NOQA