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

Commit 4fe27bd

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents f65c7a7 + 60990ce commit 4fe27bd

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@
5656
# built documents.
5757
#
5858
# The short X.Y version.
59-
version = '0.4.1'
59+
version = '0.5.0'
6060
# The full version, including alpha/beta/rc tags.
61-
release = '0.4.1'
61+
release = '0.5.0'
6262

6363
# The language for content autogenerated by Sphinx. Refer to documentation
6464
# for a list of supported languages.

pep257.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ def next(obj, default=nothing):
4949
return default
5050

5151

52+
# If possible (python >= 3.2) use tokenize.open to open files, so PEP 263
53+
# encoding markers are interpreted.
54+
try:
55+
tokenize_open = tk.open
56+
except AttributeError:
57+
tokenize_open = open
58+
59+
5260
__version__ = '0.5.0'
5361
__all__ = ('check', 'collect')
5462

@@ -671,7 +679,7 @@ def check(filenames, ignore=()):
671679
for filename in filenames:
672680
log.info('Checking file %s.', filename)
673681
try:
674-
with open(filename) as file:
682+
with tokenize_open(filename) as file:
675683
source = file.read()
676684
for error in PEP257Checker().check_source(source, filename):
677685
code = getattr(error, 'code', None)
@@ -988,11 +996,12 @@ def check_triple_double_quotes(self, definition, docstring):
988996
989997
'''
990998
if docstring and '"""' in eval(docstring) and docstring.startswith(
991-
("'''", "r'''", "u'''")):
999+
("'''", "r'''", "u'''", "ur'''")):
9921000
# Allow ''' quotes if docstring contains """, because otherwise """
9931001
# quotes could not be expressed inside docstring. Not in PEP 257.
9941002
return
995-
if docstring and not docstring.startswith(('"""', 'r"""', 'u"""')):
1003+
if docstring and not docstring.startswith(
1004+
('"""', 'r"""', 'u"""', 'ur"""')):
9961005
quotes = "'''" if "'''" in docstring[:4] else "'"
9971006
return D300(quotes)
9981007

@@ -1006,7 +1015,8 @@ def check_backslashes(self, definition, docstring):
10061015
'''
10071016
# Just check that docstring is raw, check_triple_double_quotes
10081017
# ensures the correct quotes.
1009-
if docstring and '\\' in docstring and not docstring.startswith('r'):
1018+
if docstring and '\\' in docstring and not docstring.startswith(
1019+
('r', 'ur')):
10101020
return D301()
10111021

10121022
@check_for(Definition)
@@ -1019,7 +1029,8 @@ def check_unicode_docstring(self, definition, docstring):
10191029
# Just check that docstring is unicode, check_triple_double_quotes
10201030
# ensures the correct quotes.
10211031
if docstring and sys.version_info[0] <= 2:
1022-
if not is_ascii(docstring) and not docstring.startswith('u'):
1032+
if not is_ascii(docstring) and not docstring.startswith(
1033+
('u', 'ur')):
10231034
return D302()
10241035

10251036
@check_for(Definition)

test_pep257.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import with_statement
66

7+
import sys
78
import os
89
import mock
910
import shutil
@@ -82,7 +83,7 @@ def test_ignore_list():
8283
expected_error_codes = set(('D100', 'D400', 'D401', 'D205', 'D209',
8384
'D210'))
8485
mock_open = mock.mock_open(read_data=function_to_check)
85-
with mock.patch('pep257.open', mock_open, create=True):
86+
with mock.patch('pep257.tokenize_open', mock_open, create=True):
8687
errors = tuple(pep257.check(['filepath']))
8788
error_codes = set(error.code for error in errors)
8889
assert error_codes == expected_error_codes
@@ -144,3 +145,27 @@ def foo():
144145

145146
out, err = env.invoke_pep257(args='--count')
146147
assert '2' in out
148+
149+
150+
def test_unicode_raw():
151+
"""Test acceptance of unicode raw docstrings for python 2.x."""
152+
if sys.version_info[0] >= 3:
153+
return # ur"" is a syntax error in python 3.x
154+
155+
# This is all to avoid a syntax error for python 3.2
156+
from codecs import unicode_escape_decode
157+
158+
def u(x):
159+
return unicode_escape_decode(x)[0]
160+
161+
with Pep257Env() as env:
162+
with env.open('example.py', 'wt') as example:
163+
example.write(textwrap.dedent(u('''\
164+
# -*- coding: utf-8 -*-
165+
def foo():
166+
ur"""Check unicode: \u2611 and raw: \\\\\\\\."""
167+
''').encode('utf-8')))
168+
env.write_config(ignore='D100', verbose=True)
169+
out, err = env.invoke_pep257()
170+
assert 'D301' not in err
171+
assert 'D302' not in err

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
envlist = py26, py27, py32, py33, py34, pypy, pypy3
88

99
[testenv]
10+
# Make sure reading the UTF-8 from test.py works regardless of the locale used.
11+
setenv = LANG=C LC_ALL=C
1012
commands = py.test --pep8 --clearcache
1113
deps = pytest
1214
pytest-pep8

0 commit comments

Comments
 (0)