psycopg.git / summary / log / commit / refs
commit 7bcc2d100b2aa44104f29517821002b100227887
Author: Daniele Varrazzo <daniele.varrazzo@gmail.com>
Date: Sat Nov 13 14:05:19 2021 +0000
Drop string-bytes comparisons
They were mostly internal ones, coming from dicts containing strings and
bytes as keys.
Close #147
docs/news.rst | 6 ++++++
psycopg/psycopg/_encodings.py | 8 +++-----
psycopg/psycopg/_queries.py | 25 +++++++++----------------
psycopg/psycopg/types/range.py | 2 +-
tests/pq/test_pgconn.py | 6 +-----
tests/test_conninfo.py | 2 +-
tests/types/test_composite.py | 1 -
7 files changed, 21 insertions(+), 29 deletions(-)
diff --git a/docs/news.rst b/docs/news.rst
index 23dfe8cc..6324662a 100644
--- a/docs/news.rst
+++ b/docs/news.rst
@@ -10,6 +10,12 @@
Current release
---------------
+Psycopg 3.0.4
+^^^^^^^^^^^^^
+
+- Allow to use the module with strict strings comparison (:ticket:`#147`).
+
+
Psycopg 3.0.3
^^^^^^^^^^^^^
diff --git a/psycopg/psycopg/_encodings.py b/psycopg/psycopg/_encodings.py
index 0d917466..83f3e116 100644
--- a/psycopg/psycopg/_encodings.py
+++ b/psycopg/psycopg/_encodings.py
@@ -60,7 +60,6 @@ _py_codecs = {
}
py_codecs: Dict[Union[bytes, str], str] = {}
-py_codecs.update((k, v) for k, v in _py_codecs.items())
py_codecs.update((k.encode(), v) for k, v in _py_codecs.items())
pg_codecs = {v: k.encode() for k, v in _py_codecs.items()}
@@ -79,7 +78,7 @@ def py2pgenc(name: str) -> bytes:
return pg_codecs[codecs.lookup(name).name]
-def pg2pyenc(name: Union[bytes, str]) -> str:
+def pg2pyenc(name: bytes) -> str:
"""Convert a Python encoding name to PostgreSQL encoding name.
Raise NotSupportedError if the PostgreSQL encoding is not supported by
@@ -88,6 +87,5 @@ def pg2pyenc(name: Union[bytes, str]) -> str:
try:
return py_codecs[name]
except KeyError:
- if isinstance(name, bytes):
- name = name.decode("utf8", "replace")
- raise NotSupportedError(f"codec not available in Python: {name!r}")
+ sname = name.decode("utf8", "replace")
+ raise NotSupportedError(f"codec not available in Python: {sname!r}")
diff --git a/psycopg/psycopg/_queries.py b/psycopg/psycopg/_queries.py
index a974d599..eeef7613 100644
--- a/psycopg/psycopg/_queries.py
+++ b/psycopg/psycopg/_queries.py
@@ -63,8 +63,12 @@ class PostgresQuery:
The results of this function can be obtained accessing the object
attributes (`query`, `params`, `types`, `formats`).
"""
- if isinstance(query, Composable):
- query = query.as_bytes(self._tx)
+ if isinstance(query, str):
+ bquery = query.encode(self._encoding)
+ elif isinstance(query, Composable):
+ bquery = query.as_bytes(self._tx)
+ else:
+ bquery = query
if vars is not None:
(
@@ -72,11 +76,9 @@ class PostgresQuery:
self._want_formats,
self._order,
self._parts,
- ) = _query2pg(query, self._encoding)
+ ) = _query2pg(bquery, self._encoding)
else:
- if isinstance(query, str):
- query = query.encode(self._encoding)
- self.query = query
+ self.query = bquery
self._want_formats = self._order = None
self.dump(vars)
@@ -103,7 +105,7 @@ class PostgresQuery:
@lru_cache()
def _query2pg(
- query: Union[bytes, str], encoding: str
+ query: bytes, encoding: str
) -> Tuple[bytes, List[PyFormat], Optional[List[str]], List[QueryPart]]:
"""
Convert Python query and params into something Postgres understands.
@@ -115,15 +117,6 @@ def _query2pg(
(sequence of names used in the query, in the position they appear)
``parts`` (splits of queries and placeholders).
"""
- if isinstance(query, str):
- query = query.encode(encoding)
- if not isinstance(query, bytes):
- # encoding from str already happened
- raise TypeError(
- f"the query should be str or bytes,"
- f" got {type(query).__name__} instead"
- )
-
parts = _split_query(query, encoding)
order: Optional[List[str]] = None
chunks: List[bytes] = []
diff --git a/psycopg/psycopg/types/range.py b/psycopg/psycopg/types/range.py
index bf670b3f..918fa0f2 100644
--- a/psycopg/psycopg/types/range.py
+++ b/psycopg/psycopg/types/range.py
@@ -455,7 +455,7 @@ def load_range_text(
if item is None:
item = m.group(4)
if item is not None:
- upper = load(_re_undouble.sub(r"\1", item))
+ upper = load(_re_undouble.sub(rb"\1", item))
else:
upper = load(item)
diff --git a/tests/pq/test_pgconn.py b/tests/pq/test_pgconn.py
index 294fed67..7b2e75e0 100644
--- a/tests/pq/test_pgconn.py
+++ b/tests/pq/test_pgconn.py
@@ -352,11 +352,7 @@ def test_used_password(pgconn, dsn, monkeypatch):
or [i for i in info if i.keyword == b"password"][0].val is not None
)
if has_password:
- # The assumption that the password is needed is broken on the Travis
- # PG 10 setup so let's skip that
- print("\n".join(map(str, sorted(os.environ.items()))))
- if not (os.environ.get("TRAVIS") and os.environ.get("PGVER") == "10"):
- assert pgconn.used_password
+ assert pgconn.used_password
pgconn.finish()
pgconn.used_password
diff --git a/tests/test_conninfo.py b/tests/test_conninfo.py
index 4d098d24..834e6fb4 100644
--- a/tests/test_conninfo.py
+++ b/tests/test_conninfo.py
@@ -251,7 +251,7 @@ class TestConnectionInfo:
def test_encoding(self, conn):
enc = conn.execute("show client_encoding").fetchone()[0]
- assert conn.info.encoding == pg2pyenc(enc)
+ assert conn.info.encoding == pg2pyenc(enc.encode())
@pytest.mark.parametrize(
"enc, out, codec",
diff --git a/tests/types/test_composite.py b/tests/types/test_composite.py
index 06a27052..93ba6f44 100644
--- a/tests/types/test_composite.py
+++ b/tests/types/test_composite.py
@@ -79,7 +79,6 @@ def test_dump_builtin_empty_range(conn, fmt_in):
f"select pg_typeof(%{fmt_in})",
[info.python_type(10, Range(empty=True), [])],
)
- print(cur._query.params[0])
assert cur.fetchone()[0] == "tmptype"
[parent: 298033bd3d86]