本文整理汇总了Python中test.support.swap_item函数的典型用法代码示例。如果您正苦于以下问题:Python swap_item函数的具体用法?Python swap_item怎么用?Python swap_item使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了swap_item函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_issue31566
def test_issue31566(self):
# warn() shouldn't cause an assertion failure in case of a bad
# __name__ global.
with original_warnings.catch_warnings(module=self.module):
self.module.filterwarnings('error', category=UserWarning)
with support.swap_item(globals(), '__name__', b'foo'), \
support.swap_item(globals(), '__file__', None):
self.assertRaises(UserWarning, self.module.warn, 'bar')
开发者ID:Apoorvadabhere,项目名称:cpython,代码行数:8,代码来源:__init__.py
示例2: test_swap_item
def test_swap_item(self):
D = {"x":1}
with support.swap_item(D, "x", 5) as x:
self.assertEqual(D["x"], 5)
self.assertEqual(x, 1)
self.assertEqual(D["x"], 1)
with support.swap_item(D, "y", 5) as y:
self.assertEqual(D["y"], 5)
self.assertIsNone(y)
self.assertNotIn("y", D)
with support.swap_item(D, "y", 5):
del D["y"]
self.assertNotIn("y", D)
开发者ID:CCNITSilchar,项目名称:cpython,代码行数:13,代码来源:test_support.py
示例3: customize_compiler
def customize_compiler(self):
# make sure AR gets caught
class compiler:
compiler_type = 'unix'
def set_executables(self, **kw):
self.exes = kw
sysconfig_vars = {
'AR': 'sc_ar',
'CC': 'sc_cc',
'CXX': 'sc_cxx',
'ARFLAGS': '--sc-arflags',
'CFLAGS': '--sc-cflags',
'CCSHARED': '--sc-ccshared',
'LDSHARED': 'sc_ldshared',
'SHLIB_SUFFIX': 'sc_shutil_suffix',
}
comp = compiler()
with contextlib.ExitStack() as cm:
for key, value in sysconfig_vars.items():
cm.enter_context(swap_item(sysconfig._config_vars, key, value))
sysconfig.customize_compiler(comp)
return comp
开发者ID:willingc,项目名称:cpython,代码行数:26,代码来源:test_sysconfig.py
示例4: test_cannot_replace_builtins_dict_between_calls
def test_cannot_replace_builtins_dict_between_calls(self):
def foo():
return len([1, 2, 3])
self.configure_func(foo)
self.assertEqual(foo(), 3)
with swap_item(globals(), "__builtins__", {"len": lambda x: 7}):
self.assertEqual(foo(), 3)
开发者ID:Connor124,项目名称:Gran-Theft-Crop-Toe,代码行数:8,代码来源:test_dynamic.py
示例5: test_do_not_recreate_annotations
def test_do_not_recreate_annotations(self):
# Don't rely on the existence of the '__annotations__' global.
with support.swap_item(globals(), '__annotations__', {}):
del globals()['__annotations__']
class C:
del __annotations__
with self.assertRaises(NameError):
x: int
开发者ID:Eyepea,项目名称:cpython,代码行数:8,代码来源:test_opcodes.py
示例6: test_load_source
def test_load_source(self):
# Create a temporary module since load_source(name) modifies
# sys.modules[name] attributes like __loader___
modname = f"tmp{__name__}"
mod = type(sys.modules[__name__])(modname)
with support.swap_item(sys.modules, modname, mod):
with self.assertRaisesRegex(ValueError, 'embedded null'):
imp.load_source(modname, __file__ + "\0")
开发者ID:asvetlov,项目名称:cpython,代码行数:8,代码来源:test_imp.py
示例7: test_globals_shadow_builtins
def test_globals_shadow_builtins(self):
# Modify globals() to shadow an entry in builtins.
def foo():
return len([1, 2, 3])
self.configure_func(foo)
self.assertEqual(foo(), 3)
with swap_item(globals(), "len", lambda x: 7):
self.assertEqual(foo(), 7)
开发者ID:Connor124,项目名称:Gran-Theft-Crop-Toe,代码行数:9,代码来源:test_dynamic.py
示例8: test_cannot_replace_builtins_dict_while_active
def test_cannot_replace_builtins_dict_while_active(self):
def foo():
x = range(3)
yield len(x)
yield len(x)
self.configure_func(foo)
g = foo()
self.assertEqual(next(g), 3)
with swap_item(globals(), "__builtins__", {"len": lambda x: 7}):
self.assertEqual(next(g), 3)
开发者ID:Connor124,项目名称:Gran-Theft-Crop-Toe,代码行数:11,代码来源:test_dynamic.py
示例9: test_pickle_roundtrip
def test_pickle_roundtrip(self):
def check(actual, expected):
self.assertEqual(actual, expected)
self.assertEqual(actual.is_safe, expected.is_safe)
with support.swap_item(sys.modules, 'uuid', self.uuid):
for is_safe in self.uuid.SafeUUID:
u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
is_safe=is_safe)
check(copy.copy(u), u)
check(copy.deepcopy(u), u)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(protocol=proto):
check(pickle.loads(pickle.dumps(u, proto)), u)
开发者ID:FFMG,项目名称:myoddweb.piger,代码行数:14,代码来源:test_uuid.py
示例10: test_override_builtin
def test_override_builtin(self):
# Test that overriding builtins.__import__ can bypass sys.modules.
import os
def foo():
import os
return os
self.assertEqual(foo(), os) # Quick sanity check.
with swap_attr(builtins, "__import__", lambda *x: 5):
self.assertEqual(foo(), 5)
# Test what happens when we shadow __import__ in globals(); this
# currently does not impact the import process, but if this changes,
# other code will need to change, so keep this test as a tripwire.
with swap_item(globals(), "__import__", lambda *x: 5):
self.assertEqual(foo(), os)
开发者ID:Stewori,项目名称:JyNIgate,代码行数:17,代码来源:__init__.py
示例11: copy
def copy(self, obj, proto):
with support.swap_item(sys.modules, 'operator', self.module):
pickled = pickle.dumps(obj, proto)
with support.swap_item(sys.modules, 'operator', self.module2):
return pickle.loads(pickled)
开发者ID:MaximVanyushkin,项目名称:Sharp.RemoteQueryable,代码行数:5,代码来源:test_operator.py
示例12: test_swap_item
def test_swap_item(self):
D = {"item":1}
with support.swap_item(D, "item", 5):
self.assertEqual(D["item"], 5)
self.assertEqual(D["item"], 1)
开发者ID:MYSHLIFE,项目名称:cpython-1,代码行数:5,代码来源:test_support.py
示例13: test_unpickle_previous_python_versions
#.........这里部分代码省略.........
# Python 3.6, protocol 1
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
b'tR}X\x03\x00\x00\x00intL287307832597519156748809049798316161701L'
b'\nsb.',
# Python 3.6, protocol 2
b'\x80\x02cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec'
b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.',
# Python 3.6, protocol 3
b'\x80\x03cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec'
b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.',
# Python 3.6, protocol 4
b'\x80\x04\x95+\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x8c\x04UUI'
b'D\x93)\x81}\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0Bf\xcey%'
b'\xd8\x00sb.',
# Python 3.7, protocol 0
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
b'cuuid\nSafeUUID\n(NtRsb.',
# Python 3.7, protocol 1
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(NtRub.',
# Python 3.7, protocol 2
b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
b'id\nSafeUUID\nN\x85Rub.',
# Python 3.7, protocol 3
b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
b'id\nSafeUUID\nN\x85Rub.',
# Python 3.7, protocol 4
b'\x80\x04\x95F\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93N\x85Rub'
b'.',
]
pickled_uuids_safe = [
# Python 3.7, protocol 0
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
b'cuuid\nSafeUUID\n(I0\ntRsb.',
# Python 3.7, protocol 1
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(K\x00tRub.',
# Python 3.7, protocol 2
b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
b'id\nSafeUUID\nK\x00\x85Rub.',
# Python 3.7, protocol 3
b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
b'id\nSafeUUID\nK\x00\x85Rub.',
# Python 3.7, protocol 4
b'\x80\x04\x95G\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93K\x00'
b'\x85Rub.',
]
pickled_uuids_unsafe = [
# Python 3.7, protocol 0
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
b'cuuid\nSafeUUID\n(I-1\ntRsb.',
# Python 3.7, protocol 1
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(J\xff\xff\xff\xfftR'
b'ub.',
# Python 3.7, protocol 2
b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.',
# Python 3.7, protocol 3
b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.',
# Python 3.7, protocol 4
b'\x80\x04\x95J\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93J\xff'
b'\xff\xff\xff\x85Rub.',
]
u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5')
u_safe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
is_safe=self.uuid.SafeUUID.safe)
u_unsafe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
is_safe=self.uuid.SafeUUID.unsafe)
with support.swap_item(sys.modules, 'uuid', self.uuid):
for pickled in pickled_uuids:
# is_safe was added in 3.7. When unpickling values from older
# versions, is_safe will be missing, so it should be set to
# SafeUUID.unknown.
check(pickle.loads(pickled), u)
for pickled in pickled_uuids_safe:
check(pickle.loads(pickled), u_safe)
for pickled in pickled_uuids_unsafe:
check(pickle.loads(pickled), u_unsafe)
开发者ID:FFMG,项目名称:myoddweb.piger,代码行数:101,代码来源:test_uuid.py
注:本文中的test.support.swap_item函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论