From 4ff39a79f6b6d53c32d5f0e5f0a2ded1e0582a13 Mon Sep 17 00:00:00 2001 From: "chandr-andr (Kiselev Aleksandr)" Date: Thu, 17 Oct 2024 23:51:57 +0200 Subject: [PATCH] Return actual bytes instead of list of ints for BYTEA type Signed-off-by: chandr-andr (Kiselev Aleksandr) --- Cargo.lock | 2 +- Cargo.toml | 2 +- python/tests/test_value_converter.py | 4 ++-- src/value_converter.rs | 13 +++++++++---- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d8ffb51..e7d49cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -988,7 +988,7 @@ dependencies = [ [[package]] name = "psqlpy" -version = "0.8.2" +version = "0.8.3" dependencies = [ "byteorder", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 87e0b19..f381d77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "psqlpy" -version = "0.8.2" +version = "0.8.3" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/python/tests/test_value_converter.py b/python/tests/test_value_converter.py index a3e166e..585d30b 100644 --- a/python/tests/test_value_converter.py +++ b/python/tests/test_value_converter.py @@ -103,7 +103,7 @@ async def test_as_class( @pytest.mark.parametrize( ["postgres_type", "py_value", "expected_deserialized"], ( - ("BYTEA", b"Bytes", [66, 121, 116, 101, 115]), + ("BYTEA", b"Bytes", b"Bytes"), ("VARCHAR", "Some String", "Some String"), ("TEXT", "Some String", "Some String"), ( @@ -829,7 +829,7 @@ class ValidateModelForInnerValueType(BaseModel): some_enum: TestEnum class ValidateModelForCustomType(BaseModel): - bytea_: List[int] + bytea_: bytes varchar_: str text_: str bool_: bool diff --git a/src/value_converter.rs b/src/value_converter.rs index 93edb29..bf7bc75 100644 --- a/src/value_converter.rs +++ b/src/value_converter.rs @@ -1314,10 +1314,15 @@ fn postgres_bytes_to_py( match *type_ { // ---------- Bytes Types ---------- // Convert BYTEA type into Vector, then into PyBytes - Type::BYTEA => Ok(_composite_field_postgres_to_py::>>( - type_, buf, is_simple, - )? - .to_object(py)), + Type::BYTEA => { + let vec_of_bytes = _composite_field_postgres_to_py::>>( + type_, buf, is_simple, + )?; + if let Some(vec_of_bytes) = vec_of_bytes { + return Ok(PyBytes::new_bound(py, &vec_of_bytes).to_object(py)); + } + Ok(py.None()) + }, // // ---------- String Types ---------- // // Convert TEXT and VARCHAR type into String, then into str Type::TEXT | Type::VARCHAR | Type::XML => Ok(_composite_field_postgres_to_py::>(