functions waiting for readiness, such as the ones defined in the
`selectors` module.
"""
- try:
- return self.pgconn.socket
- except pq.PQerror as exc:
- raise e.OperationalError(str(exc))
+ return self.pgconn.socket
def cancel(self) -> None:
"""Cancel the current operation on the connection."""
"""
try:
return pq.Conninfo.parse(conninfo.encode("utf8"))
- except pq.PQerror as ex:
+ except e.OperationalError as ex:
raise e.ProgrammingError(str(ex))
import logging
from typing import Callable, List, Type
-from .misc import ConninfoOption, PQerror, PGnotify, PGresAttDesc
+from .misc import ConninfoOption, PGnotify, PGresAttDesc
from .misc import error_message
from ._enums import ConnStatus, DiagnosticField, ExecStatus, Format
from ._enums import Ping, PollingStatus, TransactionStatus
"PGnotify",
"Conninfo",
"PGresAttDesc",
- "PQerror",
"error_message",
"ConninfoOption",
"version",
from typing import cast, NamedTuple, Optional, Union
-from ..errors import OperationalError
from ._enums import DiagnosticField, ConnStatus, TransactionStatus
from .proto import PGconn, PGresult
-class PQerror(OperationalError):
- __module__ = "psycopg3.pq"
-
-
class PGnotify(NamedTuple):
relname: bytes
be_pid: int
from typing import Any, Callable, List, Optional, Sequence, Tuple
from typing import cast as t_cast, TYPE_CHECKING
+from .. import errors as e
from . import _pq_ctypes as impl
-from .misc import PGnotify, ConninfoOption, PQerror, PGresAttDesc
+from .misc import PGnotify, ConninfoOption, PGresAttDesc
from .misc import error_message, connection_summary
from ._enums import Format, ExecStatus
res = PGresult(result_ptr)
try:
pgconn.notice_handler(res)
- except Exception as e:
- logger.exception("error in notice receiver: %s", e)
+ except Exception as exc:
+ logger.exception("error in notice receiver: %s", exc)
res.pgresult_ptr = None # avoid destroying the pgresult_ptr
def reset_start(self) -> None:
if not impl.PQresetStart(self.pgconn_ptr):
- raise PQerror("couldn't reset connection")
+ raise e.OperationalError("couldn't reset connection")
def reset_poll(self) -> int:
return self._call_int(impl.PQresetPoll)
raise TypeError(f"bytes expected, got {type(command)} instead")
self._ensure_pgconn()
if not impl.PQsendQuery(self.pgconn_ptr, command):
- raise PQerror(f"sending query failed: {error_message(self)}")
+ raise e.OperationalError(
+ f"sending query failed: {error_message(self)}"
+ )
def exec_params(
self,
)
self._ensure_pgconn()
if not impl.PQsendQueryParams(*args):
- raise PQerror(
+ raise e.OperationalError(
f"sending query and params failed: {error_message(self)}"
)
if not impl.PQsendPrepare(
self.pgconn_ptr, name, command, nparams, atypes
):
- raise PQerror(
+ raise e.OperationalError(
f"sending query and params failed: {error_message(self)}"
)
self._ensure_pgconn()
if not impl.PQsendQueryPrepared(*args):
- raise PQerror(
+ raise e.OperationalError(
f"sending prepared query failed: {error_message(self)}"
)
raise TypeError(f"bytes expected, got {type(name)} instead")
self._ensure_pgconn()
if not impl.PQsendDescribePrepared(self.pgconn_ptr, name):
- raise PQerror(
+ raise e.OperationalError(
f"sending describe prepared failed: {error_message(self)}"
)
raise TypeError(f"bytes expected, got {type(name)} instead")
self._ensure_pgconn()
if not impl.PQsendDescribePortal(self.pgconn_ptr, name):
- raise PQerror(
+ raise e.OperationalError(
f"sending describe portal failed: {error_message(self)}"
)
def consume_input(self) -> None:
if 1 != impl.PQconsumeInput(self.pgconn_ptr):
- raise PQerror(f"consuming input failed: {error_message(self)}")
+ raise e.OperationalError(
+ f"consuming input failed: {error_message(self)}"
+ )
def is_busy(self) -> int:
return impl.PQisBusy(self.pgconn_ptr)
@nonblocking.setter
def nonblocking(self, arg: int) -> None:
if 0 > impl.PQsetnonblocking(self.pgconn_ptr, arg):
- raise PQerror(f"setting nonblocking failed: {error_message(self)}")
+ raise e.OperationalError(
+ f"setting nonblocking failed: {error_message(self)}"
+ )
def flush(self) -> int:
if not self.pgconn_ptr:
- raise PQerror("flushing failed: the connection is closed")
+ raise e.OperationalError(
+ "flushing failed: the connection is closed"
+ )
rv: int = impl.PQflush(self.pgconn_ptr)
if rv < 0:
- raise PQerror(f"flushing failed: {error_message(self)}")
+ raise e.OperationalError(f"flushing failed: {error_message(self)}")
return rv
def set_single_row_mode(self) -> None:
if not impl.PQsetSingleRowMode(self.pgconn_ptr):
- raise PQerror("setting single row mode failed")
+ raise e.OperationalError("setting single row mode failed")
def get_cancel(self) -> "PGcancel":
"""
"""
rv = impl.PQgetCancel(self.pgconn_ptr)
if not rv:
- raise PQerror("couldn't create cancel object")
+ raise e.OperationalError("couldn't create cancel object")
return PGcancel(rv)
def notifies(self) -> Optional[PGnotify]:
buffer = bytes(buffer)
rv = impl.PQputCopyData(self.pgconn_ptr, buffer, len(buffer))
if rv < 0:
- raise PQerror(f"sending copy data failed: {error_message(self)}")
+ raise e.OperationalError(
+ f"sending copy data failed: {error_message(self)}"
+ )
return rv
def put_copy_end(self, error: Optional[bytes] = None) -> int:
rv = impl.PQputCopyEnd(self.pgconn_ptr, error)
if rv < 0:
- raise PQerror(f"sending copy end failed: {error_message(self)}")
+ raise e.OperationalError(
+ f"sending copy end failed: {error_message(self)}"
+ )
return rv
def get_copy_data(self, async_: int) -> Tuple[int, memoryview]:
buffer_ptr = c_char_p()
nbytes = impl.PQgetCopyData(self.pgconn_ptr, byref(buffer_ptr), async_)
if nbytes == -2:
- raise PQerror(f"receiving copy data failed: {error_message(self)}")
+ raise e.OperationalError(
+ f"receiving copy data failed: {error_message(self)}"
+ )
if buffer_ptr:
# TODO: do it without copy
data = string_at(buffer_ptr, nbytes)
Call one of the pgconn libpq functions returning a bytes pointer.
"""
if not self.pgconn_ptr:
- raise PQerror("the connection is closed")
+ raise e.OperationalError("the connection is closed")
rv = func(self.pgconn_ptr)
assert rv is not None
return rv
Call one of the pgconn libpq functions returning an int.
"""
if not self.pgconn_ptr:
- raise PQerror("the connection is closed")
+ raise e.OperationalError("the connection is closed")
return func(self.pgconn_ptr)
def _call_bool(self, func: Callable[[impl.PGconn_struct], int]) -> bool:
Call one of the pgconn libpq functions returning a logical value.
"""
if not self.pgconn_ptr:
- raise PQerror("the connection is closed")
+ raise e.OperationalError("the connection is closed")
return bool(func(self.pgconn_ptr))
def _ensure_pgconn(self) -> None:
if not self.pgconn_ptr:
- raise PQerror("the connection is closed")
+ raise e.OperationalError("the connection is closed")
class PGresult:
array = (impl.PGresAttDesc_struct * len(structs))(*structs) # type: ignore
rv = impl.PQsetResultAttrs(self.pgresult_ptr, len(structs), array)
if rv == 0:
- raise PQerror("PQsetResultAttrs failed")
+ raise e.OperationalError("PQsetResultAttrs failed")
class PGcancel:
self.pgcancel_ptr, pointer(buf), len(buf) # type: ignore
)
if not res:
- raise PQerror(
+ raise e.OperationalError(
f"cancel failed: {buf.value.decode('utf8', 'ignore')}"
)
if not errmsg:
raise MemoryError("couldn't allocate on conninfo parse")
else:
- exc = PQerror((errmsg.value or b"").decode("utf8", "replace"))
+ exc = e.OperationalError(
+ (errmsg.value or b"").decode("utf8", "replace")
+ )
impl.PQfreemem(errmsg)
raise exc
def escape_literal(self, data: "proto.Buffer") -> memoryview:
if not self.conn:
- raise PQerror("escape_literal failed: no connection provided")
+ raise e.OperationalError(
+ "escape_literal failed: no connection provided"
+ )
self.conn._ensure_pgconn()
# TODO: might be done without copy (however C does that)
data = bytes(data)
out = impl.PQescapeLiteral(self.conn.pgconn_ptr, data, len(data))
if not out:
- raise PQerror(
+ raise e.OperationalError(
f"escape_literal failed: {error_message(self.conn)} bytes"
)
rv = string_at(out)
def escape_identifier(self, data: "proto.Buffer") -> memoryview:
if not self.conn:
- raise PQerror("escape_identifier failed: no connection provided")
+ raise e.OperationalError(
+ "escape_identifier failed: no connection provided"
+ )
self.conn._ensure_pgconn()
data = bytes(data)
out = impl.PQescapeIdentifier(self.conn.pgconn_ptr, data, len(data))
if not out:
- raise PQerror(
+ raise e.OperationalError(
f"escape_identifier failed: {error_message(self.conn)} bytes"
)
rv = string_at(out)
)
if error:
- raise PQerror(
+ raise e.OperationalError(
f"escape_string failed: {error_message(self.conn)} bytes"
)
from typing import List
from psycopg3 import errors as e
-from psycopg3.pq import proto, error_message, PQerror
+from psycopg3.pq import proto, error_message
from psycopg3.proto import PQGen
from psycopg3.waiting import Wait, Ready
# PGconn buffer and passed to Python later.
cires = libpq.PQconsumeInput(pgconn_ptr)
if 1 != cires:
- raise PQerror(
+ raise e.OperationalError(
f"consuming input failed: {error_message(pgconn)}")
continue
ibres = libpq.PQisBusy(pgconn_ptr)
if 1 != cires:
- raise PQerror(
+ raise e.OperationalError(
f"consuming input failed: {error_message(pgconn)}")
if ibres:
yield WAIT_R
from psycopg3_c.pq cimport libpq
+from psycopg3 import errors as e
from psycopg3.pq import Format
-from psycopg3.pq.misc import PQerror, error_message
+from psycopg3.pq.misc import error_message
__impl__ = 'c'
if errmsg is NULL:
raise MemoryError("couldn't allocate on conninfo parse")
else:
- exc = PQerror(errmsg.decode("utf8", "replace"))
+ exc = e.OperationalError(errmsg.decode("utf8", "replace"))
libpq.PQfreemem(errmsg)
raise exc
cdef Py_ssize_t length
if self.conn is None:
- raise PQerror("escape_literal failed: no connection provided")
+ raise e.OperationalError("escape_literal failed: no connection provided")
if self.conn.pgconn_ptr is NULL:
- raise PQerror("the connection is closed")
+ raise e.OperationalError("the connection is closed")
_buffer_as_string_and_size(data, &ptr, &length)
out = libpq.PQescapeLiteral(self.conn.pgconn_ptr, ptr, length)
if out is NULL:
- raise PQerror(
+ raise e.OperationalError(
f"escape_literal failed: {error_message(self.conn)}"
)
_buffer_as_string_and_size(data, &ptr, &length)
if self.conn is None:
- raise PQerror("escape_identifier failed: no connection provided")
+ raise e.OperationalError("escape_identifier failed: no connection provided")
if self.conn.pgconn_ptr is NULL:
- raise PQerror("the connection is closed")
+ raise e.OperationalError("the connection is closed")
out = libpq.PQescapeIdentifier(self.conn.pgconn_ptr, ptr, length)
if out is NULL:
- raise PQerror(
+ raise e.OperationalError(
f"escape_identifier failed: {error_message(self.conn)}"
)
if self.conn is not None:
if self.conn.pgconn_ptr is NULL:
- raise PQerror("the connection is closed")
+ raise e.OperationalError("the connection is closed")
len_out = libpq.PQescapeStringConn(
self.conn.pgconn_ptr, PyByteArray_AS_STRING(rv),
ptr, length, &error
)
if error:
- raise PQerror(
+ raise e.OperationalError(
f"escape_string failed: {error_message(self.conn)}"
)
cdef Py_ssize_t length
if self.conn is not None and self.conn.pgconn_ptr is NULL:
- raise PQerror("the connection is closed")
+ raise e.OperationalError("the connection is closed")
_buffer_as_string_and_size(data, &ptr, &length)
# if a connection is passed in, it must be valid.
if self.conn is not None:
if self.conn.pgconn_ptr is NULL:
- raise PQerror("the connection is closed")
+ raise e.OperationalError("the connection is closed")
cdef size_t len_out
cdef unsigned char *out = libpq.PQunescapeBytea(data, &len_out)
cdef char buf[256]
cdef int res = libpq.PQcancel(self.pgcancel_ptr, buf, sizeof(buf))
if not res:
- raise PQerror(
+ raise e.OperationalError(
f"cancel failed: {buf.decode('utf8', 'ignore')}"
)
def reset_start(self) -> None:
if not libpq.PQresetStart(self.pgconn_ptr):
- raise PQerror("couldn't reset connection")
+ raise e.OperationalError("couldn't reset connection")
def reset_poll(self) -> int:
return _call_int(self, <conn_int_f>libpq.PQresetPoll)
def socket(self) -> int:
rv = _call_int(self, libpq.PQsocket)
if rv == -1:
- raise PQerror("the connection is lost")
+ raise e.OperationalError("the connection is lost")
return rv
@property
with nogil:
rv = libpq.PQsendQuery(self.pgconn_ptr, command)
if not rv:
- raise PQerror(f"sending query failed: {error_message(self)}")
+ raise e.OperationalError(f"sending query failed: {error_message(self)}")
def exec_params(
self,
<const char *const *>cvalues, clengths, cformats, result_format)
_clear_query_params(ctypes, cvalues, clengths, cformats)
if not rv:
- raise PQerror(
+ raise e.OperationalError(
f"sending query and params failed: {error_message(self)}"
)
)
PyMem_Free(atypes)
if not rv:
- raise PQerror(
+ raise e.OperationalError(
f"sending query and params failed: {error_message(self)}"
)
clengths, cformats, result_format)
_clear_query_params(ctypes, cvalues, clengths, cformats)
if not rv:
- raise PQerror(
+ raise e.OperationalError(
f"sending prepared query failed: {error_message(self)}"
)
_ensure_pgconn(self)
cdef int rv = libpq.PQsendDescribePrepared(self.pgconn_ptr, name)
if not rv:
- raise PQerror(
+ raise e.OperationalError(
f"sending describe prepared failed: {error_message(self)}"
)
_ensure_pgconn(self)
cdef int rv = libpq.PQsendDescribePortal(self.pgconn_ptr, name)
if not rv:
- raise PQerror(
+ raise e.OperationalError(
f"sending describe prepared failed: {error_message(self)}"
)
def consume_input(self) -> None:
if 1 != libpq.PQconsumeInput(self.pgconn_ptr):
- raise PQerror(f"consuming input failed: {error_message(self)}")
+ raise e.OperationalError(f"consuming input failed: {error_message(self)}")
def is_busy(self) -> int:
cdef int rv
@nonblocking.setter
def nonblocking(self, int arg) -> None:
if 0 > libpq.PQsetnonblocking(self.pgconn_ptr, arg):
- raise PQerror(f"setting nonblocking failed: {error_message(self)}")
+ raise e.OperationalError(f"setting nonblocking failed: {error_message(self)}")
def flush(self) -> int:
if self.pgconn_ptr == NULL:
- raise PQerror(f"flushing failed: the connection is closed")
+ raise e.OperationalError(f"flushing failed: the connection is closed")
cdef int rv = libpq.PQflush(self.pgconn_ptr)
if rv < 0:
- raise PQerror(f"flushing failed: {error_message(self)}")
+ raise e.OperationalError(f"flushing failed: {error_message(self)}")
return rv
def set_single_row_mode(self) -> None:
if not libpq.PQsetSingleRowMode(self.pgconn_ptr):
- raise PQerror("setting single row mode failed")
+ raise e.OperationalError("setting single row mode failed")
def get_cancel(self) -> PGcancel:
cdef libpq.PGcancel *ptr = libpq.PQgetCancel(self.pgconn_ptr)
if not ptr:
- raise PQerror("couldn't create cancel object")
+ raise e.OperationalError("couldn't create cancel object")
return PGcancel._from_ptr(ptr)
cpdef object notifies(self):
_buffer_as_string_and_size(buffer, &cbuffer, &length)
rv = libpq.PQputCopyData(self.pgconn_ptr, cbuffer, length)
if rv < 0:
- raise PQerror(f"sending copy data failed: {error_message(self)}")
+ raise e.OperationalError(f"sending copy data failed: {error_message(self)}")
return rv
def put_copy_end(self, error: Optional[bytes] = None) -> int:
cerr = PyBytes_AsString(error)
rv = libpq.PQputCopyEnd(self.pgconn_ptr, cerr)
if rv < 0:
- raise PQerror(f"sending copy end failed: {error_message(self)}")
+ raise e.OperationalError(f"sending copy end failed: {error_message(self)}")
return rv
def get_copy_data(self, int async_) -> Tuple[int, memoryview]:
cdef int nbytes
nbytes = libpq.PQgetCopyData(self.pgconn_ptr, &buffer_ptr, async_)
if nbytes == -2:
- raise PQerror(f"receiving copy data failed: {error_message(self)}")
+ raise e.OperationalError(f"receiving copy data failed: {error_message(self)}")
if buffer_ptr is not NULL:
data = PyMemoryView_FromObject(
PQBuffer._from_buffer(<unsigned char *>buffer_ptr, nbytes))
if pgconn.pgconn_ptr is not NULL:
return 1
- raise PQerror("the connection is closed")
+ raise e.OperationalError("the connection is closed")
cdef char *_call_bytes(PGconn pgconn, conn_bytes_f func) except NULL:
cdef int res = libpq.PQsetResultAttrs(self.pgresult_ptr, num, attrs)
PyMem_Free(attrs)
if (res == 0):
- raise PQerror("PQsetResultAttrs failed")
+ raise e.OperationalError("PQsetResultAttrs failed")
import pytest
+import psycopg3
from psycopg3 import pq
def test_conninfo_parse_bad():
- with pytest.raises(pq.PQerror) as e:
+ with pytest.raises(psycopg3.OperationalError) as e:
pq.Conninfo.parse(b"bad_conninfo=")
assert "bad_conninfo" in str(e.value)
import pytest
+import psycopg3
from psycopg3 import pq
sample_values = "values (10::int, 20::int, 'hello'::text), (40, NULL, 'world')"
def test_put_data_no_copy(pgconn):
- with pytest.raises(pq.PQerror):
+ with pytest.raises(psycopg3.OperationalError):
pgconn.put_copy_data(b"wat")
pgconn.finish()
- with pytest.raises(pq.PQerror):
+ with pytest.raises(psycopg3.OperationalError):
pgconn.put_copy_data(b"wat")
def test_put_end_no_copy(pgconn):
- with pytest.raises(pq.PQerror):
+ with pytest.raises(psycopg3.OperationalError):
pgconn.put_copy_end()
pgconn.finish()
- with pytest.raises(pq.PQerror):
+ with pytest.raises(psycopg3.OperationalError):
pgconn.put_copy_end()
def test_get_data_no_copy(pgconn):
- with pytest.raises(pq.PQerror):
+ with pytest.raises(psycopg3.OperationalError):
pgconn.get_copy_data(0)
pgconn.finish()
- with pytest.raises(pq.PQerror):
+ with pytest.raises(psycopg3.OperationalError):
pgconn.get_copy_data(0)
import pytest
+import psycopg3
from psycopg3 import pq
assert res.ftype(1) == 1700
assert res.ftype(2) == 25
- with pytest.raises(pq.PQerror):
+ with pytest.raises(psycopg3.OperationalError):
res.set_attributes(attrs)
def test_set_single_row_mode(pgconn):
- with pytest.raises(pq.PQerror):
+ with pytest.raises(psycopg3.OperationalError):
pgconn.set_single_row_mode()
pgconn.send_query(b"select 1")
cancel.cancel()
pgconn.finish()
cancel.cancel()
- with pytest.raises(pq.PQerror):
+ with pytest.raises(psycopg3.OperationalError):
pgconn.get_cancel()
def test_cancel_free(pgconn):
cancel = pgconn.get_cancel()
cancel.free()
- with pytest.raises(pq.PQerror):
+ with pytest.raises(psycopg3.OperationalError):
cancel.cancel()
cancel.free()