Repository navigation
Expand file tree
/
Copy pathtest_standard_library.py
More file actions
396 lines (340 loc) · 12.9 KB
/
Copy pathtest_standard_library.py
File metadata and controls
396 lines (340 loc) · 12.9 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""
Tests for the future.standard_library module
"""
from __future__ import absolute_import, unicode_literals, print_function
from future import standard_library
from future import utils
from future.tests.base import unittest, CodeHandler
import sys
import tempfile
import os
import copy
import textwrap
from subprocess import CalledProcessError
class TestStandardLibraryRenames(CodeHandler):
def setUp(self):
self.interpreter = 'python'
standard_library.install_hooks()
super(TestStandardLibraryRenames, self).setUp()
def tearDown(self):
standard_library.remove_hooks()
def test_can_import_several(self):
"""
This test fails if e.g. future/standard_library/email/header.py contains:
from future import standard_library
standard_library.remove_hooks(keep_sys_modules=True)
"""
import future.standard_library.urllib.parse as urllib_parse
import future.standard_library.urllib.request as urllib_request
from future.standard_library.test import support
with standard_library.hooks():
import http.server
for m in [urllib_parse, urllib_request, http.server, support]:
self.assertTrue(m is not None)
def test_is_py2_stdlib_module(self):
"""
Tests whether the internal is_py2_stdlib_module function (called by the
sys.modules scrubbing functions) is reliable.
"""
externalmodules = [standard_library, utils]
self.assertTrue(not any([standard_library.is_py2_stdlib_module(module)
for module in externalmodules]))
py2modules = [sys, tempfile, os, copy, textwrap]
if utils.PY2:
self.assertTrue(all([standard_library.is_py2_stdlib_module(module)
for module in py2modules]))
else:
self.assertTrue(
not any ([standard_library.is_py2_stdlib_module(module)
for module in py2modules]))
@unittest.skipIf(utils.PY3, 'generic import tests are for Py2 only')
def test_all(self):
"""
Tests whether all of the old imports in RENAMES are accessible
under their new names.
"""
for (oldname, newname) in standard_library.RENAMES.items():
if newname == 'winreg' and sys.platform not in ['win32', 'win64']:
continue
if newname in standard_library.REPLACED_MODULES:
# Skip this check for e.g. the stdlib's ``test`` module,
# which we have replaced completely.
continue
oldmod = __import__(oldname)
newmod = __import__(newname)
if '.' not in oldname:
self.assertEqual(oldmod, newmod)
def test_suspend_hooks(self):
"""
Code like the try/except block here appears in Pyflakes v0.6.1. This
method tests whether suspend_hooks() works as advertised.
"""
example_PY2_check = False
with standard_library.suspend_hooks():
# An example of fragile import code that we don't want to break:
try:
import builtins
except ImportError:
example_PY2_check = True
if utils.PY2:
self.assertTrue(example_PY2_check)
else:
self.assertFalse(example_PY2_check)
# The import should succeed again now:
import builtins
def test_disable_hooks(self):
"""
Tests the old (deprecated) names. These deprecated aliases should be
removed by version 1.0
"""
example_PY2_check = False
standard_library.enable_hooks() # deprecated name
old_meta_path = copy.copy(sys.meta_path)
standard_library.disable_hooks()
standard_library.scrub_future_sys_modules()
if utils.PY2:
self.assertTrue(len(old_meta_path) == len(sys.meta_path) + 1)
else:
self.assertTrue(len(old_meta_path) == len(sys.meta_path))
# An example of fragile import code that we don't want to break:
try:
import builtins
except ImportError:
example_PY2_check = True
if utils.PY2:
self.assertTrue(example_PY2_check)
else:
self.assertFalse(example_PY2_check)
standard_library.install_hooks()
# Imports should succeed again now:
import builtins
import configparser
if utils.PY2:
self.assertTrue(standard_library.detect_hooks())
self.assertTrue(len(old_meta_path) == len(sys.meta_path))
def test_remove_hooks2(self):
"""
As above, but with the new names
"""
example_PY2_check = False
standard_library.install_hooks()
old_meta_path = copy.copy(sys.meta_path)
standard_library.remove_hooks()
standard_library.scrub_future_sys_modules()
if utils.PY2:
self.assertTrue(len(old_meta_path) == len(sys.meta_path) + 1)
else:
self.assertTrue(len(old_meta_path) == len(sys.meta_path))
# An example of fragile import code that we don't want to break:
try:
import builtins
except ImportError:
example_PY2_check = True
if utils.PY2:
self.assertTrue(example_PY2_check)
else:
self.assertFalse(example_PY2_check)
standard_library.install_hooks()
# The import should succeed again now:
import builtins
self.assertTrue(len(old_meta_path) == len(sys.meta_path))
def test_detect_hooks(self):
"""
Tests whether the future.standard_library.detect_hooks is doing
its job.
"""
standard_library.install_hooks()
if utils.PY2:
self.assertTrue(standard_library.detect_hooks())
meta_path = copy.copy(sys.meta_path)
standard_library.remove_hooks()
if utils.PY2:
self.assertEqual(len(meta_path), len(sys.meta_path) + 1)
self.assertFalse(standard_library.detect_hooks())
@unittest.skipIf(utils.PY3, 'not testing for old urllib on Py3')
def test_old_urllib_import(self):
"""
Tests whether an imported module can import the old urllib package.
Importing future.standard_library in a script should be possible and
not disrupt any uses of the old Py2 standard library names in modules
imported by that script.
"""
code1 = '''
from future import standard_library
with standard_library.suspend_hooks():
import module_importing_old_urllib
'''
self._write_test_script(code1, 'runme.py')
code2 = '''
import urllib
assert 'urlopen' in dir(urllib)
print('Import succeeded!')
'''
self._write_test_script(code2, 'module_importing_old_urllib.py')
output = self._run_test_script('runme.py')
print(output)
self.assertTrue(True)
def test_sys_intern(self):
"""
Py2's builtin intern() has been moved to the sys module. Tests
whether sys.intern is available.
"""
from sys import intern
if utils.PY3:
self.assertEqual(intern('hello'), 'hello')
else:
# intern() requires byte-strings on Py2:
self.assertEqual(intern(b'hello'), b'hello')
def test_sys_maxsize(self):
"""
Tests whether sys.maxsize is available.
"""
from sys import maxsize
self.assertTrue(maxsize > 0)
def test_itertools_filterfalse(self):
"""
Tests whether itertools.filterfalse is available.
"""
from itertools import filterfalse
not_div_by_3 = filterfalse(lambda x: x % 3 == 0, range(8))
self.assertEqual(list(not_div_by_3), [1, 2, 4, 5, 7])
def test_itertools_zip_longest(self):
"""
Tests whether itertools.zip_longest is available.
"""
from itertools import zip_longest
a = (1, 2)
b = [2, 4, 6]
self.assertEqual(list(zip_longest(a, b)),
[(1, 2), (2, 4), (None, 6)])
def test_import_failure_from_module(self):
"""
Tests whether e.g. "import socketserver" succeeds in a module
imported by another module that has used and removed the stdlib hooks.
We want this to fail; the stdlib hooks should not bleed to imported
modules too without their explicitly invoking them.
"""
code1 = '''
from future import standard_library
standard_library.install_hooks()
standard_library.remove_hooks()
import importme2
'''
code2 = '''
import socketserver
print('Uh oh. importme2 should have raised an ImportError.')
'''
self._write_test_script(code1, 'importme1.py')
self._write_test_script(code2, 'importme2.py')
with self.assertRaises(CalledProcessError):
output = self._run_test_script('importme1.py')
def test_configparser(self):
import configparser
def test_copyreg(self):
import copyreg
def test_pickle(self):
import pickle
def test_profile(self):
import profile
def test_stringio(self):
from io import StringIO
s = StringIO('test')
for method in ['tell', 'read', 'seek', 'close', 'flush']:
self.assertTrue(hasattr(s, method))
def test_bytesio(self):
from io import BytesIO
s = BytesIO(b'test')
for method in ['tell', 'read', 'seek', 'close', 'flush', 'getvalue']:
self.assertTrue(hasattr(s, method))
def test_queue(self):
import queue
q = queue.Queue()
q.put('thing')
self.assertFalse(q.empty())
def test_reprlib(self):
import reprlib
self.assertTrue(True)
def test_socketserver(self):
import socketserver
self.assertTrue(True)
@unittest.skip("Not testing tkinter import (it may be installed separately from Python)")
def test_tkinter(self):
import tkinter
self.assertTrue(True)
def test_builtins(self):
import builtins
self.assertTrue(hasattr(builtins, 'tuple'))
@unittest.skip("ssl support has been stripped out for now ...")
def test_urllib_request_ssl_redirect(self):
"""
This site redirects to https://...
It therefore requires ssl support.
"""
import future.standard_library.urllib.request as urllib_request
from pprint import pprint
URL = 'http://pypi.python.org/pypi/{0}/json'
package = 'future'
r = urllib_request.urlopen(URL.format(package))
# pprint(r.read().decode('utf-8'))
self.assertTrue(True)
def test_urllib_request_http(self):
"""
This site (amazon.com) uses plain http (as of 2014-04-12).
"""
import future.standard_library.urllib.request as urllib_request
from pprint import pprint
URL = 'http://amazon.com'
r = urllib_request.urlopen(URL)
data = r.read()
self.assertTrue(b'<html>' in data)
def test_html_import(self):
import html
import html.entities
import html.parser
self.assertTrue(True)
def test_http_client_import(self):
import http.client
self.assertTrue(True)
def test_other_http_imports(self):
import http
import http.server
import http.cookies
import http.cookiejar
self.assertTrue(True)
def test_urllib_imports(self):
import future.standard_library.urllib
import future.standard_library.urllib.parse
import future.standard_library.urllib.request
import future.standard_library.urllib.robotparser
import future.standard_library.urllib.error
import future.standard_library.urllib.response
self.assertTrue(True)
def test_underscore_prefixed_modules(self):
import _thread
import _dummy_thread
import _markupbase
self.assertTrue(True)
def test_reduce(self):
"""
reduce has been moved to the functools module
"""
import functools
self.assertEqual(functools.reduce(lambda x, y: x+y, range(1, 6)), 15)
def test_collections_userstuff(self):
"""
UserDict, UserList, and UserString have been moved to the
collections module.
"""
from collections import UserDict
from collections import UserList
from collections import UserString
self.assertTrue(True)
def test_reload(self):
"""
reload has been moved to the imp module
"""
import imp
imp.reload(imp)
self.assertTrue(True)
if __name__ == '__main__':
unittest.main()