Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to use self-inspection to check for try-block

Reply
Thread Tools

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

 
 
elmar@cmbi.ru.nl
Guest
Posts: n/a
 
      03-20-2009
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
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      03-20-2009
On Fri, Mar 20, 2009 at 1:32 AM, <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
elmar@cmbi.ru.nl
Guest
Posts: n/a
 
      03-20-2009
On Mar 20, 9:44 am, Chris Rebert <(E-Mail Removed)> wrote:
> On Fri, Mar 20, 2009 at 1:32 AM, <(E-Mail Removed)> 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

 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      03-20-2009
On Fri, Mar 20, 2009 at 2:09 AM, <(E-Mail Removed)> wrote:
> On Mar 20, 9:44 am, Chris Rebert <(E-Mail Removed)> wrote:
>> On Fri, Mar 20, 2009 at 1:32 AM, *<(E-Mail Removed)> 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
 
Reply With Quote
 
elmar@cmbi.ru.nl
Guest
Posts: n/a
 
      03-20-2009
On Mar 20, 10:16 am, Chris Rebert <(E-Mail Removed)> wrote:
> On Fri, Mar 20, 2009 at 2:09 AM, <(E-Mail Removed)> wrote:
> > On Mar 20, 9:44 am, Chris Rebert <(E-Mail Removed)> wrote:
> >> On Fri, Mar 20, 2009 at 1:32 AM, <(E-Mail Removed)> 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


 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-20-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) schrieb:
> On Mar 20, 10:16 am, Chris Rebert <(E-Mail Removed)> wrote:
>> On Fri, Mar 20, 2009 at 2:09 AM, <(E-Mail Removed)> wrote:
>>> On Mar 20, 9:44 am, Chris Rebert <(E-Mail Removed)> wrote:
>>>> On Fri, Mar 20, 2009 at 1:32 AM, <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to selecet check box in the data grid ?? only one check box mit ASP .Net 1 01-25-2006 06:47 PM
how to check all check boxes in repeater control kris ASP .Net 0 09-22-2005 06:29 PM
how to check all check boxes in repeater control kris ASP .Net 0 09-22-2005 06:29 PM
.Net Pro include Visual Source Safe or other Code Check in Check out software? Davisro ASP .Net 1 06-14-2004 03:23 PM



Advertisments