def __init__(self, dsn, **kwargs): self.dsn = dsn self.status = consts.STATUS_SETUP self._encoding = None self._py_enc = None self._closed = 0 self._cancel = ffi.NULL self._typecasts = {} self._tpc_xid = None self._notifies = [] self._autocommit = False self._pgconn = None self._equote = False self._lock = threading.RLock() self.notices = [] self.cursor_factory = None # The number of commits/rollbacks done so far self._mark = 0 if 'async' in kwargs: self._async = kwargs.pop('async') elif 'async_' in kwargs: self._async = kwargs.pop('async_') else: self._async = False items = [] items.extend([(k, v) for (k, v) in kwargs.items() if v is not None]) if items: raise TypeError("'%s' is an invalid keyword argument" % items[0][0]) self._async_status = consts.ASYNC_DONE self._async_cursor = None self_ref = weakref.ref(self) self._notice_callback = ffi.callback( 'void(void *, const char *)', lambda arg, message: self_ref()._process_notice( arg, ffi.string(message).decode(self_ref()._py_enc or 'utf-8', 'replace'))) if not self._async: self._connect_sync() else: self._connect_async()
self._pgconn = None self._equote = False self._lock = threading.RLock() self.notices = [] self.cursor_factory = None # The number of commits/rollbacks done so far self._mark = 0 self._async = async self._async_status = consts.ASYNC_DONE self._async_cursor = None self_ref = weakref.ref(self) self._notice_callback = ffi.callback( 'void(void *, const char *)', lambda arg, message: self_ref()._process_notice( arg, bytes_to_ascii(ffi.string(message)))) if not self._async: self._connect_sync() else: self._connect_async() def _connect_sync(self): self._pgconn = libpq.PQconnectdb(self.dsn.encode('utf-8')) if not self._pgconn: raise exceptions.OperationalError('PQconnectdb() failed') elif libpq.PQstatus(self._pgconn) == libpq.CONNECTION_BAD: raise self._create_exception() # Register notice processor
self._equote = False self._lock = threading.RLock() self.notices = [] self.cursor_factory = None # The number of commits/rollbacks done so far self._mark = 0 self._async = async self._async_status = consts.ASYNC_DONE self._async_cursor = None self_ref = weakref.ref(self) self._notice_callback = ffi.callback( 'void(void *, const char *)', lambda arg, message: self_ref()._process_notice( arg, ffi.string(message).decode(self_ref()._py_enc or 'utf-8', 'replace'))) if not self._async: self._connect_sync() else: self._connect_async() def _connect_sync(self): self._pgconn = libpq.PQconnectdb(self.dsn.encode('utf-8')) if not self._pgconn: raise exceptions.OperationalError('PQconnectdb() failed') elif libpq.PQstatus(self._pgconn) == libpq.CONNECTION_BAD: raise self._create_exception()