7 Replies - 256 Views - Last Post: 15 August 2014 - 06:39 AM Rate Topic: -----

#1 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 202
  • Joined: 21-April 09

try-finally bug?

Posted 12 August 2014 - 01:22 PM

I think there's a bug with try-finally when used within a def

>>> try:
...     raise KeyboardInterrupt
... finally:
...     print("Goodbye, world!")
...
Goodbye, world!
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyboardInterrupt
>>> def foo():
...     try:
...             raise KeyboardInterrupt
...     finally:
...             return "Goodbye."
...
>>> foo()
'Goodbye.'


It would seem to me that when I call foo I should see a trackback and the return value. What say you?

Running on Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32

This post has been edited by code_m: 12 August 2014 - 01:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: try-finally bug?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

Re: try-finally bug?

Posted 13 August 2014 - 04:59 AM

Interesting. Looks like your return in finally is hijacking the exception bubble up. You actually shouldn't get a return value from an unhandled exception, so there'd never be a reason to put a return in a finally...

The works as expected:
>>> def foo():
...     result = None
...     try:
...         raise KeyboardInterrupt
...     finally:
...         result = "Goodbye."
...     return result
... 
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in foo
KeyboardInterrupt
>>> 


This post has been edited by baavgai: 13 August 2014 - 04:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 202
  • Joined: 21-April 09

Re: try-finally bug?

Posted 13 August 2014 - 11:40 AM

HHmmm. This doesn't seem to be a bug. The equivalent java code has the same behavior.

public class Foo
{
    public static void main(String[] args)
        throws Exception
    {
        try
        {
            throw new Exception();
        }
        finally
        {
            System.out.println("Goodbye");
            return;
        }
    }
}


This will print out only "Goodbye". Move the return out of the finally block and you will get both the stack trace and "Goodbye" statement.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: try-finally bug?

Posted 13 August 2014 - 12:38 PM

In the first case, your expression evaluates to the stack trace, and this is what's printed. In the second, it evaluates to the value returned.

Consider these expressions:

>>> a = 56
>>> a
56
>>> def b():
...    return 56
...
>>>b()
56
>>> print a
56
>>> def print_and_return(x):
...   print x
...   return x
... 
>>> print_and_return(a)
56
56



In the first case, you see a value because the interpreter helpfully tells you what your expression evaluated to. In the second case, you again see 56 because again, that's what the expression evaluated to.
In the third case, you see 56 because print sends 56 to the output.
In the last case, you see 56 twice because print sends 56 to the output, and then python helpfully shows you the value that was returned from the function.

Exercise:
define a function "just_print(x)" which prints x (like the python3 print() function) and returns nothing. Decide what you expect the output to be from the following line, and why.

>>> print just_print(x)


Then try it.
Was This Post Helpful? 0
  • +
  • -

#5 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 202
  • Joined: 21-April 09

Re: try-finally bug?

Posted 13 August 2014 - 05:18 PM

View Postjon.kiparsky, on 13 August 2014 - 03:38 PM, said:

In the first case, your expression evaluates to the stack trace, and this is what's printed. In the second, it evaluates to the value returned.

...

In the first case, you see a value because the interpreter helpfully tells you what your expression evaluated to. In the second case, you again see 56 because again, that's what the expression evaluated to.
In the third case, you see 56 because print sends 56 to the output.
In the last case, you see 56 twice because print sends 56 to the output, and then python helpfully shows you the value that was returned from the function.


Um? I don't really care if the value is being evaluated (repr) or printed (str). The question was whether both operations should happen or not.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: try-finally bug?

Posted 14 August 2014 - 06:27 PM

Perhaps my explanation doesn't work as well as I thought.

Try this and see if it helps:

>>> def bar():
...     raise KeyboardInterrupt
... 
>>> bar()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in bar
KeyboardInterrupt
>>> def baz():
...     bar
... 
>>> baz()
>>> 



It seems to me that python just doesn't propagate an interrupt unless asked to.


On the other hand, an Error is propagated.

>>> def nameError():
...     try:
...             raise NameError
...     finally:
...             print "foo!"
... 
>>> nameError()
foo!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in nameError
NameError
>>> 

Was This Post Helpful? 0
  • +
  • -

#7 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 202
  • Joined: 21-April 09

Re: try-finally bug?

Posted 15 August 2014 - 06:10 AM

Yes, that's exactly what I discovered when I posted the Java snippet. When you (incorrectly) place a return in the finally block it will hide any thrown (or raised) error. I found exactly the same behavior in both languages. Maybe I'll do a test in C# too, but I'm pretty sure the behavior is intentional.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: try-finally bug?

Posted 15 August 2014 - 06:39 AM

Look again at that second example. The return in the finally doesn't hide an error. The important thing is the distinction between an interrupt and an error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1