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
python类suspend_hooks()的实例源码
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_urllib_imports_install_aliases(self):
with standard_library.suspend_hooks():
standard_library.install_aliases()
import urllib
import urllib.parse
import urllib.request
import urllib.robotparser
import urllib.error
import urllib.response
self.assertTrue(True)