diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/collections.py | 28 | ||||
-rw-r--r-- | Lib/distutils/sysconfig.py | 6 | ||||
-rw-r--r-- | Lib/distutils/tests/test_sysconfig.py | 2 | ||||
-rw-r--r-- | Lib/test/test_collections.py | 16 | ||||
-rw-r--r-- | Lib/test/test_property.py | 98 |
5 files changed, 125 insertions, 25 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index 5fb0e05..2a7bb62 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -26,12 +26,12 @@ def namedtuple(typename, field_names, verbose=False): (11, 22) >>> p.x + p.y # fields also accessable by name 33 - >>> d = p.__asdict__() # convert to a dictionary + >>> d = p._asdict() # convert to a dictionary >>> d['x'] 11 >>> Point(**d) # convert from a dictionary Point(x=11, y=22) - >>> p.__replace__(x=100) # __replace__() is like str.replace() but targets named fields + >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields Point(x=100, y=22) """ @@ -49,8 +49,8 @@ def namedtuple(typename, field_names, verbose=False): raise ValueError('Type names and field names cannot start with a number: %r' % name) seen_names = set() for name in field_names: - if name.startswith('__') and name.endswith('__') and len(name) > 3: - raise ValueError('Field names cannot start and end with double underscores: %r' % name) + if name.startswith('_'): + raise ValueError('Field names cannot start with an underscore: %r' % name) if name in seen_names: raise ValueError('Encountered duplicate field name: %r' % name) seen_names.add(name) @@ -59,19 +59,19 @@ def namedtuple(typename, field_names, verbose=False): argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes reprtxt = ', '.join('%s=%%r' % name for name in field_names) template = '''class %(typename)s(tuple): - '%(typename)s(%(argtxt)s)' - __slots__ = () - __fields__ = property(lambda self: %(field_names)r) + '%(typename)s(%(argtxt)s)' \n + __slots__ = () \n + _fields = property(lambda self: %(field_names)r) \n def __new__(cls, %(argtxt)s): - return tuple.__new__(cls, (%(argtxt)s)) + return tuple.__new__(cls, (%(argtxt)s)) \n def __repr__(self): - return '%(typename)s(%(reprtxt)s)' %% self - def __asdict__(self, dict=dict, zip=zip): - 'Return a new dict mapping field names to their values' - return dict(zip(%(field_names)r, self)) - def __replace__(self, **kwds): + return '%(typename)s(%(reprtxt)s)' %% self \n + def _asdict(self, dict=dict, zip=zip): + 'Return a new dict which maps field names to their values' + return dict(zip(%(field_names)r, self)) \n + def _replace(self, **kwds): 'Return a new %(typename)s object replacing specified fields with new values' - return %(typename)s(**dict(zip(%(field_names)r, self), **kwds)) \n''' % locals() + return %(typename)s(*map(kwds.get, %(field_names)r, self)) \n\n''' % locals() for i, name in enumerate(field_names): template += ' %s = property(itemgetter(%d))\n' % (name, i) if verbose: diff --git a/Lib/distutils/sysconfig.py b/Lib/distutils/sysconfig.py index c450cd5..ba89c3b 100644 --- a/Lib/distutils/sysconfig.py +++ b/Lib/distutils/sysconfig.py @@ -31,8 +31,10 @@ if os.name == "nt" and "pcbuild" in project_base[-8:].lower(): # python_build: (Boolean) if true, we're either building Python or # building an extension with an un-installed Python, so we use # different (hard-wired) directories. -python_build = os.path.isfile(os.path.join(project_base, "Modules", - "Setup.local")) +# Setup.local is available for Makefile builds including VPATH builds, +# Setup.dist is available on Windows +python_build = any(os.path.isfile(os.path.join(project_base, "Modules", fn)) + for fn in ("Setup.dist", "Setup.local")) def get_python_version(): """Return a string containing the major and minor Python version, diff --git a/Lib/distutils/tests/test_sysconfig.py b/Lib/distutils/tests/test_sysconfig.py index ef7c38b..770b7c3 100644 --- a/Lib/distutils/tests/test_sysconfig.py +++ b/Lib/distutils/tests/test_sysconfig.py @@ -15,7 +15,7 @@ class SysconfigTestCase(unittest.TestCase): def test_get_python_lib(self): lib_dir = sysconfig.get_python_lib() - # XXX doesn't work on Inux when Python was never installed before + # XXX doesn't work on Linux when Python was never installed before #self.assert_(os.path.isdir(lib_dir), lib_dir) # test for pythonxx.lib? diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 45b4082..5a4029d 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -28,11 +28,11 @@ class TestNamedTuple(unittest.TestCase): self.assertRaises(ValueError, namedtuple, 'abc', 'efg g%hi') # field with non-alpha char self.assertRaises(ValueError, namedtuple, 'abc', 'abc class') # field has keyword self.assertRaises(ValueError, namedtuple, 'abc', '8efg 9ghi') # field starts with digit - self.assertRaises(ValueError, namedtuple, 'abc', '__efg__ ghi') # field with double underscores + self.assertRaises(ValueError, namedtuple, 'abc', '_efg ghi') # field with leading underscore self.assertRaises(ValueError, namedtuple, 'abc', 'efg efg ghi') # duplicate field namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names - namedtuple('_', '_ __ ___') # Verify that underscores are allowed + namedtuple('_', 'a b c') # Test leading underscores in a typename def test_instance(self): Point = namedtuple('Point', 'x y') @@ -49,17 +49,17 @@ class TestNamedTuple(unittest.TestCase): self.assertEqual(repr(p), 'Point(x=11, y=22)') self.assert_('__dict__' not in dir(p)) # verify instance has no dict self.assert_('__weakref__' not in dir(p)) - self.assertEqual(p.__fields__, ('x', 'y')) # test __fields__ attribute - self.assertEqual(p.__replace__(x=1), (1, 22)) # test __replace__ method - self.assertEqual(p.__asdict__(), dict(x=11, y=22)) # test __dict__ method + self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute + self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method + self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method - # Verify that __fields__ is read-only + # Verify that _fields is read-only try: - p.__fields__ = ('F1' ,'F2') + p._fields = ('F1' ,'F2') except AttributeError: pass else: - self.fail('The __fields__ attribute needs to be read-only') + self.fail('The _fields attribute needs to be read-only') # verify that field string can have commas Point = namedtuple('Point', 'x, y') diff --git a/Lib/test/test_property.py b/Lib/test/test_property.py new file mode 100644 index 0000000..4b6e20c --- /dev/null +++ b/Lib/test/test_property.py @@ -0,0 +1,98 @@ +# Test case for property +# more tests are in test_descr + +import unittest +from test.test_support import run_unittest + +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 + +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") + self.assert_(not hasattr(base, "spam")) + self.assert_(not hasattr(base, "_spam")) + base.spam = 20 + self.assertEqual(base.spam, 20) + self.assertEqual(base._spam, 20) + self.assertEqual(base.__class__.spam.__doc__, "BaseClass.getter") + + 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") + self.assertEqual(sub.__class__.spam.__doc__, "SubClass.getter") + + 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") + +def test_main(): + run_unittest(PropertyTests) + +if __name__ == '__main__': + test_main() |