Local objects cannot manage themselves. For that you need a local manager. You can pass a local manager multiple locals or add them later by appending them to ``manager.locals``. Every time the manager cleans up, it will clean up all the data left in the locals for this context
| 310 | |
| 311 | |
| 312 | class LocalManager: |
| 313 | """Local objects cannot manage themselves. |
| 314 | |
| 315 | For that you need a local manager. |
| 316 | You can pass a local manager multiple locals or add them |
| 317 | later by appending them to ``manager.locals``. Every time the manager |
| 318 | cleans up, it will clean up all the data left in the locals for this |
| 319 | context. |
| 320 | |
| 321 | The ``ident_func`` parameter can be added to override the default ident |
| 322 | function for the wrapped locals. |
| 323 | """ |
| 324 | |
| 325 | def __init__(self, locals=None, ident_func=None): |
| 326 | if locals is None: |
| 327 | self.locals = [] |
| 328 | elif isinstance(locals, Local): |
| 329 | self.locals = [locals] |
| 330 | else: |
| 331 | self.locals = list(locals) |
| 332 | if ident_func is not None: |
| 333 | self.ident_func = ident_func |
| 334 | for local in self.locals: |
| 335 | object.__setattr__(local, '__ident_func__', ident_func) |
| 336 | else: |
| 337 | self.ident_func = get_ident |
| 338 | |
| 339 | def get_ident(self): |
| 340 | """Return context identifier. |
| 341 | |
| 342 | This is the identifier the local objects use internally |
| 343 | for this context. You cannot override this method to change the |
| 344 | behavior but use it to link other context local objects (such as |
| 345 | SQLAlchemy's scoped sessions) to the Werkzeug locals. |
| 346 | """ |
| 347 | return self.ident_func() |
| 348 | |
| 349 | def cleanup(self): |
| 350 | """Manually clean up the data in the locals for this context. |
| 351 | |
| 352 | Call this at the end of the request or use ``make_middleware()``. |
| 353 | """ |
| 354 | for local in self.locals: |
| 355 | release_local(local) |
| 356 | |
| 357 | def __repr__(self): |
| 358 | return '<{} storages: {}>'.format( |
| 359 | self.__class__.__name__, len(self.locals)) |
| 360 | |
| 361 | |
| 362 | class _FastLocalStack(threading.local): |