本文整理匯總了Python中future.utils.PY3屬性的典型用法代碼示例。如果您正苦於以下問題:Python utils.PY3屬性的具體用法?Python utils.PY3怎麽用?Python utils.PY3使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類future.utils
的用法示例。
在下文中一共展示了utils.PY3屬性的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: scrub_py2_sys_modules
# 需要導入模塊: from future import utils [as 別名]
# 或者: from future.utils import PY3 [as 別名]
def scrub_py2_sys_modules():
"""
Removes any Python 2 standard library modules from ``sys.modules`` that
would interfere with Py3-style imports using import hooks. Examples are
modules with the same names (like urllib or email).
(Note that currently import hooks are disabled for modules like these
with ambiguous names anyway ...)
"""
if PY3:
return {}
scrubbed = {}
for modulename in REPLACED_MODULES & set(RENAMES.keys()):
if not modulename in sys.modules:
continue
module = sys.modules[modulename]
if is_py2_stdlib_module(module):
flog.debug('Deleting (Py2) {} from sys.modules'.format(modulename))
scrubbed[modulename] = sys.modules[modulename]
del sys.modules[modulename]
return scrubbed
示例2: install_hooks
# 需要導入模塊: from future import utils [as 別名]
# 或者: from future.utils import PY3 [as 別名]
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))
示例3: remove_hooks
# 需要導入模塊: from future import utils [as 別名]
# 或者: from future.utils import PY3 [as 別名]
def remove_hooks(scrub_sys_modules=False):
"""
This function removes the import hook from sys.meta_path.
"""
if PY3:
return
flog.debug('Uninstalling hooks ...')
# Loop backwards, so deleting items keeps the ordering:
for i, hook in list(enumerate(sys.meta_path))[::-1]:
if hasattr(hook, 'RENAMER'):
del sys.meta_path[i]
# Explicit is better than implicit. In the future the interface should
# probably change so that scrubbing the import hooks requires a separate
# function call. Left as is for now for backward compatibility with
# v0.11.x.
if scrub_sys_modules:
scrub_future_sys_modules()
示例4: detect_hooks
# 需要導入模塊: from future import utils [as 別名]
# 或者: from future.utils import PY3 [as 別名]
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()
示例5: test_setdefault_atomic
# 需要導入模塊: from future import utils [as 別名]
# 或者: from future.utils import PY3 [as 別名]
def test_setdefault_atomic(self):
# Issue #13521: setdefault() calls __hash__ and __eq__ only once.
class Hashed(object):
def __init__(self):
self.hash_count = 0
self.eq_count = 0
def __hash__(self):
self.hash_count += 1
return 42
def __eq__(self, other):
self.eq_count += 1
return id(self) == id(other)
hashed1 = Hashed()
y = dict({hashed1: 5})
hashed2 = Hashed()
y.setdefault(hashed2, [])
self.assertEqual(hashed1.hash_count, 1)
if PY3:
self.assertEqual(hashed2.hash_count, 1)
self.assertEqual(hashed1.eq_count + hashed2.eq_count, 1)
示例6: sendall
# 需要導入模塊: from future import utils [as 別名]
# 或者: from future.utils import PY3 [as 別名]
def sendall(self, data):
# self.data += bytes(data)
olddata = self.data
assert isinstance(olddata, bytes)
if utils.PY3:
self.data += data
else:
if isinstance(data, type(u'')): # i.e. unicode
newdata = data.encode('ascii')
elif isinstance(data, type(b'')): # native string type. FIXME!
newdata = bytes(data)
elif isinstance(data, bytes):
newdata = data
elif isinstance(data, array.array):
newdata = data.tostring()
else:
newdata = bytes(b'').join(chr(d) for d in bytes(data))
self.data += newdata
示例7: test_send
# 需要導入模塊: from future import utils [as 別名]
# 或者: from future.utils import PY3 [as 別名]
def test_send(self):
expected = bytes(b'this is a test this is only a test')
conn = client.HTTPConnection('example.com')
sock = FakeSocket(None)
conn.sock = sock
conn.send(expected)
self.assertEqual(expected, sock.data)
sock.data = bytes(b'')
if utils.PY3:
mydata = array.array('b', expected)
else:
mydata = array.array(b'b', expected)
conn.send(mydata)
self.assertEqual(expected, sock.data)
sock.data = bytes(b'')
conn.send(io.BytesIO(expected))
self.assertEqual(expected, sock.data)
示例8: is_py2_stdlib_module
# 需要導入模塊: from future import utils [as 別名]
# 或者: from future.utils import PY3 [as 別名]
def is_py2_stdlib_module(m):
"""
Tries to infer whether the module m is from the Python 2 standard library.
This may not be reliable on all systems.
"""
if PY3:
return False
if not 'stdlib_path' in is_py2_stdlib_module.__dict__:
stdlib_files = [contextlib.__file__, os.__file__, copy.__file__]
stdlib_paths = [os.path.split(f)[0] for f in stdlib_files]
if not len(set(stdlib_paths)) == 1:
# This seems to happen on travis-ci.org. Very strange. We'll try to
# ignore it.
flog.warn('Multiple locations found for the Python standard '
'library: %s' % stdlib_paths)
# Choose the first one arbitrarily
is_py2_stdlib_module.stdlib_path = stdlib_paths[0]
if m.__name__ in sys.builtin_module_names:
return True
if hasattr(m, '__file__'):
modpath = os.path.split(m.__file__)
if (modpath[0].startswith(is_py2_stdlib_module.stdlib_path) and
'site-packages' not in modpath[0]):
return True
return False
示例9: u
# 需要導入模塊: from future import utils [as 別名]
# 或者: from future.utils import PY3 [as 別名]
def u(text):
if utils.PY3:
return text
else:
return text.decode('unicode_escape')
示例10: b
# 需要導入模塊: from future import utils [as 別名]
# 或者: from future.utils import PY3 [as 別名]
def b(data):
if utils.PY3:
return data.encode('latin1')
else:
return data