From: Daniele Varrazzo Date: Thu, 2 Apr 2020 02:29:52 +0000 (+1300) Subject: exceptions module renamed to errors X-Git-Tag: 3.0.dev0~631 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9b460963de1d7e42ac183fe80c62d5a79887abed;p=thirdparty%2Fpsycopg.git exceptions module renamed to errors To be part of the public interface, for compatibility with psycopg2 --- diff --git a/psycopg3/__init__.py b/psycopg3/__init__.py index 79573f3c5..839729fab 100644 --- a/psycopg3/__init__.py +++ b/psycopg3/__init__.py @@ -7,7 +7,7 @@ psycopg3 -- PostgreSQL database adapter for Python from .consts import VERSION as __version__ # noqa from .connection import AsyncConnection, Connection -from .exceptions import ( +from .errors import ( Warning, Error, InterfaceError, diff --git a/psycopg3/adapt.py b/psycopg3/adapt.py index 082f32ad7..b25b4894e 100644 --- a/psycopg3/adapt.py +++ b/psycopg3/adapt.py @@ -9,7 +9,7 @@ from typing import cast from typing import Any, Callable, Dict, Generator, List, Optional, Sequence from typing import Tuple, Union -from . import exceptions as exc +from . import errors as e from .pq import Format, PGresult from .cursor import BaseCursor from .types.oids import type_oid, INVALID_OID @@ -273,7 +273,7 @@ class Transformer: if key in Adapter.globals: return Adapter.globals[key] - raise exc.ProgrammingError( + raise e.ProgrammingError( f"cannot adapt type {cls} to format {Format(fmt).name}" ) diff --git a/psycopg3/connection.py b/psycopg3/connection.py index 161ff2ba7..0de2b9d50 100644 --- a/psycopg3/connection.py +++ b/psycopg3/connection.py @@ -12,7 +12,7 @@ from typing import Any, Generator, List, Optional, Tuple, Type, TypeVar from typing import cast, TYPE_CHECKING from . import pq -from . import exceptions as exc +from . import errors as e from . import cursor from .conninfo import make_conninfo from .waiting import wait, wait_async, Wait, Ready @@ -80,7 +80,7 @@ class BaseConnection: logger.debug("connection started, status %s", conn.status.name) while 1: if conn.status == pq.ConnStatus.BAD: - raise exc.OperationalError( + raise e.OperationalError( f"connection is bad: {pq.error_message(conn)}" ) @@ -93,11 +93,11 @@ class BaseConnection: elif status == pq.PollingStatus.WRITING: yield conn.socket, Wait.W elif status == pq.PollingStatus.FAILED: - raise exc.OperationalError( + raise e.OperationalError( f"connection failed: {pq.error_message(conn)}" ) else: - raise exc.InternalError(f"unexpected poll status: {status}") + raise e.InternalError(f"unexpected poll status: {status}") conn.nonblocking = 1 return conn @@ -196,7 +196,7 @@ class Connection(BaseConnection): self.pgconn.send_query(command) (pgres,) = self.wait(self._exec_gen(self.pgconn)) if pgres.status != pq.ExecStatus.COMMAND_OK: - raise exc.OperationalError( + raise e.OperationalError( f"error on {command.decode('utf8')}:" f" {pq.error_message(pgres)}" ) @@ -253,7 +253,7 @@ class AsyncConnection(BaseConnection): self.pgconn.send_query(command) (pgres,) = await self.wait(self._exec_gen(self.pgconn)) if pgres.status != pq.ExecStatus.COMMAND_OK: - raise exc.OperationalError( + raise e.OperationalError( f"error on {command.decode('utf8')}:" f" {pq.error_message(pgres)}" ) diff --git a/psycopg3/conninfo.py b/psycopg3/conninfo.py index 81308a2a2..0fdce34c4 100644 --- a/psycopg3/conninfo.py +++ b/psycopg3/conninfo.py @@ -2,7 +2,7 @@ import re from typing import Any, Dict, List from . import pq -from . import exceptions as exc +from . import errors as e def make_conninfo(conninfo: str = "", **kwargs: Any) -> str: @@ -62,8 +62,8 @@ def _parse_conninfo(conninfo: str) -> List[pq.ConninfoOption]: """ try: return pq.Conninfo.parse(conninfo.encode("utf8")) - except pq.PQerror as e: - raise exc.ProgrammingError(str(e)) + except pq.PQerror as ex: + raise e.ProgrammingError(str(ex)) re_escape = re.compile(r"([\\'])") diff --git a/psycopg3/cursor.py b/psycopg3/cursor.py index 931f96f15..9e360187b 100644 --- a/psycopg3/cursor.py +++ b/psycopg3/cursor.py @@ -6,7 +6,7 @@ psycopg3 cursor objects from typing import Any, List, Mapping, Optional, Sequence, Tuple, TYPE_CHECKING -from . import exceptions as exc +from . import errors as e from .pq import error_message, DiagnosticField, ExecStatus, PGresult, Format from .utils.queries import query2pg, reorder_params from .utils.typing import Query, Params @@ -80,7 +80,7 @@ class BaseCursor: def _execute_results(self, results: List[PGresult]) -> None: # Implement part of execute() after waiting common to sync and async if not results: - raise exc.InternalError("got no result from the query") + raise e.InternalError("got no result from the query") badstats = {res.status for res in results} - { ExecStatus.TUPLES_OK, @@ -93,7 +93,7 @@ class BaseCursor: return if results[-1].status == ExecStatus.FATAL_ERROR: - ecls = exc.class_for_state( + ecls = e.class_for_state( results[-1].error_field(DiagnosticField.SQLSTATE) ) raise ecls(error_message(results[-1])) @@ -103,11 +103,11 @@ class BaseCursor: ExecStatus.COPY_OUT, ExecStatus.COPY_BOTH, }: - raise exc.ProgrammingError( + raise e.ProgrammingError( "COPY cannot be used with execute(); use copy() insead" ) else: - raise exc.InternalError( + raise e.InternalError( f"got unexpected status from query:" f" {', '.join(sorted(s.name for s in sorted(badstats)))}" ) diff --git a/psycopg3/exceptions.py b/psycopg3/errors.py similarity index 100% rename from psycopg3/exceptions.py rename to psycopg3/errors.py diff --git a/psycopg3/pq/_pq_ctypes.py b/psycopg3/pq/_pq_ctypes.py index f3868ed54..c2debb5d5 100644 --- a/psycopg3/pq/_pq_ctypes.py +++ b/psycopg3/pq/_pq_ctypes.py @@ -10,7 +10,7 @@ from ctypes import Structure, POINTER from ctypes import c_char, c_char_p, c_int, c_uint, c_void_p from typing import List, Tuple -from psycopg3.exceptions import NotSupportedError +from psycopg3.errors import NotSupportedError libname = ctypes.util.find_library("pq") if libname is None: diff --git a/psycopg3/pq/pq_ctypes.py b/psycopg3/pq/pq_ctypes.py index 1b26f71fb..ee053fb00 100644 --- a/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/pq/pq_ctypes.py @@ -23,7 +23,7 @@ from .enums import ( ) from .misc import error_message, ConninfoOption from . import _pq_ctypes as impl -from ..exceptions import OperationalError +from ..errors import OperationalError from ..utils.typing import Oid diff --git a/psycopg3/utils/queries.py b/psycopg3/utils/queries.py index 6ca0b83ab..fb89383b9 100644 --- a/psycopg3/utils/queries.py +++ b/psycopg3/utils/queries.py @@ -9,7 +9,7 @@ from codecs import CodecInfo from typing import Any, Dict, List, Mapping, Match, NamedTuple, Optional from typing import Sequence, Tuple, Union -from .. import exceptions as exc +from .. import errors as e from ..pq import Format from .typing import Params @@ -41,7 +41,7 @@ def query2pg( if isinstance(vars, Sequence) and not isinstance(vars, (bytes, str)): if len(vars) != len(parts) - 1: - raise exc.ProgrammingError( + raise e.ProgrammingError( f"the query has {len(parts) - 1} placeholders but" f" {len(vars)} parameters were passed" ) @@ -74,7 +74,7 @@ def query2pg( chunks.append(ph) else: if seen[part.item][1] != part.format: - raise exc.ProgrammingError( + raise e.ProgrammingError( f"placeholder '{part.item}' cannot have" f" different formats" ) @@ -149,18 +149,18 @@ def split_query(query: bytes, encoding: str = "ascii") -> List[QueryPart]: continue if ph == b"%(": - raise exc.ProgrammingError( + raise e.ProgrammingError( f"incomplete placeholder:" f" '{query[m.span(0)[0]:].split()[0].decode(encoding)}'" ) elif ph == b"% ": # explicit messasge for a typical error - raise exc.ProgrammingError( + raise e.ProgrammingError( "incomplete placeholder: '%'; if you want to use '%' as an" " operator you can double it up, i.e. use '%%'" ) elif ph[-1:] not in b"bs": - raise exc.ProgrammingError( + raise e.ProgrammingError( f"only '%s' and '%b' placeholders allowed, got" f" {m.group(0).decode(encoding)}" ) @@ -173,7 +173,7 @@ def split_query(query: bytes, encoding: str = "ascii") -> List[QueryPart]: phtype = type(item) else: if phtype is not type(item): # noqa - raise exc.ProgrammingError( + raise e.ProgrammingError( "positional and named placeholders cannot be mixed" ) @@ -195,7 +195,7 @@ def reorder_params( try: return [params[item] for item in order] except KeyError: - raise exc.ProgrammingError( + raise e.ProgrammingError( f"query parameter missing:" f" {', '.join(sorted(i for i in order if i not in params))}" ) diff --git a/psycopg3/waiting.py b/psycopg3/waiting.py index 0128cb290..0f52d1a56 100644 --- a/psycopg3/waiting.py +++ b/psycopg3/waiting.py @@ -10,7 +10,7 @@ from typing import Generator, Optional, Tuple, TypeVar from asyncio import get_event_loop, Event from selectors import DefaultSelector, EVENT_READ, EVENT_WRITE -from . import exceptions as exc +from . import errors as e class Wait(IntEnum): @@ -54,8 +54,8 @@ def wait( assert len(ready) == 1 fd, s = gen.send(ready[0][1]) - except StopIteration as e: - rv: RV = e.args[0] + except StopIteration as ex: + rv: RV = ex.args[0] return rv @@ -99,9 +99,9 @@ async def wait_async(gen: Generator[Tuple[int, Wait], Ready, RV]) -> RV: loop.remove_reader(fd) loop.remove_writer(fd) else: - raise exc.InternalError("bad poll status: %s") + raise e.InternalError("bad poll status: %s") fd, s = gen.send(ready) - except StopIteration as e: - rv: RV = e.args[0] + except StopIteration as ex: + rv: RV = ex.args[0] return rv