~ubuntu-branches/ubuntu/quantal/python-gevent/quantal

« back to all changes in this revision

Viewing changes to gevent/local.py

  • Committer: Bazaar Package Importer
  • Author(s): Örjan Persson
  • Date: 2011-05-17 16:43:20 UTC
  • mfrom: (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110517164320-nwhld2xo6sz58p4m
Tags: 0.13.6-1
New upstream version (Closes: #601863).

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
  ...     log.append(items)
33
33
  ...     mydata.number = 11
34
34
  ...     log.append(mydata.number)
35
 
 
36
35
  >>> greenlet = gevent.spawn(f)
37
36
  >>> greenlet.join()
38
37
  >>> log
128
127
>>> del mydata
129
128
"""
130
129
from weakref import WeakKeyDictionary
 
130
from copy import copy
131
131
from gevent.hub import getcurrent
132
132
from gevent.coros import RLock
133
133
 
134
134
__all__ = ["local"]
135
135
 
 
136
 
136
137
class _localbase(object):
137
138
    __slots__ = '_local__args', '_local__lock', '_local__dicts'
138
139
 
171
172
    def __getattribute__(self, name):
172
173
        d = object.__getattribute__(self, '_local__dicts').get(getcurrent())
173
174
        if d is None:
174
 
            # we can obtain the lock here and not earlier, because the above has no switches out
175
 
            # however, subclassed __init__ may switch so we do need obtain the lock here
 
175
            # it's OK to acquire the lock here and not earlier, because the above code won't switch out
 
176
            # however, subclassed __init__ might switch, so we do need to acquire the lock here
176
177
            lock = object.__getattribute__(self, '_local__lock')
177
178
            lock.acquire()
178
179
            try:
185
186
            return object.__getattribute__(self, name)
186
187
 
187
188
    def __setattr__(self, name, value):
 
189
        if name == '__dict__':
 
190
            raise AttributeError("%r object attribute '__dict__' is read-only" % self.__class__.__name__)
188
191
        d = object.__getattribute__(self, '_local__dicts').get(getcurrent())
189
192
        if d is None:
190
193
            lock = object.__getattribute__(self, '_local__lock')
199
202
            return object.__setattr__(self, name, value)
200
203
 
201
204
    def __delattr__(self, name):
 
205
        if name == '__dict__':
 
206
            raise AttributeError("%r object attribute '__dict__' is read-only" % self.__class__.__name__)
202
207
        d = object.__getattribute__(self, '_local__dicts').get(getcurrent())
203
208
        if d is None:
204
209
            lock = object.__getattribute__(self, '_local__lock')
211
216
        else:
212
217
            object.__setattr__(self, '__dict__', d)
213
218
            return object.__delattr__(self, name)
 
219
 
 
220
    def __copy__(self):
 
221
        currentId = getcurrent()
 
222
        d = object.__getattribute__(self, '_local__dicts').get(currentId)
 
223
        duplicate = copy(d)
 
224
 
 
225
        cls = type(self)
 
226
        if cls.__init__ is not object.__init__:
 
227
            args, kw = object.__getattribute__(self, '_local__args')
 
228
            instance = cls(*args, **kw)
 
229
        else:
 
230
            instance = cls()
 
231
 
 
232
        object.__setattr__(instance, '_local__dicts', {
 
233
            currentId: duplicate
 
234
        })
 
235
 
 
236
        return instance