Skip to content

Commit d6f0fdc

Browse files
committed
DOC+RF - reorganizing comments and tests for python 2 skipping
1 parent 7bd4a0a commit d6f0fdc

File tree

2 files changed

+68
-48
lines changed

2 files changed

+68
-48
lines changed

nisext/py3builder.py

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ def doctest_markup(in_lines):
8383
8484
* ``from StringIO import StringIO as BytesIO`` replaced by ``from io import
8585
BytesIO``.
86-
* ``from StringIO import StringIO`` replaced by ``from io import StringIO``.
8786
8887
Next it looks to see if the line ends with a comment starting with ``#2to3:``.
8988
@@ -94,8 +93,8 @@ def doctest_markup(in_lines):
9493
a variable referring to the current line number, and ``next`` is just
9594
``here+1``.
9695
* <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
9998
``line``.
10099
101100
An <expr> beginning with "replace(" we take to be short for "line.replace(".
@@ -113,22 +112,28 @@ def doctest_markup(in_lines):
113112
114113
Examples
115114
--------
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:
118116
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
122120
123-
You might want to process the next line
121+
and that is to give the output (e.g):
124122
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'
127132
128133
This might work too, to do the same thing:
129134
130-
# >>> upk.unpack('2s') #2to3: here+1; bytes
131-
# ('12',)
135+
>> 'a string'.encode('ascii') #2to3: here+1; bytes
136+
'a string'
132137
"""
133138
pos = 0
134139
lines = list(in_lines)
@@ -149,42 +154,45 @@ def doctest_markup(in_lines):
149154
continue
150155
docbits, start, marker, markup = mark_match.groups()
151156
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
154184
try:
155-
place = eval(place, {'here': here, 'next': here+1})
185+
res = eval(expr, dict(line=line,
186+
lines=lines))
156187
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))
159190
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
188196
return lines
189197

190198

nisext/tests/test_doctest_markup.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
""" Testing doctest markup tests
22
"""
33

4+
import sys
45
from ..py3builder import doctest_markup, byter
56

6-
from numpy.testing import (assert_array_almost_equal,
7-
assert_array_equal)
7+
from numpy.testing import (assert_array_almost_equal, assert_array_equal, dec)
88

99
from nose.tools import assert_true, assert_equal, assert_raises
1010

11+
is_2 = sys.version_info[0] < 3
12+
skip42 = dec.skipif(is_2)
13+
1114

1215
def test_search_replace():
1316
# Test search and replace regexps
@@ -20,6 +23,9 @@ def test_search_replace():
2023
assert_equal(['>>> from io import BytesIO'], doctest_markup([line]))
2124
line = line + '\n'
2225
assert_equal(['>>> from io import BytesIO\n'], doctest_markup([line]))
26+
27+
28+
def test_replace_markup():
2329
# Bytes output
2430
marked_lines = ['any', ' some']
2531
assert_equal(marked_lines, doctest_markup(marked_lines))
@@ -45,13 +51,19 @@ def test_search_replace():
4551
assert_equal(['>>> woo #2to3: here ; replace("wow", "woo") '],
4652
doctest_markup(
4753
['>>> wow #2to3: here ; replace("wow", "woo") ']))
54+
55+
56+
@skip42
57+
def test_byte_markup():
4858
assert_equal([">>> b'hello' #2to3: here; bytes"],
4959
doctest_markup(
5060
[">>> 'hello' #2to3: here; bytes"]))
5161
assert_equal(['>>> some #2to3: next; bytes\n', " b'TRACK'\n"],
5262
doctest_markup(
5363
['>>> some #2to3: next; bytes\n', " 'TRACK'\n"]))
5464

65+
66+
@skip42
5567
def test_byter():
5668
# Test bytes formatter
5769
assert_equal('(b"hello \' world", b\'again\')',

0 commit comments

Comments
 (0)