unable to catch this exception

Chris Rebert clp2 at rebertia.com
Tue Feb 2 07:32:08 EST 2010


On Tue, Feb 2, 2010 at 3:20 AM, mk <mrkafk at gmail.com> wrote:
> Exception in thread Thread-9 (most likely raised during interpreter
> shutdown):
> Traceback (most recent call last):
>  File "/usr/local/lib/python2.6/threading.py", line 522, in
> __bootstrap_inner
>  File "/var/www/html/cssh.py", line 875, in run
>  File "/var/www/html/cssh.py", line 617, in ssh_connect
> <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute
> 'BadAuthenticationType'
>
>
> This happens on interpreter shutdown, even though I do try to catch the
> AttributeError exception:
>
> try:
>    fake = paramiko.BadAuthenticationType
>    try:
>        self.conobj.connect(self.ip, username=self.username,
> key_filename=self.sshprivkey, port=self.port, timeout=opts.timeout)
>        loginsuccess = True
>    except paramiko.BadAuthenticationType, e: # this is line 617
>        self.conerror = str(e)
>    except paramiko.SSHException, e:
>        self.conerror = str(e)
>    except socket.timeout, e:
>        self.conerror = str(e)
>    except socket.error, e:
>        self.conerror = str(e)
> except AttributeError:
>    # this happens on interpreter shutdown
>    self.conerror = 'shutdown'
>
>
> It's clear what happens: paramiko gets its attributes cleared or the module
> perhaps gets unloaded and as result "paramiko" label leads to None, which
> obviously has no attribute BadAuthenticationType.
>
> However, even though this is surrounded by try .. except AttributeError
> block, it evidently isn't catch. How to catch that exception? Or at least
> preven displaying this message?

Let's see if psychic debugging works...at 4 am...
Pure conjecture: Do you have something like the following elsewhere in
your code?:

try:
    #code
except SomeError, AttributeError:
    #code

For why this causes problems, consider:

except SomeError, e:
vs.
except SomeError, SomeOtherError:

Example:
try:
    raise IndexError
except IndexError, IOError:
    pass
print repr(IOError) #==> IndexError()

Hence, in your case, `AttributeError` may no longer refer to
AttributeError. You can check this by adding a `print
repr(AttributeError)` at the right place in your code.
I further conjecture that pyflakes/pychecker/etc. might issue a
warning about the offending bit of code.


"Third Option" solution: Since you take the same action no matter the
exception's type, have you considered simplifying your code to:

fake = paramiko.BadAuthenticationType
try:
    self.conobj.connect(self.ip, username=self.username,
key_filename=self.sshprivkey, port=self.port, timeout=opts.timeout)
    loginsuccess = True
except Exception, e:
    self.conerror = str(e)

Cheers,
Chris
--
http://tvtropes.org/pmwiki/pmwiki.php/Main/TakeAThirdOption
http://blog.rebertia.com



More information about the Python-list mailing list