|
| 1 | +# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | +# |
| 4 | +# The Universal Permissive License (UPL), Version 1.0 |
| 5 | +# |
| 6 | +# Subject to the condition set forth below, permission is hereby granted to any |
| 7 | +# person obtaining a copy of this software, associated documentation and/or |
| 8 | +# data (collectively the "Software"), free of charge and under any and all |
| 9 | +# copyright rights in the Software, and any and all patent rights owned or |
| 10 | +# freely licensable by each licensor hereunder covering either (i) the |
| 11 | +# unmodified Software as contributed to or provided by such licensor, or (ii) |
| 12 | +# the Larger Works (as defined below), to deal in both |
| 13 | +# |
| 14 | +# (a) the Software, and |
| 15 | +# |
| 16 | +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 17 | +# one is included with the Software each a "Larger Work" to which the Software |
| 18 | +# is contributed by such licensors), |
| 19 | +# |
| 20 | +# without restriction, including without limitation the rights to copy, create |
| 21 | +# derivative works of, display, perform, and distribute the Software and make, |
| 22 | +# use, sell, offer for sale, import, export, have made, and have sold the |
| 23 | +# Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 24 | +# either these or other terms. |
| 25 | +# |
| 26 | +# This license is subject to the following condition: |
| 27 | +# |
| 28 | +# The above copyright notice and either this complete permission notice or at a |
| 29 | +# minimum a reference to the UPL must be included in all copies or substantial |
| 30 | +# portions of the Software. |
| 31 | +# |
| 32 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 33 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 34 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 35 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 36 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 37 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 38 | +# SOFTWARE. |
| 39 | + |
| 40 | +counter = 0 |
| 41 | + |
| 42 | +class Foo: |
| 43 | + def __setattr__(self, key, value): |
| 44 | + global counter |
| 45 | + counter = counter + 1 |
| 46 | + object.__setattr__(self, key, value) |
| 47 | + def __delattr__(self, key): |
| 48 | + global counter |
| 49 | + counter = counter + 10 |
| 50 | + object.__delattr__(self, key) |
| 51 | + |
| 52 | +def test_call(): |
| 53 | + global counter |
| 54 | + counter = 0 |
| 55 | + f = Foo() |
| 56 | + f.a = 1 |
| 57 | + Foo.b = 123 |
| 58 | + assert counter == 1, "setting attrib on class should not call its own __setattr__" |
| 59 | + del f.a |
| 60 | + del Foo.b |
| 61 | + assert counter == 11, "deleting attrib on class should not call its own __delattr__" |
| 62 | + |
| 63 | +class AClass: |
| 64 | + pass |
| 65 | + |
| 66 | +class BClass(AClass): |
| 67 | + pass |
| 68 | + |
| 69 | +class CClass(BClass): |
| 70 | + pass |
| 71 | + |
| 72 | +class DClass(BClass): |
| 73 | + pass |
| 74 | + |
| 75 | +def custom_set(self, key, value): |
| 76 | + object.__setattr__(self, key, value + 10 if isinstance(value, int) else value) |
| 77 | + |
| 78 | +def custom_get(self, key): |
| 79 | + value = object.__getattribute__(self, key) |
| 80 | + return value + 100 if isinstance(value, int) else value |
| 81 | + |
| 82 | +def test_assignments(): |
| 83 | + object = CClass() |
| 84 | + # writing to BClass changes the result, writing to DClass doesn't |
| 85 | + targets = (AClass, BClass, DClass, CClass, object) |
| 86 | + results = (0, 1, 1, 3, 4) |
| 87 | + for i in range(0, len(targets)): |
| 88 | + targets[i].foo = i |
| 89 | + assert object.foo == results[i], "normal %d" %i |
| 90 | + # make sure that a custom __getattribute__ is used |
| 91 | + BClass.__getattribute__ = custom_get |
| 92 | + for i in range(0, len(targets)): |
| 93 | + targets[i].bar = i |
| 94 | + assert object.bar == results[i] + 100, "custom get %d" % i |
| 95 | + # check correct lookups when deleting attributes |
| 96 | + for i in reversed(range(0, len(targets))): |
| 97 | + assert object.bar == results[i] + 100, "delete %d" % i |
| 98 | + del targets[i].bar |
| 99 | + # make sure a custom __setattr__ is used |
| 100 | + BClass.__setattr__ = custom_set |
| 101 | + object.baz = 9 |
| 102 | + assert object.baz == 119, "custom set" |
| 103 | + |
| 104 | + |
0 commit comments