unable to catch this exception

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 2 20:56:36 EST 2010


En Tue, 02 Feb 2010 08:20:34 -0300, mk <mrkafk at gmail.com> escribió:

> Exception in thread Thread-9 (most likely raised during interpreter  
> shutdown):
> Traceback (most recent call last):
>    File "/var/www/html/cssh.py", line 617, in ssh_connect
> <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute  
> 'BadAuthenticationType'

So you started several threads, and one of them was in them middle of  
connecting to somewhere when the program exited, correct?

> 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.

As part of the interpreter shutdown procedure, module globals are set to  
None. Code that might be executed in those very precarious circumstances  
should NOT reference any globals.

> 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?

You could keep a local reference to those global names; by example, by  
adding 'fake' default arguments:

     def ssh_connect(self, other, arguments,
           paramiko=paramiko, socket=socket):
        ...

or perhaps:

     def ssh_connect(self, other, arguments):
        BadAuthenticationType = paramiko.BadAuthenticationType
        socket_timeout = socket.timeout
        try:
            ...
        except BadAuthenticationType, e:
            ...
        except socket_timeout, e:
            ...

-- 
Gabriel Genellina




More information about the Python-list mailing list