bpo-30197: Enhance functions swap_attr() and swap_item() in test.supp… · python/cpython@d1a1def

@@ -295,17 +295,34 @@ def test_python_is_optimized(self):

295295296296

def test_swap_attr(self):

297297

class Obj:

298-

x = 1

298+

pass

299299

obj = Obj()

300-

with support.swap_attr(obj, "x", 5):

300+

obj.x = 1

301+

with support.swap_attr(obj, "x", 5) as x:

301302

self.assertEqual(obj.x, 5)

303+

self.assertEqual(x, 1)

302304

self.assertEqual(obj.x, 1)

305+

with support.swap_attr(obj, "y", 5) as y:

306+

self.assertEqual(obj.y, 5)

307+

self.assertIsNone(y)

308+

self.assertFalse(hasattr(obj, 'y'))

309+

with support.swap_attr(obj, "y", 5):

310+

del obj.y

311+

self.assertFalse(hasattr(obj, 'y'))

303312304313

def test_swap_item(self):

305-

D = {"item":1}

306-

with support.swap_item(D, "item", 5):

307-

self.assertEqual(D["item"], 5)

308-

self.assertEqual(D["item"], 1)

314+

D = {"x":1}

315+

with support.swap_item(D, "x", 5) as x:

316+

self.assertEqual(D["x"], 5)

317+

self.assertEqual(x, 1)

318+

self.assertEqual(D["x"], 1)

319+

with support.swap_item(D, "y", 5) as y:

320+

self.assertEqual(D["y"], 5)

321+

self.assertIsNone(y)

322+

self.assertNotIn("y", D)

323+

with support.swap_item(D, "y", 5):

324+

del D["y"]

325+

self.assertNotIn("y", D)

309326310327

class RefClass:

311328

attribute1 = None