Matthew Treinish | 0db5377 | 2013-07-26 10:39:35 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2011 OpenStack Foundation. |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Matthew Treinish | ffa94d6 | 2013-09-11 18:09:17 +0000 | [diff] [blame^] | 18 | """Local storage of variables using weak references""" |
Matthew Treinish | 0db5377 | 2013-07-26 10:39:35 -0400 | [diff] [blame] | 19 | |
Matthew Treinish | ffa94d6 | 2013-09-11 18:09:17 +0000 | [diff] [blame^] | 20 | import threading |
Matthew Treinish | 0db5377 | 2013-07-26 10:39:35 -0400 | [diff] [blame] | 21 | import weakref |
| 22 | |
Matthew Treinish | 0db5377 | 2013-07-26 10:39:35 -0400 | [diff] [blame] | 23 | |
Matthew Treinish | ffa94d6 | 2013-09-11 18:09:17 +0000 | [diff] [blame^] | 24 | class WeakLocal(threading.local): |
Matthew Treinish | 0db5377 | 2013-07-26 10:39:35 -0400 | [diff] [blame] | 25 | def __getattribute__(self, attr): |
Matthew Treinish | ffa94d6 | 2013-09-11 18:09:17 +0000 | [diff] [blame^] | 26 | rval = super(WeakLocal, self).__getattribute__(attr) |
Matthew Treinish | 0db5377 | 2013-07-26 10:39:35 -0400 | [diff] [blame] | 27 | if rval: |
| 28 | # NOTE(mikal): this bit is confusing. What is stored is a weak |
| 29 | # reference, not the value itself. We therefore need to lookup |
| 30 | # the weak reference and return the inner value here. |
| 31 | rval = rval() |
| 32 | return rval |
| 33 | |
| 34 | def __setattr__(self, attr, value): |
| 35 | value = weakref.ref(value) |
Matthew Treinish | ffa94d6 | 2013-09-11 18:09:17 +0000 | [diff] [blame^] | 36 | return super(WeakLocal, self).__setattr__(attr, value) |
Matthew Treinish | 0db5377 | 2013-07-26 10:39:35 -0400 | [diff] [blame] | 37 | |
| 38 | |
| 39 | # NOTE(mikal): the name "store" should be deprecated in the future |
| 40 | store = WeakLocal() |
| 41 | |
| 42 | # A "weak" store uses weak references and allows an object to fall out of scope |
| 43 | # when it falls out of scope in the code that uses the thread local storage. A |
| 44 | # "strong" store will hold a reference to the object so that it never falls out |
| 45 | # of scope. |
| 46 | weak_store = WeakLocal() |
Matthew Treinish | ffa94d6 | 2013-09-11 18:09:17 +0000 | [diff] [blame^] | 47 | strong_store = threading.local() |