def install_hooks():
"""
This function installs the future.standard_library import hook into
sys.meta_path.
"""
if PY3:
return
install_aliases()
flog.debug('sys.meta_path was: {0}'.format(sys.meta_path))
flog.debug('Installing hooks ...')
# Add it unless it's there already
newhook = RenameImport(RENAMES)
if not detect_hooks():
sys.meta_path.append(newhook)
flog.debug('sys.meta_path is now: {0}'.format(sys.meta_path))
python类install_hooks()的实例源码
def detect_hooks():
"""
Returns True if the import hooks are installed, False if not.
"""
flog.debug('Detecting hooks ...')
present = any([hasattr(hook, 'RENAMER') for hook in sys.meta_path])
if present:
flog.debug('Detected.')
else:
flog.debug('Not detected.')
return present
# As of v0.12, this no longer happens implicitly:
# if not PY3:
# install_hooks()
def test_remove_hooks_then_requests(self):
code = """
from future import standard_library
standard_library.install_hooks()
import builtins
import http.client
import html.parser
"""
with write_module(code, self.tempdir):
import test_imports_future_stdlib
standard_library.remove_hooks()
try:
import requests
except ImportError:
print("Requests doesn't seem to be available. Skipping requests test ...")
else:
r = requests.get('http://google.com')
self.assertTrue(r)
self.assertTrue(True)
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 detect_hooks():
"""
Returns True if the import hooks are installed, False if not.
"""
flog.debug('Detecting hooks ...')
present = any([hasattr(hook, 'RENAMER') for hook in sys.meta_path])
if present:
flog.debug('Detected.')
else:
flog.debug('Not detected.')
return present
# As of v0.12, this no longer happens implicitly:
# if not PY3:
# install_hooks()
def install_hooks():
"""
This function installs the future.standard_library import hook into
sys.meta_path.
"""
if PY3:
return
install_aliases()
flog.debug('sys.meta_path was: {0}'.format(sys.meta_path))
flog.debug('Installing hooks ...')
# Add it unless it's there already
newhook = RenameImport(RENAMES)
if not detect_hooks():
sys.meta_path.append(newhook)
flog.debug('sys.meta_path is now: {0}'.format(sys.meta_path))
def install_hooks():
"""
This function installs the future.standard_library import hook into
sys.meta_path.
"""
if PY3:
return
install_aliases()
flog.debug('sys.meta_path was: {0}'.format(sys.meta_path))
flog.debug('Installing hooks ...')
# Add it unless it's there already
newhook = RenameImport(RENAMES)
if not detect_hooks():
sys.meta_path.append(newhook)
flog.debug('sys.meta_path is now: {0}'.format(sys.meta_path))
def install_hooks():
"""
This function installs the future.standard_library import hook into
sys.meta_path.
"""
if PY3:
return
install_aliases()
flog.debug('sys.meta_path was: {0}'.format(sys.meta_path))
flog.debug('Installing hooks ...')
# Add it unless it's there already
newhook = RenameImport(RENAMES)
if not detect_hooks():
sys.meta_path.append(newhook)
flog.debug('sys.meta_path is now: {0}'.format(sys.meta_path))
def install_hooks():
"""
This function installs the future.standard_library import hook into
sys.meta_path.
"""
if PY3:
return
install_aliases()
flog.debug('sys.meta_path was: {0}'.format(sys.meta_path))
flog.debug('Installing hooks ...')
# Add it unless it's there already
newhook = RenameImport(RENAMES)
if not detect_hooks():
sys.meta_path.append(newhook)
flog.debug('sys.meta_path is now: {0}'.format(sys.meta_path))
def install_hooks():
"""
This function installs the future.standard_library import hook into
sys.meta_path.
"""
if PY3:
return
install_aliases()
flog.debug('sys.meta_path was: {0}'.format(sys.meta_path))
flog.debug('Installing hooks ...')
# Add it unless it's there already
newhook = RenameImport(RENAMES)
if not detect_hooks():
sys.meta_path.append(newhook)
flog.debug('sys.meta_path is now: {0}'.format(sys.meta_path))
def install_hooks():
"""
This function installs the future.standard_library import hook into
sys.meta_path.
"""
if PY3:
return
install_aliases()
flog.debug('sys.meta_path was: {0}'.format(sys.meta_path))
flog.debug('Installing hooks ...')
# Add it unless it's there already
newhook = RenameImport(RENAMES)
if not detect_hooks():
sys.meta_path.append(newhook)
flog.debug('sys.meta_path is now: {0}'.format(sys.meta_path))
def install_hooks():
"""
This function installs the future.standard_library import hook into
sys.meta_path.
"""
if PY3:
return
install_aliases()
flog.debug('sys.meta_path was: {0}'.format(sys.meta_path))
flog.debug('Installing hooks ...')
# Add it unless it's there already
newhook = RenameImport(RENAMES)
if not detect_hooks():
sys.meta_path.append(newhook)
flog.debug('sys.meta_path is now: {0}'.format(sys.meta_path))
def install_hooks():
"""
This function installs the future.standard_library import hook into
sys.meta_path.
"""
if PY3:
return
install_aliases()
flog.debug('sys.meta_path was: {0}'.format(sys.meta_path))
flog.debug('Installing hooks ...')
# Add it unless it's there already
newhook = RenameImport(RENAMES)
if not detect_hooks():
sys.meta_path.append(newhook)
flog.debug('sys.meta_path is now: {0}'.format(sys.meta_path))
def install_hooks():
"""
This function installs the future.standard_library import hook into
sys.meta_path.
"""
if PY3:
return
install_aliases()
flog.debug('sys.meta_path was: {0}'.format(sys.meta_path))
flog.debug('Installing hooks ...')
# Add it unless it's there already
newhook = RenameImport(RENAMES)
if not detect_hooks():
sys.meta_path.append(newhook)
flog.debug('sys.meta_path is now: {0}'.format(sys.meta_path))
def __enter__(self):
# flog.debug('Entering hooks context manager')
self.old_sys_modules = copy.copy(sys.modules)
self.hooks_were_installed = detect_hooks()
# self.scrubbed = scrub_py2_sys_modules()
install_hooks()
return self
def __exit__(self, *args):
if self.hooks_were_installed:
install_hooks()
# restore_sys_modules(self.scrubbed)
def enable_hooks():
"""
Deprecated. Use install_hooks() instead. This will be removed by
``future`` v1.0.
"""
install_hooks()
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())
def write_and_import(self, code, modulename='mymodule'):
self.assertTrue('.py' not in modulename)
filename = modulename + '.py'
if isinstance(code, bytes):
code = code.decode('utf-8')
# Be explicit about encoding the temp file as UTF-8 (issue #63):
with io.open(self.tempdir + filename, 'w', encoding='utf-8') as f:
f.write(textwrap.dedent(code).strip() + '\n')
# meta_path_len = len(sys.meta_path)
install_hooks(modulename)
# print('Hooks installed')
# assert len(sys.meta_path) == 1 + meta_path_len
# print('sys.meta_path is: {0}'.format(sys.meta_path))
module = None
sys.path.insert(0, self.tempdir)
try:
module = __import__(modulename)
except SyntaxError:
print('Bombed!')
else:
print('Succeeded!')
finally:
remove_hooks()
# print('Hooks removed')
sys.path.remove(self.tempdir)
return module
def test_import_future_standard_library(self):
"""
Does futurized Py3-like code like this work under autotranslation??
"""
code = """
from future import standard_library
standard_library.install_hooks()
import configparser
"""
module = self.write_and_import(code, 'future_standard_library')
self.assertTrue('configparser' in dir(module))
from future import standard_library
standard_library.remove_hooks()
fix_add_future_standard_library_import.py 文件源码
项目:packaging
作者: blockstack
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def transform(self, node, results):
# TODO: add a blank line between any __future__ imports and this?
touch_import_top(u'future', u'standard_library', node)
# TODO: also add standard_library.install_hooks()
def __enter__(self):
# flog.debug('Entering hooks context manager')
self.old_sys_modules = copy.copy(sys.modules)
self.hooks_were_installed = detect_hooks()
# self.scrubbed = scrub_py2_sys_modules()
install_hooks()
return self
def __exit__(self, *args):
if self.hooks_were_installed:
install_hooks()
# restore_sys_modules(self.scrubbed)
def enable_hooks():
"""
Deprecated. Use install_hooks() instead. This will be removed by
``future`` v1.0.
"""
install_hooks()
fix_add_future_standard_library_import.py 文件源码
项目:islam-buddy
作者: hamir
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def transform(self, node, results):
# TODO: add a blank line between any __future__ imports and this?
touch_import_top(u'future', u'standard_library', node)
# TODO: also add standard_library.install_hooks()
def __enter__(self):
# flog.debug('Entering hooks context manager')
self.old_sys_modules = copy.copy(sys.modules)
self.hooks_were_installed = detect_hooks()
# self.scrubbed = scrub_py2_sys_modules()
install_hooks()
return self
def __exit__(self, *args):
if self.hooks_were_installed:
install_hooks()
# restore_sys_modules(self.scrubbed)
def enable_hooks():
"""
Deprecated. Use install_hooks() instead. This will be removed by
``future`` v1.0.
"""
install_hooks()