我们从Python开源项目中,提取了以下38个代码示例,用于说明如何使用future.standard_library.install_aliases()。
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 test_urllib_refactor(self): # Code like this using urllib is refactored by futurize --stage2 to use # the new Py3 module names, but ``future`` doesn't support urllib yet. before = """ import urllib URL = 'http://pypi.python.org/pypi/future/json' package = 'future' r = urllib.urlopen(URL.format(package)) data = r.read() """ after = """ from future import standard_library standard_library.install_aliases() import urllib.request URL = 'http://pypi.python.org/pypi/future/json' package = 'future' r = urllib.request.urlopen(URL.format(package)) data = r.read() """ self.convert_check(before, after)
def strip_future_imports(self, code): """ Strips any of these import lines: from __future__ import <anything> from future <anything> from future.<anything> from builtins <anything> or any line containing: install_hooks() or: install_aliases() Limitation: doesn't handle imports split across multiple lines like this: from __future__ import (absolute_import, division, print_function, unicode_literals) """ output = [] # We need .splitlines(keepends=True), which doesn't exist on Py2, # so we use this instead: for line in code.split('\n'): if not (line.startswith('from __future__ import ') or line.startswith('from future ') or line.startswith('from builtins ') or 'install_hooks()' in line or 'install_aliases()' in line # but don't match "from future_builtins" :) or line.startswith('from future.')): output.append(line) return '\n'.join(output)
def monkey_patch_support_for_python2(): from future import standard_library standard_library.install_aliases() # Monkey patch the backported ipaddress module with alternative # versions that accept string addresses, in addition to unicode # addresses, in python2 code. # # This keep compatibility simple. Slightly complicated by the fact # that some of these classes inherit from each other. import ipaddress def python2_compat(cls, bases=()): def __init__(self, address, *args, **kwargs): if isinstance(address, str) and len(address) > 4: address = address.decode('utf-8') return cls.__init__(self, address, *args, **kwargs) return type(cls.__name__, (cls,) + bases, {'__init__': __init__}) ipaddress.IPv4Network = python2_compat(ipaddress.IPv4Network) ipaddress.IPv4Address = python2_compat(ipaddress.IPv4Address) ipaddress.IPv4Interface = python2_compat(ipaddress.IPv4Interface, bases=(ipaddress.IPv4Address,)) ipaddress.IPv6Network = python2_compat(ipaddress.IPv6Network) ipaddress.IPv6Address = python2_compat(ipaddress.IPv6Address) ipaddress.IPv6Interface = python2_compat(ipaddress.IPv6Interface, bases=(ipaddress.IPv6Address,))
def setUp(self): self.interpreter = sys.executable standard_library.install_aliases() super(TestStandardLibraryReorganization, self).setUp()
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)
def test_install_aliases(self): """ Does the install_aliases() interface monkey-patch urllib etc. successfully? """ from future.standard_library import remove_hooks, install_aliases remove_hooks() install_aliases() from collections import Counter, OrderedDict # backported to Py2.6 from collections import UserDict, UserList, UserString # Requires Python dbm support: # import dbm # import dbm.dumb # import dbm.gnu # import dbm.ndbm from itertools import filterfalse, zip_longest from subprocess import check_output # backported to Py2.6 from subprocess import getoutput, getstatusoutput from sys import intern # test_support may not be available (e.g. on Anaconda Py2.6): # import test.support import urllib.error import urllib.parse import urllib.request import urllib.response import urllib.robotparser self.assertTrue('urlopen' in dir(urllib.request))
def test_all_imports(self): before = """ import math import os l = range(10) assert isinstance(l, list) print 'Hello' for i in xrange(100): pass print('Hello') """ after = """ from __future__ import unicode_literals from __future__ import print_function from __future__ import division from __future__ import absolute_import from future import standard_library standard_library.install_aliases() from builtins import range from builtins import * import math import os l = list(range(10)) assert isinstance(l, list) print('Hello') for i in range(100): pass print('Hello') """ self.convert_check(before, after, all_imports=True)
def setUp(self): """ The outputs from the various futurize stages should have the following headers: """ # After stage1: # TODO: use this form after implementing a fixer to consolidate # __future__ imports into a single line: # self.headers1 = """ # from __future__ import absolute_import, division, print_function # """ self.headers1 = reformat_code(""" from __future__ import absolute_import from __future__ import division from __future__ import print_function """) # After stage2 --all-imports: # TODO: use this form after implementing a fixer to consolidate # __future__ imports into a single line: # self.headers2 = """ # from __future__ import (absolute_import, division, # print_function, unicode_literals) # from future import standard_library # from future.builtins import * # """ self.headers2 = reformat_code(""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from future import standard_library standard_library.install_aliases() from builtins import * """) self.interpreters = [sys.executable] self.tempdir = tempfile.mkdtemp() + os.path.sep pypath = os.getenv('PYTHONPATH') if pypath: self.env = {'PYTHONPATH': os.getcwd() + os.pathsep + pypath} else: self.env = {'PYTHONPATH': os.getcwd()}