summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorent Xicluna <florent.xicluna@gmail.com>2014-03-23 02:45:41 +0100
committerFlorent Xicluna <florent.xicluna@gmail.com>2014-03-23 02:45:41 +0100
commit61a1f6ddc749ff0e909532f7044c25debe62c404 (patch)
tree048b85a47ad1e201436ec759563036842afc95eb
parentdd06247127fd415c424177a35ec89b46423b460f (diff)
parent9ab266d03d82d9d3bf5536a303624999a1ed89bc (diff)
downloadpep8-61a1f6ddc749ff0e909532f7044c25debe62c404.tar.gz
Merge branch 'master' of https://github.com/gward/pep8 into gward-master
-rwxr-xr-xpep8.py58
-rw-r--r--testsuite/E50.py23
-rw-r--r--testsuite/W19.py22
-rw-r--r--testsuite/W29.py3
-rw-r--r--testsuite/W39no.py6
5 files changed, 98 insertions, 14 deletions
diff --git a/pep8.py b/pep8.py
index e38ab26..47ea867 100755
--- a/pep8.py
+++ b/pep8.py
@@ -200,7 +200,7 @@ def missing_newline(physical_line):
return len(physical_line), "W292 no newline at end of file"
-def maximum_line_length(physical_line, max_line_length):
+def maximum_line_length(physical_line, max_line_length, multiline):
"""
Limit all lines to a maximum of 79 characters.
@@ -216,6 +216,10 @@ def maximum_line_length(physical_line, max_line_length):
line = physical_line.rstrip()
length = len(line)
if length > max_line_length and not noqa(line):
+ # Sometimes, long lines in docstrings are hard to avoid -- like,
+ # a long URL that can't be wrapped because it has no whitespace.
+ if multiline and re.match(r'^\s*\S+$', line):
+ return
if hasattr(line, 'decode'): # Python 2
# The line could contain multi-byte characters
try:
@@ -1189,6 +1193,7 @@ class Checker(object):
self._logical_checks = options.logical_checks
self._ast_checks = options.ast_checks
self.max_line_length = options.max_line_length
+ self.multiline = False # in a multiline string?
self.hang_closing = options.hang_closing
self.verbose = options.verbose
self.filename = filename
@@ -1237,16 +1242,9 @@ class Checker(object):
self.line_number += 1
if self.line_number > len(self.lines):
return ''
- return self.lines[self.line_number - 1]
-
- def readline_check_physical(self):
- """
- Check and return the next physical line. This method can be
- used to feed tokenize.generate_tokens.
- """
- line = self.readline()
- if line:
- self.check_physical(line)
+ line = self.lines[self.line_number - 1]
+ if self.indent_char is None and line[:1] in WHITESPACE:
+ self.indent_char = line[0]
return line
def run_check(self, check, argument_names):
@@ -1263,8 +1261,6 @@ class Checker(object):
Run all physical checks on a raw input line.
"""
self.physical_line = line
- if self.indent_char is None and line[:1] in WHITESPACE:
- self.indent_char = line[0]
for name, check, argument_names in self._physical_checks:
result = self.run_check(check, argument_names)
if result is not None:
@@ -1352,13 +1348,47 @@ class Checker(object):
def generate_tokens(self):
if self._io_error:
self.report_error(1, 0, 'E902 %s' % self._io_error, readlines)
- tokengen = tokenize.generate_tokens(self.readline_check_physical)
+ tokengen = tokenize.generate_tokens(self.readline)
try:
for token in tokengen:
yield token
+ self.maybe_check_physical(token)
except (SyntaxError, tokenize.TokenError):
self.report_invalid_syntax()
+ def maybe_check_physical(self, token):
+ """
+ If appropriate (based on token), check current physical line(s).
+ """
+ # This is called after every token, but we only want to take action
+ # after a token that ends a line.
+ if token[0] in (tokenize.NEWLINE, tokenize.NL):
+ # Obviously, a newline token ends a single physical line.
+ self.check_physical(token[4])
+ elif token[0] == tokenize.STRING and token[1].count('\n'):
+ # Less obviously, a string that contains newlines is a
+ # multiline string, either triple-quoted or with internal
+ # newlines backslash-escaped. Check every physical line in the
+ # string *except* for the last one: its newline is outside of
+ # the multiline string, so we consider it a regular physical
+ # line, and will check it like any other physical line.
+ #
+ # Subtleties:
+ # - we don't *completely* ignore the last line; if it contains
+ # the magical "# noqa" comment, we disable all physical
+ # checks for the entire multiline string
+ # - have to wind self.line_number back because initially it
+ # points to the last line of the string, and we want
+ # check_physical() to give accurate feedback
+ if noqa(token[4]):
+ return
+ self.multiline = True
+ self.line_number = token[2][0]
+ for line in token[1].split('\n')[:-1]:
+ self.check_physical(line + '\n')
+ self.line_number += 1
+ self.multiline = False
+
def check_all(self, expected=None, line_offset=0):
"""
Run all checks on the input file.
diff --git a/testsuite/E50.py b/testsuite/E50.py
index 4cc4383..6d62b38 100644
--- a/testsuite/E50.py
+++ b/testsuite/E50.py
@@ -45,3 +45,26 @@ ddd = \
('''
''' + ' \
')
+#: E501 E225 E226
+very_long_identifiers=and_terrible_whitespace_habits(are_no_excuse+for_long_lines)
+#
+#: E501
+'''multiline string
+with a long long long long long long long long long long long long long long long long line
+'''
+#: E501
+'''same thing, but this time without a terminal newline in the string
+long long long long long long long long long long long long long long long long line'''
+#
+# issue 224 (unavoidable long lines in docstrings)
+#: Okay
+"""
+I'm some great documentation. Because I'm some great documentation, I'm
+going to give you a reference to some valuable information about some API
+that I'm calling:
+
+ http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
+"""
+#: E501
+"""
+longnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaces"""
diff --git a/testsuite/W19.py b/testsuite/W19.py
index c81f46f..2209e38 100644
--- a/testsuite/W19.py
+++ b/testsuite/W19.py
@@ -97,6 +97,28 @@ if length > options.max_line_length:
if os.path.exists(os.path.join(path, PEP8_BIN)):
cmd = ([os.path.join(path, PEP8_BIN)] +
self._pep8_options(targetfile))
+#: W191
+'''
+ multiline string with tab in it'''
+#: E101 W191
+'''multiline string
+ with tabs
+ and spaces
+'''
+#: Okay
+'''sometimes, you just need to go nuts in a multiline string
+ and allow all sorts of crap
+ like mixed tabs and spaces
+
+or trailing whitespace
+or long long long long long long long long long long long long long long long long long lines
+''' # nopep8
+#: Okay
+'''this one
+ will get no warning
+even though the noqa comment is not immediately after the string
+''' + foo # noqa
+#
#: E101 W191
if foo is None and bar is "frop" and \
blah == 'yeah':
diff --git a/testsuite/W29.py b/testsuite/W29.py
index 2578f4f..42802ca 100644
--- a/testsuite/W29.py
+++ b/testsuite/W29.py
@@ -6,5 +6,8 @@ print
class Foo(object):
bang = 12
+#: W291
+'''multiline
+string with trailing whitespace'''
#: W292
# This line doesn't have a linefeed \ No newline at end of file
diff --git a/testsuite/W39no.py b/testsuite/W39no.py
new file mode 100644
index 0000000..2948b82
--- /dev/null
+++ b/testsuite/W39no.py
@@ -0,0 +1,6 @@
+#: Okay
+'''there is nothing wrong
+with a multiline string at EOF
+
+that happens to have a blank line in it
+'''