@@ -83,7 +83,6 @@ def doctest_markup(in_lines):
83
83
84
84
* ``from StringIO import StringIO as BytesIO`` replaced by ``from io import
85
85
BytesIO``.
86
- * ``from StringIO import StringIO`` replaced by ``from io import StringIO``.
87
86
88
87
Next it looks to see if the line ends with a comment starting with ``#2to3:``.
89
88
@@ -94,8 +93,8 @@ def doctest_markup(in_lines):
94
93
a variable referring to the current line number, and ``next`` is just
95
94
``here+1``.
96
95
* <expr> is a python3 expression returning a processed value, where
97
- ``line`` contains the line number referred to by ``here``, and ``lines``
98
- is a list of all lines, such that ``lines[here]`` gives the value of
96
+ ``line`` contains the line referred to by line number ``here``, and
97
+ ``lines`` is a list of all lines. ``lines[here]`` gives the value of
99
98
``line``.
100
99
101
100
An <expr> beginning with "replace(" we take to be short for "line.replace(".
@@ -113,22 +112,28 @@ def doctest_markup(in_lines):
113
112
114
113
Examples
115
114
--------
116
- The next three lines all do the same thing. The # at the beginning disables
117
- them as runnable doctests in this docstring.
115
+ The next three lines all do the same thing:
118
116
119
- # > >> a = '1234567890' #2to3: here; line.replace("'12", "b'12")
120
- # > >> a = '1234567890' #2to3: here; replace("'12", "b'12")
121
- # > >> a = '1234567890' #2to3: here; bytes
117
+ >> a = '1234567890' #2to3: here; line.replace("'12", "b'12")
118
+ >> a = '1234567890' #2to3: here; replace("'12", "b'12")
119
+ >> a = '1234567890' #2to3: here; bytes
122
120
123
- You might want to process the next line
121
+ and that is to give the output (e.g):
124
122
125
- # >>> upk.unpack('2s') #2to3: next; bytes
126
- # ('12',)
123
+ >> a = b'1234567890' #2to3: here; line.replace("'12", "b'12")
124
+
125
+ in the processed doctest.
126
+
127
+ You might want to process the line after the comment - such as test output.
128
+ The next test replaces "'a string'" with "b'a string'"
129
+
130
+ >> 'a string'.encode('ascii') #2to3: next; bytes
131
+ 'a string'
127
132
128
133
This might work too, to do the same thing:
129
134
130
- # >>> upk.unpack('2s ') #2to3: here+1; bytes
131
- # ('12',)
135
+ >> 'a string'.encode('ascii ') #2to3: here+1; bytes
136
+ 'a string'
132
137
"""
133
138
pos = 0
134
139
lines = list (in_lines )
@@ -149,42 +154,45 @@ def doctest_markup(in_lines):
149
154
continue
150
155
docbits , start , marker , markup = mark_match .groups ()
151
156
pe_match = PLACE_EXPR .match (markup )
152
- if pe_match :
153
- place , expr = pe_match .groups ()
157
+ if pe_match is None :
158
+ continue
159
+ # Get place, expr expressions
160
+ place , expr = pe_match .groups ()
161
+ try :
162
+ place = eval (place , {'here' : here , 'next' : here + 1 })
163
+ except :
164
+ print ('Error finding place with "%s", line "%s"; skipping' %
165
+ (place , this ))
166
+ continue
167
+ # Prevent processing operating on 2to3 comment part of line
168
+ if place == here :
169
+ line = start
170
+ else :
171
+ line = lines [place ]
172
+ # Process expr
173
+ expr = expr .strip ()
174
+ # Shorthand
175
+ if expr == 'bytes' :
176
+ # Any strings on the given line are byte strings
177
+ pre , mid , post = INDENT_SPLITTER .match (line ).groups ()
178
+ res = byter (mid )
179
+ res = pre + res + post
180
+ else :
181
+ # If expr starts with 'replace', implies "line.replace"
182
+ if expr .startswith ('replace(' ):
183
+ expr = 'line.' + expr
154
184
try :
155
- place = eval (place , {'here' : here , 'next' : here + 1 })
185
+ res = eval (expr , dict (line = line ,
186
+ lines = lines ))
156
187
except :
157
- print ('Error finding place with "%s", line "%s"; skipping' %
158
- ( place , this ))
188
+ print ('Error working on "%s" at line %d with "%s"; skipping' %
189
+ ( line , place , expr ))
159
190
continue
160
- # Prevent processing operating on comment
161
- if place == here :
162
- line = start
163
- else :
164
- line = lines [place ]
165
- expr = expr .strip ()
166
- # Shorthand stuff
167
- if expr == 'bytes' :
168
- # Any strings on the given line are byte strings
169
- pre , mid , post = INDENT_SPLITTER .match (line ).groups ()
170
- res = byter (mid )
171
- res = pre + res + post
172
- else :
173
- # If expr starts with 'replace', implies "line.replace"
174
- if expr .startswith ('replace(' ):
175
- expr = 'line.' + expr
176
- try :
177
- res = eval (expr , dict (line = line ,
178
- lines = lines ))
179
- except :
180
- print ('Error working on "%s" at line %d with "%s"; skipping' %
181
- (line , place , expr ))
182
- continue
183
- # Put back comment if removed
184
- if place == here :
185
- res = docbits + res + marker + markup
186
- if res != line :
187
- lines [place ] = res
191
+ # Put back comment if removed
192
+ if place == here :
193
+ res = docbits + res + marker + markup
194
+ if res != line :
195
+ lines [place ] = res
188
196
return lines
189
197
190
198
0 commit comments