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')
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)
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)
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)
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
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', # On macOS, disable _osx_support.customize_compiler() 'CUSTOMIZED_OSX_COMPILER': 'True', } 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
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")
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
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)
def test_globals_shadow_builtins(self): 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)
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)
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)
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)
def test_override_builtin(self): import os def foo(): import os return os self.assertEqual(foo(), os) with swap_attr(builtins, '__import__', lambda *x: 5): self.assertEqual(foo(), 5) with swap_item(globals(), '__import__', lambda *x: 5): self.assertEqual(foo(), os)
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)
def test_do_not_recreate_annotations(self): annotations = {} # Don't rely on the existence of the '__annotations__' global. with support.swap_item(globals(), '__annotations__', annotations): class C: del __annotations__ x: int # Updates the '__annotations__' global. self.assertIn('x', annotations) self.assertIs(annotations['x'], int)
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)
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)
def test_customize_compiler(self): os.environ['AR'] = 'my_ar' os.environ['CC'] = 'my_cc' os.environ['ARFLAGS'] = '--myarflags' os.environ['CFLAGS'] = '--mycflags' # make sure AR gets caught class compiler: compiler_type = 'unix' def set_executables(self, **kw): self.exes = kw # Make sure that sysconfig._config_vars is initialized sysconfig.get_config_vars() comp = compiler() with swap_item(sysconfig._config_vars, 'CFLAGS', '--sysconfig-cflags'): sysconfig.customize_compiler(comp) self.assertEqual(comp.exes['archiver'], 'my_ar --myarflags') self.assertEqual(comp.exes['compiler'], 'my_cc --sysconfig-cflags --mycflags')
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)
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)
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)
def test_unpickle_previous_python_versions(self): def check(actual, expected): self.assertEqual(actual, expected) self.assertEqual(actual.is_safe, expected.is_safe) pickled_uuids = [ # Python 2.7, protocol 0 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' b'tR(dS\'int\'\nL287307832597519156748809049798316161701L\nsb.', # Python 2.7, protocol 1 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' b'tR}U\x03intL287307832597519156748809049798316161701L\nsb.', # Python 2.7, protocol 2 b'\x80\x02cuuid\nUUID\n)\x81}U\x03int\x8a\x11\xa5z\xecz\nI\xdf}' b'\xde\xa0Bf\xcey%\xd8\x00sb.', # Python 3.6, protocol 0 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' b'tR(dVint\nL287307832597519156748809049798316161701L\nsb.', # 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)