Changeset 880 for trunk/pg.py


Ignore:
Timestamp:
Jul 23, 2016, 9:21:57 AM (3 years ago)
Author:
cito
Message:

Enable garbage collection after deleting DB instance

Needed to add destructor and weak references since desctructors
are not called when there are circular references.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pg.py

    r879 r880  
    3636import select
    3737import warnings
     38import weakref
    3839
    3940from datetime import date, time, datetime, timedelta, tzinfo
     
    322323
    323324    def __init__(self, db):
    324         self.db = db
    325         self.encode_json = db.encode_json
    326         db = db.db
    327         self.escape_bytea = db.escape_bytea
    328         self.escape_string = db.escape_string
     325        self.db = weakref.proxy(db)
    329326
    330327    @classmethod
     
    357354    def _adapt_bytea(self, v):
    358355        """Adapt a bytea parameter."""
    359         return self.escape_bytea(v)
     356        return self.db.escape_bytea(v)
    360357
    361358    def _adapt_json(self, v):
     
    365362        if isinstance(v, basestring):
    366363            return v
    367         return self.encode_json(v)
     364        return self.db.encode_json(v)
    368365
    369366    @classmethod
     
    418415        if v is None:
    419416            return b'null'
    420         return self.escape_bytea(v).replace(b'\\', b'\\\\')
     417        return self.db.escape_bytea(v).replace(b'\\', b'\\\\')
    421418
    422419    def _adapt_json_array(self, v):
     
    428425            return 'null'
    429426        if not isinstance(v, basestring):
    430             v = self.encode_json(v)
     427            v = self.db.encode_json(v)
    431428        if self._re_array_quote.search(v):
    432429            v = '"%s"' % self._re_array_escape.sub(r'\\\1', v)
     
    550547            return value
    551548        if isinstance(value, Bytea):
    552             value = self.escape_bytea(value)
     549            value = self.db.escape_bytea(value)
    553550            if bytes is not str:  # Python >= 3.0
    554551                value = value.decode('ascii')
     
    556553            if value.encode:
    557554                return value.encode()
    558             value = self.encode_json(value)
     555            value = self.db.encode_json(value)
    559556        elif isinstance(value, (datetime, date, time, timedelta)):
    560557            value = str(value)
    561558        if isinstance(value, basestring):
    562             value = self.escape_string(value)
     559            value = self.db.escape_string(value)
    563560            return "'%s'" % value
    564561        if isinstance(value, bool):
     
    10861083        """Initialize type cache for connection."""
    10871084        super(DbTypes, self).__init__()
     1085        self._db = weakref.proxy(db)
    10881086        self._regtypes = False
    1089         self._get_attnames = db.get_attnames
    10901087        self._typecasts = Typecasts()
    10911088        self._typecasts.get_attnames = self.get_attnames
    1092         self._typecasts.connection = db
    1093         db = db.db
    1094         self.query = db.query
    1095         self.escape_string = db.escape_string
     1089        self._typecasts.connection = self._db
    10961090        if db.server_version < 80400:
    10971091            # older remote databases (not officially supported)
     
    11281122        try:
    11291123            q = self._query_pg_type % (_quote_if_unqualified('$1', key),)
    1130             res = self.query(q, (key,)).getresult()
     1124            res = self._db.query(q, (key,)).getresult()
    11311125        except ProgrammingError:
    11321126            res = None
     
    11531147        if not typ.relid:
    11541148            return None
    1155         return self._get_attnames(typ.relid, with_oid=False)
     1149        return self._db.get_attnames(typ.relid, with_oid=False)
    11561150
    11571151    def get_typecast(self, typ):
     
    14511445        else:
    14521446            self.rollback()
     1447
     1448    def __del__(self):
     1449        try:
     1450            db = self.db
     1451        except AttributeError:
     1452            db = None
     1453        if db:
     1454            db.set_cast_hook(None)
     1455            if self._closeable:
     1456                db.close()
    14531457
    14541458    # Auxiliary methods
Note: See TracChangeset for help on using the changeset viewer.