On Wed, 7 Sep 2016 21:17:20 +0200
Christoph Zwerschke <[email protected]> wrote:
>  > Or, set it as a class variable before __init__ even gets called.
>  > For some reason I thought that we already did that.  
> 
> Another possible fix:
> 
>      def __getattr__(self, name):
>          if name != 'db' and self.db:
>              return getattr(self.db, name)
>          raise _int_error('Connection is not valid')
> 
> This would raise an InternalErrorwhen self.db is accessed, but not
> set.
> 
> In __del__ we could then check for that error:
> 
>      def __del__(self):
>          try:
>              db = self.db
>          except (AttributeError, InternalError):
>              db = None
>          if db:
>              db.set_cast_hook(None)
>              if self._closeable:
>                  db.close()

Seems like overkill.  A class variable effectively does all of that in
one line by making sure that it is always set.

class DB:
    """Wrapper class for the _pg connection type."""

    db = None #  <== added line
    def __init__(self, *args, **kw):
        """Create a new connection
etc.

-- 
D'Arcy J.M. Cain
PyGreSQL Development Group
http://www.PyGreSQL.org IM:[email protected]
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to