]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Drop string-bytes comparisons
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 13 Nov 2021 14:05:19 +0000 (15:05 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 13 Nov 2021 15:31:21 +0000 (16:31 +0100)
They were mostly internal ones, coming from dicts containing strings and
bytes as keys.

Close #147

docs/news.rst
psycopg/psycopg/_encodings.py
psycopg/psycopg/_queries.py
psycopg/psycopg/types/range.py
tests/pq/test_pgconn.py
tests/test_conninfo.py
tests/types/test_composite.py

index 23dfe8cc2c94b3af6108c4a8fbfcf7b2b3e80751..6324662aa27ec5219e55b840bbccd9102b830247 100644 (file)
 Current release
 ---------------
 
+Psycopg 3.0.4
+^^^^^^^^^^^^^
+
+- Allow to use the module with strict strings comparison (:ticket:`#147`).
+
+
 Psycopg 3.0.3
 ^^^^^^^^^^^^^
 
index 0d917466a4ee36e1df1bc98d338633fd00678dfc..83f3e116c1229f71e5b0317c911f213297bd7954 100644 (file)
@@ -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}")
index a974d599cfc6422e78e745974116725e95de51f7..eeef76137531862a07ce1b198a432caa45f4dca9 100644 (file)
@@ -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] = []
index bf670b3f1d66d33541e9849cf98029bc8b2dd06d..918fa0f27cb937a68b16e8ba12684f4659a5983f 100644 (file)
@@ -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)
 
index 294fed67188e283594bbf5f633e4b1e05bfe4096..7b2e75e02fac8cf0c664f4a03a6cb2edc578a02c 100644 (file)
@@ -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
index 4d098d24d052107e49c263c28a7455304cd756a1..834e6fb470cc01006e0c44be2aab87ae55a77a37 100644 (file)
@@ -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",
index 06a27052b45bfd8906e826624461b712ce2b9954..93ba6f44dc27ab5688227dae566246b0d795458e 100644 (file)
@@ -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"