Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25421

Re: assertraises behaviour

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'interpreter': 0.04; 'context': 0.05; 'executed': 0.07; 'raised': 0.07; 'raises': 0.07; 'stops': 0.07; 'try:': 0.07; 'python': 0.09; '%r"': 0.09; 'block.': 0.09; 'option:': 0.09; 'oserror': 0.09; 'raised.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '"it\'s': 0.16; '*can*': 0.16; '*should*': 0.16; 'a()': 0.16; 'false:': 0.16; 'invoking': 0.16; 'message-id:@dough.gmane.org': 0.16; 'oserror)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.17; '>>>': 0.18; '(or': 0.18; 'translate': 0.20; 'equivalent': 0.20; '"",': 0.22; 'raise': 0.24; 'testing': 0.24; 'pass': 0.25; 'header:User- Agent:1': 0.26; '(most': 0.27; 'reaches': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'trigger': 0.29; 'url:mailman': 0.29; 'words': 0.29; 'class': 0.29; "i'm": 0.29; 'normally': 0.30; 'code': 0.31; '(and': 0.32; 'url:python': 0.32; 'file': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'christian': 0.34; 'fail': 0.35; 'something': 0.35; 'received:org': 0.36; 'except': 0.36; 'but': 0.36; 'url:org': 0.36; 'test': 0.36; 'should': 0.36; 'quite': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'url:mail': 0.40; 'your': 0.60; 'helps': 0.63; 'more': 0.63; 'show': 0.63; 'therefore': 0.65; 'false;': 0.65; 'sound': 0.65; 'andrea': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: assertraises behaviour
Date Mon, 16 Jul 2012 17:10:49 +0200
Organization None
References <CAF_E5JYANst57yOud+Ot0AbKkePruoCGPUNC5BbyBoMbgaw2_Q@mail.gmail.com> <ju16g6$8o2$1@dough.gmane.org> <CAF_E5JZNrusmb2SziNBOah67ioZG0yf_w5RNJxVVrU8B5Tqzuw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a6fd.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2177.1342451467.4697.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1342451467 news.xs4all.nl 6934 [2001:888:2000:d::a6]:36955
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:25421

Show key headers only | View raw


andrea crotti wrote:

> 2012/7/16 Christian Heimes <lists@cheimes.de>:
>>
>> The OSError isn't catched as the code never reaches the line with "raise
>> OSError". In other words "raise OSError" is never executed as the
>> exception raised by "assert False" stops the context manager.
>>
>> You should avoid testing more than one line of code in a with
>> self.assertRaises() block.
>>
>> Christian
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
> 
> Ok now it's more clear, and normally I only test one thing in the block.
> But I find this quite counter-intuitive, because in the block I want
> to be able to run something that raises the exception I catch (and
> which I'm aware of), but if another exception is raised it *should*
> show it and fail in my opinion..

That doesn't sound like "it's clearer". Perhaps it helps if you translate 
your code into a standard try...except:

>>> class A(Exception): pass
... 
>>> class B(Exception): pass
... 
>>> try:
...     raise A
...     raise B
... except A as e:
...     print "caught %r" % e
... 
caught A()

The try block is left at the "raise A' statement" -- Python doesn't have an 
equivalent of Basic's "resume next". Therefore B (or OSError) is never 
raised.

PS: Strictly speaking your "assert False" is equivalent to 

if __debug__ and False: raise AssertionError

Therefore you *can* trigger the OSError by invoking the interpreter with the
"-O" option:

$ python -c 'assert False; raise OSError'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AssertionError
$ python -O -c 'assert False; raise OSError'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OSError

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: assertraises behaviour Peter Otten <__peter__@web.de> - 2012-07-16 17:10 +0200

csiph-web