Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in return type format on Intel and M1 Mac #561

Closed
dpage opened this issue May 11, 2023 · 4 comments
Closed

Inconsistency in return type format on Intel and M1 Mac #561

dpage opened this issue May 11, 2023 · 4 comments

Comments

@dpage
Copy link

dpage commented May 11, 2023

I recently ran into an issue when working on pgAdmin where a scalar query result was returned as a string on Intel and bytes on M1. Here's a simple test case:

M1:

Python 3.10.11 (v3.10.11:7d4cc5aa85, Apr  4 2023, 19:05:19) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg
>>> with psycopg.connect("dbname=postgres user=dpage") as conn:
...     with conn.cursor() as cur:
...             cur.execute("SELECT version()")
...             cur.fetchone()
... 
<psycopg.Cursor [TUPLES_OK] [INTRANS] (user=dpage database=postgres) at 0x103a72610>
(b'PostgreSQL 15.1 on aarch64-apple-darwin22.1.0, compiled by Apple clang version 14.0.0 (clang-1400.0.29.202), 64-bit',)

Intel:

Python 3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg
>>> with psycopg.connect("dbname=postgres user=dpage") as conn:
...     with conn.cursor() as cur:
...             cur.execute("SELECT version()")
...             cur.fetchone()
...
<psycopg.Cursor [TUPLES_OK] [INTRANS] (user=dpage database=postgres) at 0x10e6c7530>
('PostgreSQL 15.2 (Homebrew) on x86_64-apple-darwin22.3.0, compiled by Apple clang version 14.0.0 (clang-1400.0.29.202), 64-bit',)

It seems to me that this should be consistent.

dpage added a commit to dpage/pgadmin4 that referenced this issue May 11, 2023
@dlax
Copy link
Contributor

dlax commented May 11, 2023

Can you also share conn.info.encoding in both cases?

@dvarrazzo
Copy link
Member

Most definitely it should.

The only way psycopg returns bytes (I mean designed, as opposite as bugs) is that the client encoding of the connection is SQL_ASCII.

import psycopg
conn = psycopg.connect()
conn.execute("show client_encoding").fetchone()[0]
# 'UTF8'
conn.execute("set client_encoding to sql_ascii")
# <psycopg.Cursor [COMMAND_OK] [INTRANS] (database=piro) at 0x7fe76707e6c0>
conn.execute("show client_encoding").fetchone()[0]
# b'SQL_ASCII'

What does an execute("SHOW client_encoding").fetchone() from those connections show?

dpage added a commit to dpage/pgadmin4 that referenced this issue May 11, 2023
@dpage
Copy link
Author

dpage commented May 11, 2023

You've hit the nail on the head (as usual :-) ). My test server on Intel was initialised in UTF-8, but (for some unknown reason) SQL_ASCII on the M1 box.

Returning bytes for SQL_ASCII seems perfectly reasonable; I'll fix our code to handle that case.

Thanks!

@dvarrazzo
Copy link
Member

No worries :) For completeness, strings adaptation rules are explained here.

khushboovashi pushed a commit to pgadmin-org/pgadmin4 that referenced this issue May 16, 2023
* Handle cases when psycopg returns bytes instead of a string.

See psycopg/psycopg#561

* A more appropriate fix, suggested by Daniele Varrazzo

* Fix typo.

* Re-think this check. Our own driver effectively monkey-patches the psycopg encoding mappings.
akshay-joshi pushed a commit to akshay-joshi/pgadmin4 that referenced this issue May 19, 2023
* Handle cases when psycopg returns bytes instead of a string.

See psycopg/psycopg#561

* A more appropriate fix, suggested by Daniele Varrazzo

* Fix typo.

* Re-think this check. Our own driver effectively monkey-patches the psycopg encoding mappings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants