[Python-Dev] Problems with regrtest and with logging

Nick Coghlan ncoghlan at gmail.com
Mon May 9 17:00:38 CEST 2011


On Sat, May 7, 2011 at 3:51 AM, Éric Araujo <merwok at netwok.org> wrote:
> regrtest helpfully reports when a test leaves the environment unclean
> (sys.path, os.environ, logging._handlerList), but I think the implementation
> is buggy: it compares object identity and then value.  Why is comparing
> identity useful?  I’d just use ==.  It makes writing cleanup code easier
> (just use addCleanup(setattr, obj, 'attr', copy(obj.attr))).

Because changing the identity of any of those global state attributes
that regrtest monitors is itself suggestive of a bug. When it comes to
containers, identity matters at least as much as value does (and
sometimes more so - e.g. sys.modules). Replacing those global
containers with new ones isn't guaranteed to work, as they may be
cached in various places rather than always retrieved fresh from the
relevant module namespace. Modifying them in place, on the other hand,
does the right thing even in the presence of cached references.

A comment to that effect may be a useful addition to regrtest, as I
expect others may have similar questions about those identity checks
in the future. (It may even be a useful addition to the documentation,
but I have no idea where it could be sensibly included).

Also, don't be surprised if wholesale cleanup like that isn't
completely reliable - it's far, far better if the test case
understands the changes it is making (even indirectly) and explicitly
reverses them. Save-and-restore should be a last resort technique
(although context managers that are designed for more general use,
such as warnings.catch_warnings(), use save-and-restore by necessity,
since they have no control over the body of the relevant with
statements).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list