Merged revisions 59488-59511 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59489 | christian.heimes | 2007-12-14 03:33:57 +0100 (Fri, 14 Dec 2007) | 1 line
Silence a warning about an unsed variable in debug builds
........
r59490 | christian.heimes | 2007-12-14 03:35:23 +0100 (Fri, 14 Dec 2007) | 2 lines
Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
........
r59491 | raymond.hettinger | 2007-12-14 03:49:47 +0100 (Fri, 14 Dec 2007) | 1 line
Cleaner method naming convention
........
r59492 | christian.heimes | 2007-12-14 04:02:34 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed a warning in _codecs_iso2022.c and some non C89 conform // comments.
........
r59493 | christian.heimes | 2007-12-14 05:38:13 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed warning in ssl module
........
r59500 | raymond.hettinger | 2007-12-14 19:08:20 +0100 (Fri, 14 Dec 2007) | 1 line
Add line spacing for readability
........
r59501 | raymond.hettinger | 2007-12-14 19:12:21 +0100 (Fri, 14 Dec 2007) | 3 lines
Update method names for named tuples.
........
r59503 | georg.brandl | 2007-12-14 20:03:36 +0100 (Fri, 14 Dec 2007) | 3 lines
Add a section about nested listcomps to the tutorial.
Thanks to Ian Bruntlett and Robert Lehmann.
........
r59504 | raymond.hettinger | 2007-12-14 20:19:59 +0100 (Fri, 14 Dec 2007) | 1 line
Faster and simpler _replace() method
........
r59505 | raymond.hettinger | 2007-12-14 22:51:50 +0100 (Fri, 14 Dec 2007) | 1 line
Add usage note
........
r59507 | andrew.kuchling | 2007-12-14 23:41:18 +0100 (Fri, 14 Dec 2007) | 1 line
Remove warning about URL
........
r59510 | andrew.kuchling | 2007-12-14 23:52:36 +0100 (Fri, 14 Dec 2007) | 1 line
Bump the version number, and make a few small edits
........
r59511 | christian.heimes | 2007-12-15 00:42:36 +0100 (Sat, 15 Dec 2007) | 2 lines
Fixed bug #1628
The detection now works on Unix with Makefile, Makefile with VPATH and on Windows.
........
2007-12-14 21:27:15 -04:00
|
|
|
# Test case for property
|
|
|
|
# more tests are in test_descr
|
|
|
|
|
2010-02-23 21:46:21 -04:00
|
|
|
import sys
|
Merged revisions 59488-59511 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59489 | christian.heimes | 2007-12-14 03:33:57 +0100 (Fri, 14 Dec 2007) | 1 line
Silence a warning about an unsed variable in debug builds
........
r59490 | christian.heimes | 2007-12-14 03:35:23 +0100 (Fri, 14 Dec 2007) | 2 lines
Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
........
r59491 | raymond.hettinger | 2007-12-14 03:49:47 +0100 (Fri, 14 Dec 2007) | 1 line
Cleaner method naming convention
........
r59492 | christian.heimes | 2007-12-14 04:02:34 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed a warning in _codecs_iso2022.c and some non C89 conform // comments.
........
r59493 | christian.heimes | 2007-12-14 05:38:13 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed warning in ssl module
........
r59500 | raymond.hettinger | 2007-12-14 19:08:20 +0100 (Fri, 14 Dec 2007) | 1 line
Add line spacing for readability
........
r59501 | raymond.hettinger | 2007-12-14 19:12:21 +0100 (Fri, 14 Dec 2007) | 3 lines
Update method names for named tuples.
........
r59503 | georg.brandl | 2007-12-14 20:03:36 +0100 (Fri, 14 Dec 2007) | 3 lines
Add a section about nested listcomps to the tutorial.
Thanks to Ian Bruntlett and Robert Lehmann.
........
r59504 | raymond.hettinger | 2007-12-14 20:19:59 +0100 (Fri, 14 Dec 2007) | 1 line
Faster and simpler _replace() method
........
r59505 | raymond.hettinger | 2007-12-14 22:51:50 +0100 (Fri, 14 Dec 2007) | 1 line
Add usage note
........
r59507 | andrew.kuchling | 2007-12-14 23:41:18 +0100 (Fri, 14 Dec 2007) | 1 line
Remove warning about URL
........
r59510 | andrew.kuchling | 2007-12-14 23:52:36 +0100 (Fri, 14 Dec 2007) | 1 line
Bump the version number, and make a few small edits
........
r59511 | christian.heimes | 2007-12-15 00:42:36 +0100 (Sat, 15 Dec 2007) | 2 lines
Fixed bug #1628
The detection now works on Unix with Makefile, Makefile with VPATH and on Windows.
........
2007-12-14 21:27:15 -04:00
|
|
|
import unittest
|
2018-02-13 07:32:18 -04:00
|
|
|
from test import support
|
Merged revisions 59488-59511 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59489 | christian.heimes | 2007-12-14 03:33:57 +0100 (Fri, 14 Dec 2007) | 1 line
Silence a warning about an unsed variable in debug builds
........
r59490 | christian.heimes | 2007-12-14 03:35:23 +0100 (Fri, 14 Dec 2007) | 2 lines
Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
........
r59491 | raymond.hettinger | 2007-12-14 03:49:47 +0100 (Fri, 14 Dec 2007) | 1 line
Cleaner method naming convention
........
r59492 | christian.heimes | 2007-12-14 04:02:34 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed a warning in _codecs_iso2022.c and some non C89 conform // comments.
........
r59493 | christian.heimes | 2007-12-14 05:38:13 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed warning in ssl module
........
r59500 | raymond.hettinger | 2007-12-14 19:08:20 +0100 (Fri, 14 Dec 2007) | 1 line
Add line spacing for readability
........
r59501 | raymond.hettinger | 2007-12-14 19:12:21 +0100 (Fri, 14 Dec 2007) | 3 lines
Update method names for named tuples.
........
r59503 | georg.brandl | 2007-12-14 20:03:36 +0100 (Fri, 14 Dec 2007) | 3 lines
Add a section about nested listcomps to the tutorial.
Thanks to Ian Bruntlett and Robert Lehmann.
........
r59504 | raymond.hettinger | 2007-12-14 20:19:59 +0100 (Fri, 14 Dec 2007) | 1 line
Faster and simpler _replace() method
........
r59505 | raymond.hettinger | 2007-12-14 22:51:50 +0100 (Fri, 14 Dec 2007) | 1 line
Add usage note
........
r59507 | andrew.kuchling | 2007-12-14 23:41:18 +0100 (Fri, 14 Dec 2007) | 1 line
Remove warning about URL
........
r59510 | andrew.kuchling | 2007-12-14 23:52:36 +0100 (Fri, 14 Dec 2007) | 1 line
Bump the version number, and make a few small edits
........
r59511 | christian.heimes | 2007-12-15 00:42:36 +0100 (Sat, 15 Dec 2007) | 2 lines
Fixed bug #1628
The detection now works on Unix with Makefile, Makefile with VPATH and on Windows.
........
2007-12-14 21:27:15 -04:00
|
|
|
|
|
|
|
class PropertyBase(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PropertyGet(PropertyBase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PropertySet(PropertyBase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PropertyDel(PropertyBase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BaseClass(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._spam = 5
|
|
|
|
|
|
|
|
@property
|
|
|
|
def spam(self):
|
|
|
|
"""BaseClass.getter"""
|
|
|
|
return self._spam
|
|
|
|
|
|
|
|
@spam.setter
|
|
|
|
def spam(self, value):
|
|
|
|
self._spam = value
|
|
|
|
|
|
|
|
@spam.deleter
|
|
|
|
def spam(self):
|
|
|
|
del self._spam
|
|
|
|
|
|
|
|
class SubClass(BaseClass):
|
|
|
|
|
|
|
|
@BaseClass.spam.getter
|
|
|
|
def spam(self):
|
|
|
|
"""SubClass.getter"""
|
|
|
|
raise PropertyGet(self._spam)
|
|
|
|
|
|
|
|
@spam.setter
|
|
|
|
def spam(self, value):
|
|
|
|
raise PropertySet(self._spam)
|
|
|
|
|
|
|
|
@spam.deleter
|
|
|
|
def spam(self):
|
|
|
|
raise PropertyDel(self._spam)
|
|
|
|
|
|
|
|
class PropertyDocBase(object):
|
|
|
|
_spam = 1
|
|
|
|
def _get_spam(self):
|
|
|
|
return self._spam
|
|
|
|
spam = property(_get_spam, doc="spam spam spam")
|
|
|
|
|
|
|
|
class PropertyDocSub(PropertyDocBase):
|
|
|
|
@PropertyDocBase.spam.getter
|
|
|
|
def spam(self):
|
|
|
|
"""The decorator does not use this doc string"""
|
|
|
|
return self._spam
|
|
|
|
|
2009-05-04 19:59:07 -03:00
|
|
|
class PropertySubNewGetter(BaseClass):
|
|
|
|
@BaseClass.spam.getter
|
|
|
|
def spam(self):
|
|
|
|
"""new docstring"""
|
|
|
|
return 5
|
|
|
|
|
|
|
|
class PropertyNewGetter(object):
|
|
|
|
@property
|
|
|
|
def spam(self):
|
|
|
|
"""original docstring"""
|
|
|
|
return 1
|
|
|
|
@spam.getter
|
|
|
|
def spam(self):
|
|
|
|
"""new docstring"""
|
|
|
|
return 8
|
|
|
|
|
Merged revisions 59488-59511 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59489 | christian.heimes | 2007-12-14 03:33:57 +0100 (Fri, 14 Dec 2007) | 1 line
Silence a warning about an unsed variable in debug builds
........
r59490 | christian.heimes | 2007-12-14 03:35:23 +0100 (Fri, 14 Dec 2007) | 2 lines
Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
........
r59491 | raymond.hettinger | 2007-12-14 03:49:47 +0100 (Fri, 14 Dec 2007) | 1 line
Cleaner method naming convention
........
r59492 | christian.heimes | 2007-12-14 04:02:34 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed a warning in _codecs_iso2022.c and some non C89 conform // comments.
........
r59493 | christian.heimes | 2007-12-14 05:38:13 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed warning in ssl module
........
r59500 | raymond.hettinger | 2007-12-14 19:08:20 +0100 (Fri, 14 Dec 2007) | 1 line
Add line spacing for readability
........
r59501 | raymond.hettinger | 2007-12-14 19:12:21 +0100 (Fri, 14 Dec 2007) | 3 lines
Update method names for named tuples.
........
r59503 | georg.brandl | 2007-12-14 20:03:36 +0100 (Fri, 14 Dec 2007) | 3 lines
Add a section about nested listcomps to the tutorial.
Thanks to Ian Bruntlett and Robert Lehmann.
........
r59504 | raymond.hettinger | 2007-12-14 20:19:59 +0100 (Fri, 14 Dec 2007) | 1 line
Faster and simpler _replace() method
........
r59505 | raymond.hettinger | 2007-12-14 22:51:50 +0100 (Fri, 14 Dec 2007) | 1 line
Add usage note
........
r59507 | andrew.kuchling | 2007-12-14 23:41:18 +0100 (Fri, 14 Dec 2007) | 1 line
Remove warning about URL
........
r59510 | andrew.kuchling | 2007-12-14 23:52:36 +0100 (Fri, 14 Dec 2007) | 1 line
Bump the version number, and make a few small edits
........
r59511 | christian.heimes | 2007-12-15 00:42:36 +0100 (Sat, 15 Dec 2007) | 2 lines
Fixed bug #1628
The detection now works on Unix with Makefile, Makefile with VPATH and on Windows.
........
2007-12-14 21:27:15 -04:00
|
|
|
class PropertyTests(unittest.TestCase):
|
|
|
|
def test_property_decorator_baseclass(self):
|
|
|
|
# see #1620
|
|
|
|
base = BaseClass()
|
|
|
|
self.assertEqual(base.spam, 5)
|
|
|
|
self.assertEqual(base._spam, 5)
|
|
|
|
base.spam = 10
|
|
|
|
self.assertEqual(base.spam, 10)
|
|
|
|
self.assertEqual(base._spam, 10)
|
|
|
|
delattr(base, "spam")
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(not hasattr(base, "spam"))
|
|
|
|
self.assertTrue(not hasattr(base, "_spam"))
|
Merged revisions 59488-59511 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59489 | christian.heimes | 2007-12-14 03:33:57 +0100 (Fri, 14 Dec 2007) | 1 line
Silence a warning about an unsed variable in debug builds
........
r59490 | christian.heimes | 2007-12-14 03:35:23 +0100 (Fri, 14 Dec 2007) | 2 lines
Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
........
r59491 | raymond.hettinger | 2007-12-14 03:49:47 +0100 (Fri, 14 Dec 2007) | 1 line
Cleaner method naming convention
........
r59492 | christian.heimes | 2007-12-14 04:02:34 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed a warning in _codecs_iso2022.c and some non C89 conform // comments.
........
r59493 | christian.heimes | 2007-12-14 05:38:13 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed warning in ssl module
........
r59500 | raymond.hettinger | 2007-12-14 19:08:20 +0100 (Fri, 14 Dec 2007) | 1 line
Add line spacing for readability
........
r59501 | raymond.hettinger | 2007-12-14 19:12:21 +0100 (Fri, 14 Dec 2007) | 3 lines
Update method names for named tuples.
........
r59503 | georg.brandl | 2007-12-14 20:03:36 +0100 (Fri, 14 Dec 2007) | 3 lines
Add a section about nested listcomps to the tutorial.
Thanks to Ian Bruntlett and Robert Lehmann.
........
r59504 | raymond.hettinger | 2007-12-14 20:19:59 +0100 (Fri, 14 Dec 2007) | 1 line
Faster and simpler _replace() method
........
r59505 | raymond.hettinger | 2007-12-14 22:51:50 +0100 (Fri, 14 Dec 2007) | 1 line
Add usage note
........
r59507 | andrew.kuchling | 2007-12-14 23:41:18 +0100 (Fri, 14 Dec 2007) | 1 line
Remove warning about URL
........
r59510 | andrew.kuchling | 2007-12-14 23:52:36 +0100 (Fri, 14 Dec 2007) | 1 line
Bump the version number, and make a few small edits
........
r59511 | christian.heimes | 2007-12-15 00:42:36 +0100 (Sat, 15 Dec 2007) | 2 lines
Fixed bug #1628
The detection now works on Unix with Makefile, Makefile with VPATH and on Windows.
........
2007-12-14 21:27:15 -04:00
|
|
|
base.spam = 20
|
|
|
|
self.assertEqual(base.spam, 20)
|
|
|
|
self.assertEqual(base._spam, 20)
|
|
|
|
|
|
|
|
def test_property_decorator_subclass(self):
|
|
|
|
# see #1620
|
|
|
|
sub = SubClass()
|
|
|
|
self.assertRaises(PropertyGet, getattr, sub, "spam")
|
|
|
|
self.assertRaises(PropertySet, setattr, sub, "spam", None)
|
|
|
|
self.assertRaises(PropertyDel, delattr, sub, "spam")
|
2010-02-23 21:46:21 -04:00
|
|
|
|
|
|
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
|
|
"Docstrings are omitted with -O2 and above")
|
|
|
|
def test_property_decorator_subclass_doc(self):
|
|
|
|
sub = SubClass()
|
Merged revisions 59488-59511 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59489 | christian.heimes | 2007-12-14 03:33:57 +0100 (Fri, 14 Dec 2007) | 1 line
Silence a warning about an unsed variable in debug builds
........
r59490 | christian.heimes | 2007-12-14 03:35:23 +0100 (Fri, 14 Dec 2007) | 2 lines
Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
........
r59491 | raymond.hettinger | 2007-12-14 03:49:47 +0100 (Fri, 14 Dec 2007) | 1 line
Cleaner method naming convention
........
r59492 | christian.heimes | 2007-12-14 04:02:34 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed a warning in _codecs_iso2022.c and some non C89 conform // comments.
........
r59493 | christian.heimes | 2007-12-14 05:38:13 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed warning in ssl module
........
r59500 | raymond.hettinger | 2007-12-14 19:08:20 +0100 (Fri, 14 Dec 2007) | 1 line
Add line spacing for readability
........
r59501 | raymond.hettinger | 2007-12-14 19:12:21 +0100 (Fri, 14 Dec 2007) | 3 lines
Update method names for named tuples.
........
r59503 | georg.brandl | 2007-12-14 20:03:36 +0100 (Fri, 14 Dec 2007) | 3 lines
Add a section about nested listcomps to the tutorial.
Thanks to Ian Bruntlett and Robert Lehmann.
........
r59504 | raymond.hettinger | 2007-12-14 20:19:59 +0100 (Fri, 14 Dec 2007) | 1 line
Faster and simpler _replace() method
........
r59505 | raymond.hettinger | 2007-12-14 22:51:50 +0100 (Fri, 14 Dec 2007) | 1 line
Add usage note
........
r59507 | andrew.kuchling | 2007-12-14 23:41:18 +0100 (Fri, 14 Dec 2007) | 1 line
Remove warning about URL
........
r59510 | andrew.kuchling | 2007-12-14 23:52:36 +0100 (Fri, 14 Dec 2007) | 1 line
Bump the version number, and make a few small edits
........
r59511 | christian.heimes | 2007-12-15 00:42:36 +0100 (Sat, 15 Dec 2007) | 2 lines
Fixed bug #1628
The detection now works on Unix with Makefile, Makefile with VPATH and on Windows.
........
2007-12-14 21:27:15 -04:00
|
|
|
self.assertEqual(sub.__class__.spam.__doc__, "SubClass.getter")
|
|
|
|
|
2010-02-23 21:46:21 -04:00
|
|
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
|
|
"Docstrings are omitted with -O2 and above")
|
|
|
|
def test_property_decorator_baseclass_doc(self):
|
|
|
|
base = BaseClass()
|
|
|
|
self.assertEqual(base.__class__.spam.__doc__, "BaseClass.getter")
|
|
|
|
|
Merged revisions 59488-59511 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59489 | christian.heimes | 2007-12-14 03:33:57 +0100 (Fri, 14 Dec 2007) | 1 line
Silence a warning about an unsed variable in debug builds
........
r59490 | christian.heimes | 2007-12-14 03:35:23 +0100 (Fri, 14 Dec 2007) | 2 lines
Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
........
r59491 | raymond.hettinger | 2007-12-14 03:49:47 +0100 (Fri, 14 Dec 2007) | 1 line
Cleaner method naming convention
........
r59492 | christian.heimes | 2007-12-14 04:02:34 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed a warning in _codecs_iso2022.c and some non C89 conform // comments.
........
r59493 | christian.heimes | 2007-12-14 05:38:13 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed warning in ssl module
........
r59500 | raymond.hettinger | 2007-12-14 19:08:20 +0100 (Fri, 14 Dec 2007) | 1 line
Add line spacing for readability
........
r59501 | raymond.hettinger | 2007-12-14 19:12:21 +0100 (Fri, 14 Dec 2007) | 3 lines
Update method names for named tuples.
........
r59503 | georg.brandl | 2007-12-14 20:03:36 +0100 (Fri, 14 Dec 2007) | 3 lines
Add a section about nested listcomps to the tutorial.
Thanks to Ian Bruntlett and Robert Lehmann.
........
r59504 | raymond.hettinger | 2007-12-14 20:19:59 +0100 (Fri, 14 Dec 2007) | 1 line
Faster and simpler _replace() method
........
r59505 | raymond.hettinger | 2007-12-14 22:51:50 +0100 (Fri, 14 Dec 2007) | 1 line
Add usage note
........
r59507 | andrew.kuchling | 2007-12-14 23:41:18 +0100 (Fri, 14 Dec 2007) | 1 line
Remove warning about URL
........
r59510 | andrew.kuchling | 2007-12-14 23:52:36 +0100 (Fri, 14 Dec 2007) | 1 line
Bump the version number, and make a few small edits
........
r59511 | christian.heimes | 2007-12-15 00:42:36 +0100 (Sat, 15 Dec 2007) | 2 lines
Fixed bug #1628
The detection now works on Unix with Makefile, Makefile with VPATH and on Windows.
........
2007-12-14 21:27:15 -04:00
|
|
|
def test_property_decorator_doc(self):
|
|
|
|
base = PropertyDocBase()
|
|
|
|
sub = PropertyDocSub()
|
|
|
|
self.assertEqual(base.__class__.spam.__doc__, "spam spam spam")
|
|
|
|
self.assertEqual(sub.__class__.spam.__doc__, "spam spam spam")
|
|
|
|
|
2010-06-28 12:43:25 -03:00
|
|
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
2010-02-23 21:46:21 -04:00
|
|
|
"Docstrings are omitted with -O2 and above")
|
2009-05-04 19:59:07 -03:00
|
|
|
def test_property_getter_doc_override(self):
|
|
|
|
newgettersub = PropertySubNewGetter()
|
|
|
|
self.assertEqual(newgettersub.spam, 5)
|
|
|
|
self.assertEqual(newgettersub.__class__.spam.__doc__, "new docstring")
|
|
|
|
newgetter = PropertyNewGetter()
|
|
|
|
self.assertEqual(newgetter.spam, 8)
|
|
|
|
self.assertEqual(newgetter.__class__.spam.__doc__, "new docstring")
|
|
|
|
|
2011-12-15 16:34:02 -04:00
|
|
|
def test_property___isabstractmethod__descriptor(self):
|
|
|
|
for val in (True, False, [], [1], '', '1'):
|
|
|
|
class C(object):
|
|
|
|
def foo(self):
|
|
|
|
pass
|
|
|
|
foo.__isabstractmethod__ = val
|
|
|
|
foo = property(foo)
|
|
|
|
self.assertIs(C.foo.__isabstractmethod__, bool(val))
|
|
|
|
|
|
|
|
# check that the property's __isabstractmethod__ descriptor does the
|
|
|
|
# right thing when presented with a value that fails truth testing:
|
|
|
|
class NotBool(object):
|
2015-03-12 16:56:08 -03:00
|
|
|
def __bool__(self):
|
2011-12-15 16:34:02 -04:00
|
|
|
raise ValueError()
|
2015-03-12 16:56:08 -03:00
|
|
|
__len__ = __bool__
|
2011-12-15 16:34:02 -04:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
class C(object):
|
|
|
|
def foo(self):
|
|
|
|
pass
|
|
|
|
foo.__isabstractmethod__ = NotBool()
|
|
|
|
foo = property(foo)
|
|
|
|
C.foo.__isabstractmethod__
|
|
|
|
|
2015-05-13 05:09:59 -03:00
|
|
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
|
|
"Docstrings are omitted with -O2 and above")
|
|
|
|
def test_property_builtin_doc_writable(self):
|
|
|
|
p = property(doc='basic')
|
|
|
|
self.assertEqual(p.__doc__, 'basic')
|
|
|
|
p.__doc__ = 'extended'
|
|
|
|
self.assertEqual(p.__doc__, 'extended')
|
|
|
|
|
|
|
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
|
|
"Docstrings are omitted with -O2 and above")
|
|
|
|
def test_property_decorator_doc_writable(self):
|
2015-12-11 17:48:13 -04:00
|
|
|
class PropertyWritableDoc(object):
|
|
|
|
|
|
|
|
@property
|
|
|
|
def spam(self):
|
|
|
|
"""Eggs"""
|
|
|
|
return "eggs"
|
|
|
|
|
2015-05-13 05:09:59 -03:00
|
|
|
sub = PropertyWritableDoc()
|
|
|
|
self.assertEqual(sub.__class__.spam.__doc__, 'Eggs')
|
|
|
|
sub.__class__.spam.__doc__ = 'Spam'
|
|
|
|
self.assertEqual(sub.__class__.spam.__doc__, 'Spam')
|
2009-05-04 19:59:07 -03:00
|
|
|
|
2018-02-13 07:32:18 -04:00
|
|
|
@support.refcount_test
|
|
|
|
def test_refleaks_in___init__(self):
|
|
|
|
gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
|
|
|
|
fake_prop = property('fget', 'fset', 'fdel', 'doc')
|
|
|
|
refs_before = gettotalrefcount()
|
|
|
|
for i in range(100):
|
|
|
|
fake_prop.__init__('fget', 'fset', 'fdel', 'doc')
|
|
|
|
self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
|
|
|
|
|
|
|
|
|
2009-05-04 19:59:07 -03:00
|
|
|
# Issue 5890: subclasses of property do not preserve method __doc__ strings
|
|
|
|
class PropertySub(property):
|
|
|
|
"""This is a subclass of property"""
|
|
|
|
|
|
|
|
class PropertySubSlots(property):
|
|
|
|
"""This is a subclass of property that defines __slots__"""
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
class PropertySubclassTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_slots_docstring_copy_exception(self):
|
|
|
|
try:
|
|
|
|
class Foo(object):
|
|
|
|
@PropertySubSlots
|
|
|
|
def spam(self):
|
|
|
|
"""Trying to copy this docstring will raise an exception"""
|
|
|
|
return 1
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise Exception("AttributeError not raised")
|
|
|
|
|
2010-02-23 21:46:21 -04:00
|
|
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
|
|
"Docstrings are omitted with -O2 and above")
|
|
|
|
def test_docstring_copy(self):
|
|
|
|
class Foo(object):
|
|
|
|
@PropertySub
|
|
|
|
def spam(self):
|
|
|
|
"""spam wrapped in property subclass"""
|
|
|
|
return 1
|
|
|
|
self.assertEqual(
|
|
|
|
Foo.spam.__doc__,
|
|
|
|
"spam wrapped in property subclass")
|
|
|
|
|
2013-01-28 07:25:44 -04:00
|
|
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
2010-02-23 21:46:21 -04:00
|
|
|
"Docstrings are omitted with -O2 and above")
|
2009-05-04 19:59:07 -03:00
|
|
|
def test_property_setter_copies_getter_docstring(self):
|
|
|
|
class Foo(object):
|
|
|
|
def __init__(self): self._spam = 1
|
|
|
|
@PropertySub
|
|
|
|
def spam(self):
|
|
|
|
"""spam wrapped in property subclass"""
|
|
|
|
return self._spam
|
|
|
|
@spam.setter
|
|
|
|
def spam(self, value):
|
|
|
|
"""this docstring is ignored"""
|
|
|
|
self._spam = value
|
|
|
|
foo = Foo()
|
|
|
|
self.assertEqual(foo.spam, 1)
|
|
|
|
foo.spam = 2
|
|
|
|
self.assertEqual(foo.spam, 2)
|
|
|
|
self.assertEqual(
|
|
|
|
Foo.spam.__doc__,
|
|
|
|
"spam wrapped in property subclass")
|
|
|
|
class FooSub(Foo):
|
|
|
|
@Foo.spam.setter
|
|
|
|
def spam(self, value):
|
|
|
|
"""another ignored docstring"""
|
|
|
|
self._spam = 'eggs'
|
|
|
|
foosub = FooSub()
|
|
|
|
self.assertEqual(foosub.spam, 1)
|
|
|
|
foosub.spam = 7
|
|
|
|
self.assertEqual(foosub.spam, 'eggs')
|
|
|
|
self.assertEqual(
|
|
|
|
FooSub.spam.__doc__,
|
|
|
|
"spam wrapped in property subclass")
|
|
|
|
|
2013-01-28 07:25:44 -04:00
|
|
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
2010-02-23 21:46:21 -04:00
|
|
|
"Docstrings are omitted with -O2 and above")
|
2009-05-04 19:59:07 -03:00
|
|
|
def test_property_new_getter_new_docstring(self):
|
|
|
|
|
|
|
|
class Foo(object):
|
|
|
|
@PropertySub
|
|
|
|
def spam(self):
|
|
|
|
"""a docstring"""
|
|
|
|
return 1
|
|
|
|
@spam.getter
|
|
|
|
def spam(self):
|
|
|
|
"""a new docstring"""
|
|
|
|
return 2
|
|
|
|
self.assertEqual(Foo.spam.__doc__, "a new docstring")
|
|
|
|
class FooBase(object):
|
|
|
|
@PropertySub
|
|
|
|
def spam(self):
|
|
|
|
"""a docstring"""
|
|
|
|
return 1
|
|
|
|
class Foo2(FooBase):
|
|
|
|
@FooBase.spam.getter
|
|
|
|
def spam(self):
|
|
|
|
"""a new docstring"""
|
|
|
|
return 2
|
|
|
|
self.assertEqual(Foo.spam.__doc__, "a new docstring")
|
|
|
|
|
|
|
|
|
|
|
|
|
Merged revisions 59488-59511 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59489 | christian.heimes | 2007-12-14 03:33:57 +0100 (Fri, 14 Dec 2007) | 1 line
Silence a warning about an unsed variable in debug builds
........
r59490 | christian.heimes | 2007-12-14 03:35:23 +0100 (Fri, 14 Dec 2007) | 2 lines
Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
........
r59491 | raymond.hettinger | 2007-12-14 03:49:47 +0100 (Fri, 14 Dec 2007) | 1 line
Cleaner method naming convention
........
r59492 | christian.heimes | 2007-12-14 04:02:34 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed a warning in _codecs_iso2022.c and some non C89 conform // comments.
........
r59493 | christian.heimes | 2007-12-14 05:38:13 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed warning in ssl module
........
r59500 | raymond.hettinger | 2007-12-14 19:08:20 +0100 (Fri, 14 Dec 2007) | 1 line
Add line spacing for readability
........
r59501 | raymond.hettinger | 2007-12-14 19:12:21 +0100 (Fri, 14 Dec 2007) | 3 lines
Update method names for named tuples.
........
r59503 | georg.brandl | 2007-12-14 20:03:36 +0100 (Fri, 14 Dec 2007) | 3 lines
Add a section about nested listcomps to the tutorial.
Thanks to Ian Bruntlett and Robert Lehmann.
........
r59504 | raymond.hettinger | 2007-12-14 20:19:59 +0100 (Fri, 14 Dec 2007) | 1 line
Faster and simpler _replace() method
........
r59505 | raymond.hettinger | 2007-12-14 22:51:50 +0100 (Fri, 14 Dec 2007) | 1 line
Add usage note
........
r59507 | andrew.kuchling | 2007-12-14 23:41:18 +0100 (Fri, 14 Dec 2007) | 1 line
Remove warning about URL
........
r59510 | andrew.kuchling | 2007-12-14 23:52:36 +0100 (Fri, 14 Dec 2007) | 1 line
Bump the version number, and make a few small edits
........
r59511 | christian.heimes | 2007-12-15 00:42:36 +0100 (Sat, 15 Dec 2007) | 2 lines
Fixed bug #1628
The detection now works on Unix with Makefile, Makefile with VPATH and on Windows.
........
2007-12-14 21:27:15 -04:00
|
|
|
if __name__ == '__main__':
|
2015-04-13 17:00:43 -03:00
|
|
|
unittest.main()
|