MCPcopy Index your code
hub / github.com/python/cpython / test_properties

Method test_properties

Lib/test/test_descr.py:2423–2484  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2421 del r
2422
2423 def test_properties(self):
2424 # Testing property...
2425 class C(object):
2426 def getx(self):
2427 return self.__x
2428 def setx(self, value):
2429 self.__x = value
2430 def delx(self):
2431 del self.__x
2432 x = property(getx, setx, delx, doc="I'm the x property.")
2433 a = C()
2434 self.assertNotHasAttr(a, "x")
2435 a.x = 42
2436 self.assertEqual(a._C__x, 42)
2437 self.assertEqual(a.x, 42)
2438 del a.x
2439 self.assertNotHasAttr(a, "x")
2440 self.assertNotHasAttr(a, "_C__x")
2441 C.x.__set__(a, 100)
2442 self.assertEqual(C.x.__get__(a), 100)
2443 C.x.__delete__(a)
2444 self.assertNotHasAttr(a, "x")
2445
2446 raw = C.__dict__['x']
2447 self.assertIsInstance(raw, property)
2448
2449 attrs = dir(raw)
2450 self.assertIn("__doc__", attrs)
2451 self.assertIn("fget", attrs)
2452 self.assertIn("fset", attrs)
2453 self.assertIn("fdel", attrs)
2454
2455 self.assertEqual(raw.__doc__, "I'm the x property.")
2456 self.assertIs(raw.fget, C.__dict__['getx'])
2457 self.assertIs(raw.fset, C.__dict__['setx'])
2458 self.assertIs(raw.fdel, C.__dict__['delx'])
2459
2460 for attr in "fget", "fset", "fdel":
2461 try:
2462 setattr(raw, attr, 42)
2463 except AttributeError as msg:
2464 if str(msg).find('readonly') < 0:
2465 self.fail("when setting readonly attr %r on a property, "
2466 "got unexpected AttributeError msg %r" % (attr, str(msg)))
2467 else:
2468 self.fail("expected AttributeError from trying to set readonly %r "
2469 "attr on a property" % attr)
2470
2471 raw.__doc__ = 42
2472 self.assertEqual(raw.__doc__, 42)
2473
2474 class D(object):
2475 __getitem__ = property(lambda s: 1/0)
2476
2477 d = D()
2478 try:
2479 for i in d:
2480 str(i)

Callers

nothing calls this directly

Calls 13

strFunction · 0.85
assertNotHasAttrMethod · 0.80
assertIsInstanceMethod · 0.80
assertInMethod · 0.80
CClass · 0.70
DClass · 0.70
assertEqualMethod · 0.45
__set__Method · 0.45
__get__Method · 0.45
__delete__Method · 0.45
assertIsMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected