1
- from cStringIO import StringIO
1
+ from __future__ import division , absolute_import , print_function
2
+
3
+ import sys
4
+ if sys .version_info [0 ] >= 3 :
5
+ from io import StringIO
6
+ else :
7
+ from io import StringIO
8
+
2
9
import compiler
3
10
import inspect
4
11
import textwrap
5
12
import tokenize
6
13
7
- from compiler_unparse import unparse
14
+ from . compiler_unparse import unparse
8
15
9
16
10
17
class Comment (object ):
11
-
12
18
""" A comment block.
13
19
"""
14
20
is_comment = True
15
-
16
21
def __init__ (self , start_lineno , end_lineno , text ):
17
22
# int : The first line number in the block. 1-indexed.
18
23
self .start_lineno = start_lineno
19
24
# int : The last line number. Inclusive!
20
25
self .end_lineno = end_lineno
21
- # str : The text block including '#' character but not any leading
22
- # spaces.
26
+ # str : The text block including '#' character but not any leading spaces.
23
27
self .text = text
24
28
25
29
def add (self , string , start , end , line ):
@@ -31,15 +35,13 @@ def add(self, string, start, end, line):
31
35
32
36
def __repr__ (self ):
33
37
return '%s(%r, %r, %r)' % (self .__class__ .__name__ , self .start_lineno ,
34
- self .end_lineno , self .text )
38
+ self .end_lineno , self .text )
35
39
36
40
37
41
class NonComment (object ):
38
-
39
42
""" A non-comment block of code.
40
43
"""
41
44
is_comment = False
42
-
43
45
def __init__ (self , start_lineno , end_lineno ):
44
46
self .start_lineno = start_lineno
45
47
self .end_lineno = end_lineno
@@ -54,14 +56,12 @@ def add(self, string, start, end, line):
54
56
55
57
def __repr__ (self ):
56
58
return '%s(%r, %r)' % (self .__class__ .__name__ , self .start_lineno ,
57
- self .end_lineno )
59
+ self .end_lineno )
58
60
59
61
60
62
class CommentBlocker (object ):
61
-
62
63
""" Pull out contiguous comment blocks.
63
64
"""
64
-
65
65
def __init__ (self ):
66
66
# Start with a dummy.
67
67
self .current_block = NonComment (0 , 0 )
@@ -75,7 +75,11 @@ def __init__(self):
75
75
def process_file (self , file ):
76
76
""" Process a file object.
77
77
"""
78
- for token in tokenize .generate_tokens (file .next ):
78
+ if sys .version_info [0 ] >= 3 :
79
+ nxt = file .__next__
80
+ else :
81
+ nxt = file .next
82
+ for token in tokenize .generate_tokens (nxt ):
79
83
self .process_token (* token )
80
84
self .make_index ()
81
85
@@ -160,6 +164,6 @@ def get_class_traits(klass):
160
164
if isinstance (node , compiler .ast .Assign ):
161
165
name = node .nodes [0 ].name
162
166
rhs = unparse (node .expr ).strip ()
163
- doc = strip_comment_marker (
164
- cb .search_for_comment (node .lineno , default = '' ))
167
+ doc = strip_comment_marker (cb .search_for_comment (node .lineno , default = '' ))
165
168
yield name , rhs , doc
169
+
0 commit comments