Break up a circular dependency on shutdown
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 11c7b1d · Browse files · 3 files·+93 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+93 added−0 removed
- other:+93 added−0 removed
Provenance
- Commit
7f4c12b- Tree
365a523- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
11c7b1d
3 files changed+93 added−0 removed
added:flask/testsuite/regression.py+83 added
1+# -*- coding: utf-8 -*-2+"""3+ flask.testsuite.regression4+ ~~~~~~~~~~~~~~~~~~~~~~~~~~5+6+ Tests regressions.7+8+ :copyright: (c) 2011 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+12+from __future__ import with_statement13+14+import gc15+import sys16+import flask17+import threading18+import unittest19+from werkzeug.test import run_wsgi_app, create_environ20+from flask.testsuite import FlaskTestCase21+22+23+_gc_lock = threading.Lock()24+25+26+class _NoLeakAsserter(object):27+28+ def __init__(self, testcase):29+ self.testcase = testcase30+31+ def __enter__(self):32+ gc.disable()33+ _gc_lock.acquire()34+ loc = flask._request_ctx_stack._local35+36+ # Force Python to track this dictionary at all times.37+ # This is necessary since Python only starts tracking38+ # dicts if they contain mutable objects. It's a horrible,39+ # horrible hack but makes this kinda testable.40+ loc.__storage__['FOOO'] = [1, 2, 3]41+42+ gc.collect()43+ self.old_objects = len(gc.get_objects())44+45+ def __exit__(self, exc_type, exc_value, tb):46+ if not hasattr(sys, 'getrefcount'):47+ gc.collect()48+ new_objects = len(gc.get_objects())49+ if new_objects > self.old_objects:50+ self.testcase.fail('Example code leaked')51+ _gc_lock.release()52+ gc.enable()53+54+55+class MemoryTestCase(FlaskTestCase):56+57+ def assert_no_leak(self):58+ return _NoLeakAsserter(self)59+60+ def test_memory_consumption(self):61+ app = flask.Flask(__name__)62+ @app.route('/')63+ def index():64+ return flask.render_template('simple_template.html', whiskey=42)65+66+ def fire():67+ with app.test_client() as c:68+ rv = c.get('/')69+ self.assert_equal(rv.status_code, 200)70+ self.assert_equal(rv.data, '<h1>42</h1>')71+72+ # Trigger caches73+ fire()74+75+ with self.assert_no_leak():76+ for x in xrange(10):77+ fire()78+79+80+def suite():81+ suite = unittest.TestSuite()82+ suite.addTest(unittest.makeSuite(MemoryTestCase))83+ return suitemodified:flask/ctx.py+4 added
148 unmodified lines149 rv = _request_ctx_stack.pop()150 assert rv is self, 'Popped wrong request context. (%r instead of %r)' \151 % (rv, self)152 153+ # get rid of circular dependencies at the end of the request154+ # so that we don't require the GC to be active.155+ rv.request.environ['werkzeug.request'] = None156+157 def __enter__(self):158 self.push()159 return self160 19 unmodified linesmodified:CHANGES+6 added
12 unmodified lines13 - The :func:`flask.url_for` function now can also explicitly generate14 URL rules specific to a given HTTP method.15 - Logger now only returns the debug log setting if it was not set16 explicitly.17+- Unregister a circular dependency between the WSGI environment and18+ the request object when shutting down the request. This means that19+ environ ``werkzeug.request`` will be `None` after the response was20+ returned to the WSGI server but has the advantage that the garbage21+ collector is not needed on CPython to tear down the request unless22+ the user created circular dependencies themselves.23 24 Version 0.8.125 -------------26 294 unmodified lines