[Python-bugs-list] [ python-Bugs-467381 ] print statement and exception (original) (raw)

noreply@sourceforge.net noreply@sourceforge.net
Fri, 05 Oct 2001 15:56:23 -0700


Bugs item #467381, was opened at 2001-10-02 19:19 You can respond by visiting: http://sourceforge.net/tracker/?func=detail&atid=105470&aid=467381&group_id=5470

Category: Python Interpreter Core Group: Not a Bug Status: Closed Resolution: Invalid Priority: 5 Submitted By: paul rubin (phr) Assigned to: Guido van Rossum (gvanrossum) Summary: print statement and exception

Initial Comment: a = 3 b = 0 try: print "quotient is",a/b except: pass print "sum is", a+b

prints something like "quotient is sum is 3". The first print statement prints "quotient is" before the exception is thrown, and then doesn't print the final newline. The final newline should be protected by the equivalent of a "finally" clause.


Comment By: paul rubin (phr) Date: 2001-10-05 15:56

Message: Logged In: YES user_id=72053

I believe that's inconsistent with the documentation, (http://www.python.org/doc/current/ref/print.html), which says 'A "\n" character is written at the end, unless the print statement ends with a comma.'. It also says "print evaluates each expression in turn and writes the resulting object to the standard output", which could be interpreted to mean it builds up a tuple of the value of all the expressions, then prints the tuple. In that situation, an exception evaluating one of the expressions could result in printing nothing at all, or just printing a blank line. Anyway, I never would have guessed the actual behavior by reading the documentation.

At minimum, the doc should be clarified.

Regards

Paul


Comment By: Guido van Rossum (gvanrossum) Date: 2001-10-02 20:08

Message: Logged In: YES user_id=6380

Sorry, this is how it's defined. The print statement does its things one at a time. If one of its things raises an exception, then the remaining things won't be printed -- including the newline.

If that's not what you want, calculate the value and catch the exception before printing the value, or use a print with a trailing comma (suppresses the newline printing) and use an empty print statement separately (supplies the newline), like this:

try: print "quotient is", a/b, except: pass print


You can respond by visiting: http://sourceforge.net/tracker/?func=detail&atid=105470&aid=467381&group_id=5470