This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unittest.main negates -bb option and programmatic warning configuration
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder: unittest overrides more serious warnings filter added before unittest.main()
View: 42186
Assigned To: Nosy List: Arfrever, Ben.Darnell, chris.jerdonek, ezio.melotti, michael.foord, ncoghlan, rbcollins, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2012-08-12 00:12 by Ben.Darnell, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
testbb.py Ben.Darnell, 2012-08-12 00:12
Messages (6)
msg168013 - (view) Author: Ben Darnell (Ben.Darnell) * Date: 2012-08-12 00:12
In python 3.2, unittest.main by default modifies the warning configuration if no -W options were given on the command line.  This undoes the effect of -bb, turning BytesWarning back into a warning instead of an error.

If both -bb and -Werror::BytesWarning are given, then unittest does not tamper with the warning configuration and byte operations raise errors as expected (but note that -Werror doesn't work in the current version of virtualenv due to https://github.com/pypa/virtualenv/issues/194 ).  unittest.main should check for the -bb flag in addition to sys.warnoptions.

Additionally, if warning filters were modified programmatically before calling unittest.main, unittest will still modify the configuration and potentially override the user's settings.  This is a rarer case, but I've done it before to avoid the problem with -W in a virtualenv (https://github.com/facebook/tornado/blob/master/tornado/test/runtests.py).  It would be good for there to be some way to tell unittest.main not to touch the warnings at all.  main(warnings=False) currently works, but that's relying on an implementation detail (any non-None, non-truthy value works), so either this should be documented or an official way to do the same thing should be added.

The attached test file demonstrates the problem:  when run with -bb, an warning is logged, but the test passes.
msg222693 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-10 19:40
The behaviour is still the same for 3.4.1 and 3.5.0a0 on Windows 7.
msg331688 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-12 10:47
Something was changed in 3.7, but I do not see differences in unittest that can cause this.

$ python3.6 -bb testbb.py 
testbb.py:7: BytesWarning: str() on a bytes instance
  str(b"")
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

$ python3.7 -bb testbb.py 
E
======================================================================
ERROR: test_foo (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "testbb.py", line 7, in test_foo
    str(b"")
BytesWarning: str() on a bytes instance

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)


Perhaps it is related to issue20361.
msg331696 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-12 15:05
> Something was changed in 3.7

Yeah, Python 3.7 handles warning options differently: there is the PEP 565 and this commit:

commit 747f48e2e92390c44c72f52a1239959601cde157
Author: Victor Stinner <victor.stinner@gmail.com>
Date:   Tue Dec 12 22:59:48 2017 +0100

    bpo-32230: Set sys.warnoptions with -X dev (#4820)
    
    Rather than supporting dev mode directly in the warnings module, this
    instead adjusts the initialisation code to add an extra 'default'
    entry to sys.warnoptions when dev mode is enabled.
    
    This ensures that dev mode behaves *exactly* as if `-Wdefault` had
    been passed on the command line, including in the way it interacts
    with `sys.warnoptions`, and with other command line flags like `-bb`.
    
    Fix also bpo-20361: have -b & -bb options take precedence over any
    other warnings options.
    
    Patch written by Nick Coghlan, with minor modifications of Victor Stinner.
msg332138 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-12-19 13:38
Aye, the relevant change here would be the fact that -b and -bb now modify sys.warnoptions, rather than the warnings module being aware of those command line options specifically: https://docs.python.org/3/whatsnew/3.7.html#warnings

As a result, unittest.main will see a non-empty sys.warnoptions in 3.7+ when -bb is passed.

That means most of Ben's original request has been implemented, with the one remaining aspect being the request for an officially documented and supported way to tell unittest.main to leave the warnings filters alone (regardless of the value of sys.warnoptions)
msg380038 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2020-10-31 05:07
Issue #42186 now covers the request to have a way to tell unittest to leave the warnings filter alone even if it's set programmatically rather than by modifying ``sys.warnoptions``.

(Changing version to 3.7 to indicate when the interaction with `-bb` was fixed, rather than the version it was reported against)
History
Date User Action Args
2022-04-11 14:57:34adminsetgithub: 59831
2020-10-31 05:07:43ncoghlansetsuperseder: unittest overrides more serious warnings filter added before unittest.main()
2020-10-31 05:07:26ncoghlansetstatus: open -> closed
versions: + Python 3.7, - Python 3.6
messages: + msg380038

resolution: fixed
stage: resolved
2018-12-19 13:38:26ncoghlansetmessages: + msg332138
2018-12-12 16:11:09BreamoreBoysetnosy: - BreamoreBoy
2018-12-12 15:05:33vstinnersetmessages: + msg331696
2018-12-12 10:47:02serhiy.storchakasetversions: + Python 3.6, - Python 3.4, Python 3.5
nosy: + rbcollins, serhiy.storchaka, ncoghlan, vstinner

messages: + msg331688

components: + Library (Lib), - Tests
2014-07-10 19:40:44BreamoreBoysetversions: + Python 3.4, Python 3.5, - Python 3.2, Python 3.3
nosy: + BreamoreBoy

messages: + msg222693

components: + Tests
2012-09-05 01:06:25Arfreversetnosy: + Arfrever
2012-08-12 10:37:56pitrousetnosy: + michael.foord

versions: + Python 3.3
2012-08-12 04:56:07r.david.murraysetnosy: + ezio.melotti
2012-08-12 00:59:54chris.jerdoneksetnosy: + chris.jerdonek
2012-08-12 00:12:55Ben.Darnellcreate