are variables local only to try/except blocks?

Barry Searle searle at ca.ibm.com
Tue Sep 20 19:41:38 EDT 2005


in the case of nested try/except code generation, 
your suggestion could cause a problem:
# 1) this is fine:
doInitialStuff  # With no exceptions
excp = 0
try:
     doLotsHere()
except aParticularSetOfExceptions:
     excp = 1
if excp:
     handleException()
doLotsMoreStuff()

# 2) nested code generation has problem:
doInitialStuff  # With no exceptions
excp = 0
try:

     doMOREInitialStuff  # With no exceptions
     excp = 0
     try:
          doMORELotsHere()
     except aParticularSetOfExceptions:
          excp = 1  #### this causes the problem
     if excp:
          handleException()
     doMORELotsMoreStuff()

     doLotsHere()
except aParticularSetOfExceptions:
     excp = 1
#### excp=1 from inner nested code generation
if excp:
     handleException()
doLotsMoreStuff()

# 3) Hence I return to my planned template code generation:
excp = 0 #### Probaly need to do ONCE, to "bind" variable ?
doInitialStuff  # With no exceptions
try:

     doMOREInitialStuff  # With no exceptions
     try:
          doMORELotsHere()
          excp = 0 ##### always reset excp as last action
     except aParticularSetOfExceptions:
          excp = 1
     if excp:
          handleException()
     doMORELotsMoreStuff()

     doLotsHere()
     excp = 0 ##### always reset excp as last action
except aParticularSetOfExceptions:
     excp = 1
if excp:  #### should be fine
     handleException()
doLotsMoreStuff()


Barry Searle,    searle at ca.ibm.com,  905-413-4020  (TL:969-4020)
Barry Searle/Toronto/IBM at IBMCA  (D3/639/8200/MKM)
"Architect, WebSphere Tools for WsAdmin Scripting and Automated Build "





Steve Holden <steve at holdenweb.com> 
20/09/2005 04:11 PM

To
Barry Searle/Toronto/IBM at IBMCA
cc
python-list at python.org
Subject
Re: are variables local only to try/except blocks?






I am taking the liberty of copying my response to your off-list reply 
back to the c.l.py community. (and I don't normally top-post except in 
politeness to other top-posters :-)

Seems to me you could avoid many of your problems by simply re-jigging 
your template to read

doInitialStuff  # With presumably no exceptions
excp = 0
try:
     doLotsHere()
except aParticularSetOfExceptions:
     excp = 1
if excp:
     handleException()
doLotsMoreStuff()

If you have been experiencing problems with "unbound local variables" 
this is almost certainly because, as you cast your template, there is 
the problem that if doLotsHere() raises an exception then the "excp" 
variable will not be bound.

The reason for this is that Python variables (technically names) are 
only "created" (technically, associated with a value) when they are 
bound by the execution of an assignment. If the exception is raised 
before the assignment then the variable still (technically) doesn't 
exist in the current namespace (which your average Pythonista might 
describe by saying that the name hasn't been bound).

There is *some* static analysis in Python to support name scoping, but 
names must be bound by assignment in order to be referencable (?) 
without causing an AttributeError or NameError exception. That's a 
fundamental part of the way Python works.

Hence my suggestion that you assign the zero before you do anything that 
might raise exceptions. You will also note I suggest you check for 
specific exceptions (you can check for more that one by using a tuple 
rather than a single exception), as otherwise you may very well end up 
treating unanticipated exceptions inappropriately and masking their 
occurrence.

regards
  Steve

Barry Searle wrote:
> 
> Sorry, the quick sample was just to illustrate the problem and solution 
> template.
> The generic template would be:
>   doInitialStuff()
>  try:
>     doLotsHere()
>     excp = 0
>  except:
>     excp = 1
>  #endTry
>  if (excp): doExceptionHandling()
>  doLotsMoreStuff()
> 
> The template will be used (is being used) for the automatic generation
> of a bunch of Jython code, and I wanted to be sure there were not scope
> issues (i.e. that excp was not only defined within the try/except 
blocks).
> 
> Barry Searle,    searle at ca.ibm.com,  905-413-4020  (TL:969-4020)
> Barry Searle/Toronto/IBM at IBMCA  (D3/639/8200/MKM)
> "Architect, WebSphere Tools for WsAdmin Scripting and Automated Build "
> 
> 
> 
> *Steve Holden <steve at holdenweb.com>*
> 
> 20/09/2005 11:55 AM
> 
> 
> To
> 
> cc
>                Barry Searle/Toronto/IBM at IBMCA
> Subject
>                Re: are variables local only to try/except blocks?
> 
> 
> 
> 
> 
> 
> 
> 
> BarrySearle wrote:
>  >  # Is this valid (or is excp local to try/except)?
>  >  try:
>  >     try:
>  >        doSomething1
>  >        excp = 0
> 
> This block is problematic because excp won;t be set if doSomething1
> raises an exception.
> 
>  >     except:
>  >        excp = 1
>  >     #endTry
>  >     if (_excp_): doSomething1 # is excp defined here?
> 
> Presumably you are expecting doSomething1 to fail or succeed in some
> non-deterministic way? Otherwise this will just raise the same exception
> again!
> 
>  >     excp = 0
>  >  except:
>  >     excp = 1
>  > #endTry
>  > if (excp): doSomething2 # is excp defined here?
>  >
>  >
>  >  # valid, but more verbose (and maybe redundant?)
>  >  excp = 0
>  >  try:
>  >     excp = 0
>  >     try:
>  >        doSomething1
>  >        excp = 0  # reset incase future inner block
>  >     except:
>  >        excp = 1
>  >     #endTry
>  >     if (_excp_): doSomething1
>  >     excp = 0  # reset incase inner block set excp=1
>  >  except:
>  >     excp = 1
>  > #endTry
>  > if (excp): doSomething2
>  >
>  > I am not so interested in what a particular version of the
>  > Python/Jython interpreter does, but rather what is "right".
>  >
>  > Pls "CC" replies to searle at ca.ibm.com (as well as newsgroup)
>  > Barry Searle,    searle at ca.ibm.com
>  >
> Your approach to exception handling is a little simplistic, resulting on
> code that reads about as well as a plate of spaghetti.
> 
> What are you actually trying to *do*? What problem do you need to solve?
> 
> regards
>  Steve
> -- 
> Steve Holden       +44 150 684 7255  +1 800 494 3119
> Holden Web LLC                     www.holdenweb.com
> PyCon TX 2006                          www.pycon.org
> 


-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                          www.pycon.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050920/2d00a9d2/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 5191 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20050920/2d00a9d2/attachment.bin>
-------------- next part --------------
-- 
http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 5191 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20050920/2d00a9d2/attachment-0001.bin>


More information about the Python-list mailing list