How to use self-inspection to check for try-block

Discussion in 'Python' started by elmar@cmbi.ru.nl, Mar 20, 2009.

  1. Guest

    Hi everyone,

    is there a sufficiently easy possibility for a Python function to find
    out whether it has been called from a try-block or not?

    try:
    print "Calling foo"
    foo()
    except:
    print "Got exception"

    In the example above, foo() should be able to 'see' that it was called
    from a try block, allowing it to behave differently.

    Can this information be obtained from the traceback/frame/code
    objects, or is that too difficult?

    Many thanks for your help,
    Elmar
     
    , Mar 20, 2009
    #1
    1. Advertising

  2. Chris Rebert Guest

    On Fri, Mar 20, 2009 at 1:32 AM, <> wrote:
    > Hi everyone,
    >
    > is there a sufficiently easy possibility for a Python function to find
    > out whether it has been called from a try-block or not?
    >
    > try:
    >  print "Calling foo"
    >  foo()
    > except:
    >  print "Got exception"
    >
    > In the example above, foo() should be able to 'see' that it was called
    > from a try block, allowing it to behave differently.
    >
    > Can this information be obtained from the traceback/frame/code
    > objects, or is that too difficult?


    It might be possible, but it seems like there ought to be a better way
    to accomplish your goal. Could you explain why you want to do this in
    the first place? Perhaps a better alternative can be found.

    Cheers,
    Chris

    --
    I have a blog:
    http://blog.rebertia.com
     
    Chris Rebert, Mar 20, 2009
    #2
    1. Advertising

  3. Guest

    On Mar 20, 9:44 am, Chris Rebert <> wrote:
    > On Fri, Mar 20, 2009 at 1:32 AM, <> wrote:
    > > Hi everyone,

    >
    > > is there a sufficiently easy possibility for a Python function to find
    > > out whether it has been called from a try-block or not?

    >
    > > try:
    > > print "Calling foo"
    > > foo()
    > > except:
    > > print "Got exception"

    >
    > > In the example above, foo() should be able to 'see' that it was called
    > > from a try block, allowing it to behave differently.

    >
    > > Can this information be obtained from the traceback/frame/code
    > > objects, or is that too difficult?

    >
    > It might be possible, but it seems like there ought to be a better way
    > to accomplish your goal. Could you explain why you want to do this in
    > the first place? Perhaps a better alternative can be found.


    Well, foo() communicates with another application using sockets, and
    an exception might occur in the other application. For performance
    reasons, foo() normally returns before the other application has
    finished execution, unless foo() is forced to wait for the result.
    This can for example be achieved by using foo()'s return value (foo()
    uses self-inspection to see if its return value is discarded or not).

    I also want foo() to wait in case it's in a try block, so that the
    user can catch exceptions that occur in the other application.

    Thanks,
    Elmar
     
    , Mar 20, 2009
    #3
  4. Chris Rebert Guest

    On Fri, Mar 20, 2009 at 2:09 AM, <> wrote:
    > On Mar 20, 9:44 am, Chris Rebert <> wrote:
    >> On Fri, Mar 20, 2009 at 1:32 AM,  <> wrote:
    >> > Hi everyone,

    >>
    >> > is there a sufficiently easy possibility for a Python function to find
    >> > out whether it has been called from a try-block or not?

    >>
    >> > try:
    >> >  print "Calling foo"
    >> >  foo()
    >> > except:
    >> >  print "Got exception"

    >>
    >> > In the example above, foo() should be able to 'see' that it was called
    >> > from a try block, allowing it to behave differently.

    >>
    >> > Can this information be obtained from the traceback/frame/code
    >> > objects, or is that too difficult?

    >>
    >> It might be possible, but it seems like there ought to be a better way
    >> to accomplish your goal. Could you explain why you want to do this in
    >> the first place? Perhaps a better alternative can be found.

    >
    > Well, foo() communicates with another application using sockets, and
    > an exception might occur in the other application. For performance
    > reasons, foo() normally returns before the other application has
    > finished execution, unless foo() is forced to wait for the result.
    > This can for example be achieved by using foo()'s return value (foo()
    > uses self-inspection to see if its return value is discarded or not).
    >
    > I also want foo() to wait in case it's in a try block, so that the
    > user can catch exceptions that occur in the other application.


    Is there any reason you can't just add a parameter (e.g. 'wait') to
    foo() to tell it whether to wait for the exception or not? It's
    certainly less magical than detecting `try` in the caller.

    Cheers,
    Chris

    --
    I have a blog:
    http://blog.rebertia.com
     
    Chris Rebert, Mar 20, 2009
    #4
  5. Guest

    On Mar 20, 10:16 am, Chris Rebert <> wrote:
    > On Fri, Mar 20, 2009 at 2:09 AM, <> wrote:
    > > On Mar 20, 9:44 am, Chris Rebert <> wrote:
    > >> On Fri, Mar 20, 2009 at 1:32 AM, <> wrote:
    > >> > Hi everyone,

    >
    > >> > is there a sufficiently easy possibility for a Python function to find
    > >> > out whether it has been called from a try-block or not?

    >
    > >> > try:
    > >> > print "Calling foo"
    > >> > foo()
    > >> > except:
    > >> > print "Got exception"

    >
    > >> > In the example above, foo() should be able to 'see' that it was called
    > >> > from a try block, allowing it to behave differently.

    >
    > >> > Can this information be obtained from the traceback/frame/code
    > >> > objects, or is that too difficult?

    >
    > >> It might be possible, but it seems like there ought to be a better way
    > >> to accomplish your goal. Could you explain why you want to do this in
    > >> the first place? Perhaps a better alternative can be found.

    >
    > > Well, foo() communicates with another application using sockets, and
    > > an exception might occur in the other application. For performance
    > > reasons, foo() normally returns before the other application has
    > > finished execution, unless foo() is forced to wait for the result.
    > > This can for example be achieved by using foo()'s return value (foo()
    > > uses self-inspection to see if its return value is discarded or not).

    >
    > > I also want foo() to wait in case it's in a try block, so that the
    > > user can catch exceptions that occur in the other application.

    >
    > Is there any reason you can't just add a parameter (e.g. 'wait') to
    > foo() to tell it whether to wait for the exception or not? It's
    > certainly less magical than detecting `try` in the caller.
    >


    The system is used by people who don't know about these technical
    details, and the goal is to hide the complexity from the user, without
    having to explain when to add a 'wait' parameter etc.
    Anyway, thanks for your time. I'll dig something out...

    Ciao,
    Elmar
     
    , Mar 20, 2009
    #5
  6. schrieb:
    > On Mar 20, 10:16 am, Chris Rebert <> wrote:
    >> On Fri, Mar 20, 2009 at 2:09 AM, <> wrote:
    >>> On Mar 20, 9:44 am, Chris Rebert <> wrote:
    >>>> On Fri, Mar 20, 2009 at 1:32 AM, <> wrote:
    >>>>> Hi everyone,
    >>>>> is there a sufficiently easy possibility for a Python function to find
    >>>>> out whether it has been called from a try-block or not?
    >>>>> try:
    >>>>> print "Calling foo"
    >>>>> foo()
    >>>>> except:
    >>>>> print "Got exception"
    >>>>> In the example above, foo() should be able to 'see' that it was called
    >>>>> from a try block, allowing it to behave differently.
    >>>>> Can this information be obtained from the traceback/frame/code
    >>>>> objects, or is that too difficult?
    >>>> It might be possible, but it seems like there ought to be a better way
    >>>> to accomplish your goal. Could you explain why you want to do this in
    >>>> the first place? Perhaps a better alternative can be found.
    >>> Well, foo() communicates with another application using sockets, and
    >>> an exception might occur in the other application. For performance
    >>> reasons, foo() normally returns before the other application has
    >>> finished execution, unless foo() is forced to wait for the result.
    >>> This can for example be achieved by using foo()'s return value (foo()
    >>> uses self-inspection to see if its return value is discarded or not).
    >>> I also want foo() to wait in case it's in a try block, so that the
    >>> user can catch exceptions that occur in the other application.

    >> Is there any reason you can't just add a parameter (e.g. 'wait') to
    >> foo() to tell it whether to wait for the exception or not? It's
    >> certainly less magical than detecting `try` in the caller.
    >>

    >
    > The system is used by people who don't know about these technical
    > details, and the goal is to hide the complexity from the user, without
    > having to explain when to add a 'wait' parameter etc.
    > Anyway, thanks for your time. I'll dig something out...


    They can't be bothered with parameters (or a second function, name
    "foo_wait" that passes the wait-parameter to the first one), but they
    know about exception handling? Interesting people...

    Diez
     
    Diez B. Roggisch, Mar 20, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    612
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    612
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    420
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    396
    falcon
    Jul 31, 2005
  5. morrell
    Replies:
    1
    Views:
    990
    roy axenov
    Oct 10, 2006
Loading...

Share This Page