← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~allenap/launchpad/cached-property-bug-893074 into lp:launchpad

 

Gavin Panella has proposed merging lp:~allenap/launchpad/cached-property-bug-893074 into lp:launchpad.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #893074 in Launchpad itself: "CachedProperty allows value setting and deleting."
  https://bugs.launchpad.net/launchpad/+bug/893074

For more details, see:
https://code.launchpad.net/~allenap/launchpad/cached-property-bug-893074/+merge/82871

Prevents set/delete operations on cached properties.
-- 
https://code.launchpad.net/~allenap/launchpad/cached-property-bug-893074/+merge/82871
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~allenap/launchpad/cached-property-bug-893074 into lp:launchpad.
=== modified file 'lib/lp/services/doc/propertycache.txt'
--- lib/lp/services/doc/propertycache.txt	2010-10-25 09:02:16 +0000
+++ lib/lp/services/doc/propertycache.txt	2011-11-21 12:40:35 +0000
@@ -14,7 +14,7 @@
     >>> from itertools import count
     >>> counter = count(1)
 
-    >>> class Foo:
+    >>> class Foo(object):
     ...     @cachedproperty
     ...     def bar(self):
     ...         return counter.next()
@@ -123,7 +123,7 @@
 not provided it will be derived from the decorated object. This name
 is the name under which values will be cached.
 
-    >>> class Foo:
+    >>> class Foo(object):
     ...     @cachedproperty("a_in_cache")
     ...     def a(self):
     ...         return 1234
@@ -164,3 +164,17 @@
     5678
     >>> get_property_cache(foo).b
     5678
+
+Cached properties cannot be set or deleted from the host object.
+
+    >>> foo.a = 4321
+    Traceback (most recent call last):
+    ...
+    AttributeError: a_in_cache cannot be set here; instead set
+    explicitly with get_property_cache(object).a_in_cache = 4321
+
+    >>> del foo.a
+    Traceback (most recent call last):
+    ...
+    AttributeError: a_in_cache cannot be deleted here; instead delete
+    explicitly with del get_property_cache(object).a_in_cache

=== modified file 'lib/lp/services/propertycache.py'
--- lib/lp/services/propertycache.py	2010-10-24 12:56:04 +0000
+++ lib/lp/services/propertycache.py	2011-11-21 12:40:35 +0000
@@ -117,6 +117,18 @@
             setattr(cache, self.name, value)
             return value
 
+    def __set__(self, instance, value):
+        raise AttributeError(
+            "%s cannot be set here; instead set explicitly with "
+            "get_property_cache(object).%s = %r" % (
+                self.name, self.name, value))
+
+    def __delete__(self, instance):
+        raise AttributeError(
+            "%s cannot be deleted here; instead delete explicitly "
+            "with del get_property_cache(object).%s" % (
+                self.name, self.name))
+
 
 def cachedproperty(name_or_function):
     """Decorator to create a cached property.

=== modified file 'lib/lp/services/tests/test_doc.py'
--- lib/lp/services/tests/test_doc.py	2010-10-04 19:50:45 +0000
+++ lib/lp/services/tests/test_doc.py	2011-11-21 12:40:35 +0000
@@ -8,10 +8,7 @@
 import os
 
 from canonical.launchpad.testing.systemdocs import LayeredDocFileSuite
-from canonical.testing.layers import (
-    BaseLayer,
-    LaunchpadZopelessLayer,
-    )
+from canonical.testing.layers import BaseLayer
 from lp.services.testing import build_test_suite
 
 
@@ -24,7 +21,7 @@
         layer=BaseLayer),
     'propertycache.txt': LayeredDocFileSuite(
         '../doc/propertycache.txt',
-        layer=LaunchpadZopelessLayer),
+        layer=BaseLayer),
     }