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

Commit 72336fd

Browse files
committed
Tests pass. Now we need to make them beatiful...
1 parent d630a13 commit 72336fd

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

pep257.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from optparse import OptionParser
2222
from re import compile as re
2323
import itertools
24+
from collections import defaultdict
2425

2526
try: # Python 3.x
2627
from ConfigParser import RawConfigParser
@@ -131,7 +132,7 @@ def __str__(self):
131132
class Module(Definition):
132133

133134
_fields = ('name', '_source', 'start', 'end', 'decorators', 'docstring',
134-
'children', 'parent', '_all', 'unicode_literals')
135+
'children', 'parent', '_all', 'future_imports')
135136
is_public = True
136137
_nest = staticmethod(lambda s: {'def': Function, 'class': Class}[s])
137138
module = property(lambda self: self)
@@ -197,7 +198,7 @@ class Decorator(Value):
197198

198199
class TokenKind(int):
199200
def __repr__(self):
200-
return "tk.{}".format(tk.tok_name[self])
201+
return "tk.{0}".format(tk.tok_name[self])
201202

202203

203204
class Token(Value):
@@ -252,7 +253,7 @@ def __call__(self, filelike, filename):
252253
self.filename = filename
253254
self.all = None
254255
# TODO: what about Python 3.x?
255-
self.unicode_literals = False
256+
self.future_imports = defaultdict(lambda: False)
256257
self._accumulated_decorators = []
257258
return self.parse_module()
258259

@@ -414,7 +415,7 @@ def parse_module(self):
414415
[], docstring, children, None, self.all)
415416
for child in module.children:
416417
child.parent = module
417-
module.unicode_literals = self.unicode_literals
418+
module.future_imports = self.future_imports
418419
log.debug("finished parsing module.")
419420
return module
420421

@@ -479,9 +480,29 @@ def parse_from_import_statement(self):
479480
self.stream.move()
480481
assert self.current.value == 'import', self.current.value
481482
self.stream.move()
482-
# TODO: lookout for parenthesis, comments, line breaks, etc.
483-
if self.current.value == 'unicode_literals':
484-
self.unicode_literals = True
483+
if self.current.value == '(':
484+
self.consume(tk.OP)
485+
expected_end_kind = tk.OP
486+
else:
487+
expected_end_kind = tk.NEWLINE
488+
while self.current.kind != expected_end_kind:
489+
if self.current.kind != tk.NAME:
490+
self.stream.move()
491+
continue
492+
log.debug("parsing import, token is %r (%s)",
493+
self.current.kind, self.current.value)
494+
log.debug('found future import: %s', self.current.value)
495+
self.future_imports[self.current.value] = True
496+
self.consume(tk.NAME)
497+
log.debug("parsing import, token is %r (%s)",
498+
self.current.kind, self.current.value)
499+
if self.current.kind == tk.NAME:
500+
self.consume(tk.NAME) # as
501+
self.consume(tk.NAME) # new name, irrelevant
502+
if self.current.value == ',':
503+
self.consume(tk.OP)
504+
log.debug("parsing import, token is %r (%s)",
505+
self.current.kind, self.current.value)
485506

486507

487508
class Error(object):
@@ -1146,7 +1167,7 @@ def check_unicode_docstring(self, definition, docstring):
11461167
For Unicode docstrings, use u"""Unicode triple-quoted strings""".
11471168
11481169
'''
1149-
if definition.module.unicode_literals:
1170+
if definition.module.future_imports['unicode_literals']:
11501171
return
11511172

11521173
# Just check that docstring is unicode, check_triple_double_quotes

test_definitions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ def nested_3(self):
4242

4343
def test_parser():
4444
dunder_all = ('a', 'bc')
45-
unicode_literals = False
4645
module = parse(StringIO(source), 'file.py')
4746
assert len(list(module)) == 8
4847
assert Module('file.py', _, 1, len(source.split('\n')),
49-
_, '"""Module."""', _, _, dunder_all, unicode_literals) == \
48+
_, '"""Module."""', _, _, dunder_all, {}) == \
5049
module
5150

5251
function, class_ = module.children
@@ -75,17 +74,18 @@ def test_parser():
7574

7675
module = parse(StringIO(source_alt), 'file_alt.py')
7776
assert Module('file_alt.py', _, 1, len(source_alt.split('\n')),
78-
_, None, _, _, dunder_all, unicode_literals) == module
77+
_, None, _, _, dunder_all, {}) == module
7978

8079
module = parse(StringIO(source_alt_nl_at_bracket), 'file_alt_nl.py')
8180
assert Module('file_alt_nl.py', _, 1,
8281
len(source_alt_nl_at_bracket.split('\n')), _, None, _, _,
83-
dunder_all, unicode_literals) == module
82+
dunder_all, {}) == module
8483

8584
module = parse(StringIO(source_unicode_literals), 'file_ucl.py')
8685
assert Module('file_ucl.py', _, 1,
8786
_, _, None, _, _,
88-
_, True) == module
87+
_, {'unicode_literals': True}) == module
88+
assert module.future_imports['unicode_literals']
8989

9090

9191
def _test_module():

test_pep257.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def invoke_pep257(self, args=""):
5757
cmd = shlex.split("python {0} {1} {2}"
5858
.format(pep257_location, self.tempdir, args),
5959
posix=False)
60-
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
60+
p = subprocess.Popen(cmd,
61+
stdout=subprocess.PIPE,
6162
stderr=subprocess.PIPE)
6263
out, err = p.communicate()
6364
return self.Result(out=out.decode('utf-8'),
@@ -72,6 +73,7 @@ def __enter__(self):
7273

7374
def __exit__(self, *args, **kwargs):
7475
shutil.rmtree(self.tempdir)
76+
pass
7577

7678

7779
def test_pep257_conformance():
@@ -315,7 +317,7 @@ def u(x):
315317
with env.open('example.py', 'wt') as example:
316318
example.write(textwrap.dedent(u('''\
317319
# -*- coding: utf-8 -*-
318-
"""This is a modue."""
320+
"""This is a module."""
319321
320322
def foo():
321323
"""Check unicode: \u2611."""
@@ -346,7 +348,7 @@ def foo():
346348
"""This is a module."""
347349
348350
from __future__ import (nested_scopes as ns,
349-
unicode_literals)
351+
unicode_literals)
350352
351353
def foo():
352354
"""Check unicode: \u2611."""

0 commit comments

Comments
 (0)