From 3a703f7f06d81ffa708cb986a470d171f65e4ed5 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 29 Aug 2022 02:10:27 +0100 Subject: [PATCH] fix: load bytea as bytes, not memoryview, in Python implementation --- docs/news.rst | 1 + psycopg/psycopg/types/string.py | 2 +- tests/types/test_string.py | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/news.rst b/docs/news.rst index d6a9cca1a..ac4d40e64 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -42,6 +42,7 @@ Psycopg 3.0.17 (unreleased) - Fix segfaults on fork on some Linux systems using `ctypes` implementation (:ticket:`#300`). +- Load bytea as bytes, not memoryview, using `ctypes` implementation. Current release diff --git a/psycopg/psycopg/types/string.py b/psycopg/psycopg/types/string.py index 593055df5..5cd0d0e07 100644 --- a/psycopg/psycopg/types/string.py +++ b/psycopg/psycopg/types/string.py @@ -148,7 +148,7 @@ class ByteaLoader(Loader): self.__class__._escaping = Escaping() def load(self, data: Buffer) -> bytes: - return self._escaping.unescape_bytea(data) + return bytes(self._escaping.unescape_bytea(data)) class ByteaBinaryLoader(Loader): diff --git a/tests/types/test_string.py b/tests/types/test_string.py index 0722fcc89..8499959e1 100644 --- a/tests/types/test_string.py +++ b/tests/types/test_string.py @@ -283,8 +283,10 @@ def test_load_1byte(conn, fmt_out): cur = conn.cursor(binary=fmt_out) for i in range(0, 256): cur.execute("select set_byte('x', 0, %s)", (i,)) - assert cur.fetchone()[0] == bytes([i]) + val = cur.fetchone()[0] + assert val == bytes([i]) + assert isinstance(val, bytes) assert cur.pgresult.fformat(0) == fmt_out -- 2.47.2