3 new commits in pytest: https://bitbucket.org/hpk42/pytest/commits/2e18fb60a375/ Changeset: 2e18fb60a375 Branch: document_ExceptionInfo_ref_cycle User: jurko Date: 2014-01-23 09:46:36 Summary: document explicitly clearing local references to pytest.raises() results
pytest.raises() returns an ExceptionInfo object which, if a local reference is made to it, forms a reference cycle: ExceptionInfo --> exception --> stack frame raising the exception --> current stack frame --> current local variables --> Exception Info Such a reference cycle would then prevent any local variables in the current stack frame, or any of its child stack frames involved in the same reference cycle, from being garbage collected until the next reference cycle garbage collection phase. This unnecessarily increases the program's memory footprint and potentially slows it down. This situation is based on a similar one described in the official 'try' statement Python documentation for locally stored exception references. Affected #: 1 file diff -r 60052491d38bf3fd68909c35aa04e063415fe2b6 -r 2e18fb60a37595729a4ec9fbed78091f84a2cf20 _pytest/python.py --- a/_pytest/python.py +++ b/_pytest/python.py @@ -938,6 +938,12 @@ This helper produces a ``py.code.ExceptionInfo()`` object. + Note that any local references made to such returned + ``py.code.ExceptionInfo()`` objects should be explicitly cleared, as they + are part of a reference cycle similar to local references to caught Python + exception objects. See the official Python ``try`` statement documentation + for more detailed information. + If using Python 2.5 or above, you may use this function as a context manager:: https://bitbucket.org/hpk42/pytest/commits/89e8707d6b2f/ Changeset: 89e8707d6b2f Branch: document_ExceptionInfo_ref_cycle User: jurko Date: 2014-01-23 11:36:04 Summary: reword note on explicitly clearing local references to pytest.raises() results Made it clearer that clearing such references is not mandatory and is only an optional step which may help the Python interpreter speed up its garbage collection. Affected #: 1 file diff -r 2e18fb60a37595729a4ec9fbed78091f84a2cf20 -r 89e8707d6b2f45a79331f5865a2a42bf11f681d3 _pytest/python.py --- a/_pytest/python.py +++ b/_pytest/python.py @@ -938,12 +938,6 @@ This helper produces a ``py.code.ExceptionInfo()`` object. - Note that any local references made to such returned - ``py.code.ExceptionInfo()`` objects should be explicitly cleared, as they - are part of a reference cycle similar to local references to caught Python - exception objects. See the official Python ``try`` statement documentation - for more detailed information. - If using Python 2.5 or above, you may use this function as a context manager:: @@ -968,6 +962,22 @@ >>> raises(ZeroDivisionError, "f(0)") <ExceptionInfo ...> + + Performance note: + ----------------- + + Similar to caught exception objects in Python, explicitly clearing local + references to returned ``py.code.ExceptionInfo`` objects can help the Python + interpreter speed up its garbage collection. + + Clearing those references breaks a reference cycle (``ExceptionInfo`` --> + caught exception --> frame stack raising the exception --> current frame + stack --> local variables --> ``ExceptionInfo``) which makes Python keep all + objects referenced from that cycle (including all local variables in the + current frame) alive until the next cyclic garbage collection run. See the + official Python ``try`` statement documentation for more detailed + information. + """ __tracebackhide__ = True if ExpectedException is AssertionError: https://bitbucket.org/hpk42/pytest/commits/7432b6d6f51d/ Changeset: 7432b6d6f51d User: hpk42 Date: 2014-01-23 11:42:51 Summary: Merged in jurko/pytest/document_ExceptionInfo_ref_cycle (pull request #109) document explicitly clearing local references to pytest.raises() results (redo of pull request #98) Affected #: 1 file diff -r b96fa77ac4af935a5a7b967ab11ece8bb02e6012 -r 7432b6d6f51d935054290c8020d211eb07af485e _pytest/python.py --- a/_pytest/python.py +++ b/_pytest/python.py @@ -962,6 +962,22 @@ >>> raises(ZeroDivisionError, "f(0)") <ExceptionInfo ...> + + Performance note: + ----------------- + + Similar to caught exception objects in Python, explicitly clearing local + references to returned ``py.code.ExceptionInfo`` objects can help the Python + interpreter speed up its garbage collection. + + Clearing those references breaks a reference cycle (``ExceptionInfo`` --> + caught exception --> frame stack raising the exception --> current frame + stack --> local variables --> ``ExceptionInfo``) which makes Python keep all + objects referenced from that cycle (including all local variables in the + current frame) alive until the next cyclic garbage collection run. See the + official Python ``try`` statement documentation for more detailed + information. + """ __tracebackhide__ = True if ExpectedException is AssertionError: Repository URL: https://bitbucket.org/hpk42/pytest/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. _______________________________________________ pytest-commit mailing list pytest-commit@python.org https://mail.python.org/mailman/listinfo/pytest-commit