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

Commit 30aea07

Browse files
author
Vladimir Keleshev
committed
Fix build for Python versions other than 2.7
1 parent f89ba17 commit 30aea07

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

pep257.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
http://github.com/GreenSteam/pep257
1212
1313
"""
14+
from __future__ import with_statement
15+
1416
import os
1517
import sys
1618
import tokenize as tk
@@ -28,8 +30,16 @@
2830
try:
2931
next
3032
except NameError: # Python 2.5 and earlier
31-
def next(obj):
32-
return obj.next()
33+
nothing = object()
34+
35+
def next(obj, default=nothing):
36+
if default == nothing:
37+
return obj.next()
38+
else:
39+
try:
40+
return obj.next()
41+
except StopIteration:
42+
return default
3343

3444

3545
__version__ = '0.3.0'
@@ -491,8 +501,8 @@ def check_no_blank_before(self, function, docstring): # def
491501
# NOTE: This does not take into account functions with groups of code.
492502
if docstring:
493503
before, _, after = function.source.partition(docstring)
494-
blanks_before = map(is_blank, before.split('\n')[:-1])
495-
blanks_after = map(is_blank, after.split('\n')[1:])
504+
blanks_before = list(map(is_blank, before.split('\n')[:-1]))
505+
blanks_after = list(map(is_blank, after.split('\n')[1:]))
496506
blanks_before_count = sum(takewhile(bool, reversed(blanks_before)))
497507
blanks_after_count = sum(takewhile(bool, blanks_after))
498508
if blanks_before_count != 0:
@@ -526,8 +536,8 @@ def check_blank_before_after_class(slef, class_, docstring):
526536
# def foo(): pass
527537
if docstring:
528538
before, _, after = class_.source.partition(docstring)
529-
blanks_before = map(is_blank, before.split('\n')[:-1])
530-
blanks_after = map(is_blank, after.split('\n')[1:])
539+
blanks_before = list(map(is_blank, before.split('\n')[:-1]))
540+
blanks_after = list(map(is_blank, after.split('\n')[1:]))
531541
blanks_before_count = sum(takewhile(bool, reversed(blanks_before)))
532542
blanks_after_count = sum(takewhile(bool, blanks_after))
533543
if blanks_before_count != 1:

test.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
"""Docstring."""
22
# encoding: utf-8
3-
errors = set([])
3+
import sys
4+
5+
6+
expected = set([])
47

58

69
def expect(*args):
710
"""Decorator that expects a certain PEP 257 violation."""
8-
ignore = lambda a: None
11+
none = lambda a: None
912
if len(args) == 1:
10-
return lambda f: errors.add((f.__name__, args[0])) or ignore(f()) or f
11-
errors.add(args)
13+
return lambda f: expected.add((f.__name__, args[0])) or none(f()) or f
14+
expected.add(args)
15+
16+
17+
expect('class_', 'D101: Docstring missing')
1218

1319

14-
@expect('D101: Docstring missing')
1520
class class_:
1621

17-
@expect('D101: Docstring missing')
22+
expect('meta', 'D101: Docstring missing')
23+
1824
class meta:
1925
""""""
2026

@@ -70,20 +76,30 @@ def trailing_and_leading_space():
7076
pass
7177

7278

73-
@expect('D203: Expected 1 blank line *before* class docstring, found 0')
79+
expect('LeadingSpaceMissing',
80+
'D203: Expected 1 blank line *before* class docstring, found 0')
81+
82+
7483
class LeadingSpaceMissing:
7584
"""Leading space missing."""
7685

7786

78-
@expect('D204: Expected 1 blank line *after* class docstring, found 0')
87+
expect('TrailingSpace',
88+
'D204: Expected 1 blank line *after* class docstring, found 0')
89+
90+
7991
class TrailingSpace:
8092

8193
"""TrailingSpace."""
8294
pass
8395

8496

85-
@expect('D203: Expected 1 blank line *before* class docstring, found 0')
86-
@expect('D204: Expected 1 blank line *after* class docstring, found 0')
97+
expect('LeadingAndTrailingSpaceMissing',
98+
'D203: Expected 1 blank line *before* class docstring, found 0')
99+
expect('LeadingAndTrailingSpaceMissing',
100+
'D204: Expected 1 blank line *after* class docstring, found 0')
101+
102+
87103
class LeadingAndTrailingSpaceMissing:
88104
"""Leading and trailing space missing."""
89105
pass
@@ -182,9 +198,10 @@ def lalsksdewnlkjl():
182198
"""Sum\\mary."""
183199

184200

185-
@expect('D302: Use u""" for docstrings with Unicode')
186-
def lasewnlkjl():
187-
"""Юникод."""
201+
if sys.version_info[0] <= 2:
202+
@expect('D302: Use u""" for docstrings with Unicode')
203+
def lasewnlkjl():
204+
"""Юникод."""
188205

189206

190207
@expect("D400: First line should end with '.', not 'y'")
@@ -208,6 +225,6 @@ def run():
208225
results = list(pep257.check([__file__]))
209226
assert set(map(type, results)) == set([pep257.Error]), results
210227
results = set([(e.definition.name, e.message) for e in results])
211-
print('\nmissing: %r' % (results - errors))
212-
print('\n extra: %r' % (errors - results))
213-
assert errors == results
228+
print('\n extra: %r' % (results - expected))
229+
print('\nmissing: %r' % (expected - results))
230+
assert expected == results

0 commit comments

Comments
 (0)