MCPcopy Create free account
hub / github.com/celery/celery / _LocalStack

Class _LocalStack

celery/utils/threads.py:210–309  ·  view source on GitHub ↗

Local stack. This class works similar to a :class:`Local` but keeps a stack of objects instead. This is best explained with an example:: >>> ls = LocalStack() >>> ls.push(42) >>> ls.top 42 >>> ls.push(23) >>> ls.top 23 >>> ls

Source from the content-addressed store, hash-verified

208
209
210class _LocalStack:
211 """Local stack.
212
213 This class works similar to a :class:`Local` but keeps a stack
214 of objects instead. This is best explained with an example::
215
216 >>> ls = LocalStack()
217 >>> ls.push(42)
218 >>> ls.top
219 42
220 >>> ls.push(23)
221 >>> ls.top
222 23
223 >>> ls.pop()
224 23
225 >>> ls.top
226 42
227
228 They can be force released by using a :class:`LocalManager` or with
229 the :func:`release_local` function but the correct way is to pop the
230 item from the stack after using. When the stack is empty it will
231 no longer be bound to the current context (and as such released).
232
233 By calling the stack without arguments it will return a proxy that
234 resolves to the topmost item on the stack.
235 """
236
237 def __init__(self):
238 self._local = Local()
239
240 def __release_local__(self):
241 self._local.__release_local__()
242
243 def _get__ident_func__(self):
244 return self._local.__ident_func__
245
246 def _set__ident_func__(self, value):
247 object.__setattr__(self._local, '__ident_func__', value)
248 __ident_func__ = property(_get__ident_func__, _set__ident_func__)
249 del _get__ident_func__, _set__ident_func__
250
251 def __call__(self):
252 def _lookup():
253 rv = self.top
254 if rv is None:
255 raise RuntimeError('object unbound')
256 return rv
257 return Proxy(_lookup)
258
259 def push(self, obj):
260 """Push a new item to the stack."""
261 rv = getattr(self._local, 'stack', None)
262 if rv is None:
263 # pylint: disable=assigning-non-slot
264 # This attribute is defined now.
265 self._local.stack = rv = []
266 rv.append(obj)
267 return rv

Callers 1

test_stackMethod · 0.90

Calls

no outgoing calls

Tested by 1

test_stackMethod · 0.72