forked from PythonCharmers/python-future
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pasteurize.py
More file actions
233 lines (198 loc) · 6.76 KB
/
Copy pathtest_pasteurize.py
File metadata and controls
233 lines (198 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""
This module contains snippets of Python 3 code (invalid Python 2) and
tests for whether they can be passed to ``pasteurize`` and
immediately run under both Python 2 and Python 3.
"""
from __future__ import print_function, absolute_import
import pprint
from subprocess import Popen, PIPE
import tempfile
import os
from future.tests.base import CodeHandler, unittest, skip26
class TestPasteurize(CodeHandler):
"""
After running ``pasteurize``, these Python 3 code snippets should run
on both Py3 and Py2.
"""
@skip26 # Python 2.6's lib2to3 causes the "from future.builtins import
# range" line to be stuck at the bottom of the module!
def test_range_slice(self):
"""
After running ``pasteurize``, this Python 3 code should run
quickly on both Py3 and Py2 without a MemoryError
"""
code = '''
for i in range(10**15)[:10]:
pass
'''
self.unchanged(code, from3=True)
def test_print(self):
"""
This Python 3-only code is a SyntaxError on Py2 without the
print_function import from __future__.
"""
code = '''
import sys
print('Hello', file=sys.stderr)
'''
self.unchanged(code, from3=True)
def test_division(self):
"""
True division should not be screwed up by conversion from 3 to both
"""
code = '''
x = 3 / 2
assert x == 1.5
'''
self.unchanged(code, from3=True)
@unittest.expectedFailure
def test_exception_indentation(self):
"""
As of v0.11.2, pasteurize broke the indentation of ``raise`` statements
using with_traceback. Test for this.
"""
before = '''
import sys
if True:
try:
'string' + 1
except TypeError:
ty, va, tb = sys.exc_info()
raise TypeError("can't do that!").with_traceback(tb)
'''
after = '''
import sys
from future.utils import raise_with_traceback
if True:
try:
'string' + 1
except TypeError:
ty, va, tb = sys.exc_info()
raise_with_traceback(TypeError("can't do that!"), tb)
'''
self.convert_check(before, after, from3=True)
# TODO: fix and test this test
def test_urllib_request(self):
"""
Example Python 3 code using the new urllib.request module.
Does the ``pasteurize`` script handle this?
"""
before = """
import pprint
import urllib.request
URL = 'http://pypi.python.org/pypi/{}/json'
package = 'future'
r = urllib.request.urlopen(URL.format(package))
pprint.pprint(r.read())
"""
after = """
import pprint
import future.standard_library.urllib.request as urllib_request
URL = 'http://pypi.python.org/pypi/{}/json'
package = 'future'
r = urllib_request.urlopen(URL.format(package))
pprint.pprint(r.read())
"""
self.convert_check(before, after, from3=True)
def test_urllib_refactor2(self):
before = """
import urllib.request, urllib.parse
f = urllib.request.urlopen(url, timeout=15)
filename = urllib.parse.urlparse(url)[2].split('/')[-1]
"""
after = """
from future.standard_library.urllib import request as urllib_request
from future.standard_library.urllib import parse as urllib_parse
f = urllib_request.urlopen(url, timeout=15)
filename = urllib_parse.urlparse(url)[2].split('/')[-1]
"""
class TestFuturizeAnnotations(CodeHandler):
@unittest.expectedFailure
def test_return_annotations_alone(self):
before = "def foo() -> 'bar': pass"
after = """
def foo(): pass
foo.__annotations__ = {'return': 'bar'}
"""
self.convert_check(before, after, from3=True)
b = """
def foo() -> "bar":
print "baz"
print "what's next, again?"
"""
a = """
def foo():
print "baz"
print "what's next, again?"
"""
self.convert_check(b, a, from3=True)
@unittest.expectedFailure
def test_single_param_annotations(self):
b = "def foo(bar:'baz'): pass"
a = """
def foo(bar): pass
foo.__annotations__ = {'bar': 'baz'}
"""
self.convert_check(b, a, from3=True)
b = """
def foo(bar:"baz"="spam"):
print("what's next, again?")
print("whatever.")
"""
a = """
def foo(bar="spam"):
print("what's next, again?")
print("whatever.")
foo.__annotations__ = {'bar': 'baz'}
"""
self.convert_check(b, a, from3=True)
def test_multiple_param_annotations(self):
b = "def foo(bar:'spam'=False, baz:'eggs'=True, ham:False='spaghetti'): pass"
a = "def foo(bar=False, baz=True, ham='spaghetti'): pass"
self.convert_check(b, a, from3=True)
b = """
def foo(bar:"spam"=False, baz:"eggs"=True, ham:False="spam"):
print("this is filler, just doing a suite")
print("suites require multiple lines.")
"""
a = """
def foo(bar=False, baz=True, ham="spam"):
print("this is filler, just doing a suite")
print("suites require multiple lines.")
"""
self.convert_check(b, a, from3=True)
def test_mixed_annotations(self):
b = "def foo(bar=False, baz:'eggs'=True, ham:False='spaghetti') -> 'zombies': pass"
a = "def foo(bar=False, baz=True, ham='spaghetti'): pass"
self.convert_check(b, a, from3=True)
b = """
def foo(bar:"spam"=False, baz=True, ham:False="spam") -> 'air':
print("this is filler, just doing a suite")
print("suites require multiple lines.")
"""
a = """
def foo(bar=False, baz=True, ham="spam"):
print("this is filler, just doing a suite")
print("suites require multiple lines.")
"""
self.convert_check(b, a, from3=True)
b = "def foo(bar) -> 'brains': pass"
a = "def foo(bar): pass"
self.convert_check(b, a, from3=True)
def test_functions_unchanged(self):
s = "def foo(): pass"
self.unchanged(s, from3=True)
s = """
def foo():
pass
pass
"""
self.unchanged(s, from3=True)
s = """
def foo(bar='baz'):
pass
pass
"""
self.unchanged(s, from3=True)
if __name__ == '__main__':
unittest.main()