This project is archived and is in readonly mode.
Python 3, cursor.query returns bytes instead of str
-
bernardy
Although, thinking about it, fixing this bug could break a lot of extent user code, since the typical way to handle it would be:
cursor.query.decode('utf-8')which would generate an AttributeError exception if cursor.query suddenly changed from bytes to str.
So you might just want to declare this as a documentation bug, regardless of original intent.
-
bernardy
Also just noticed that cursor.mogrify() returns bytes in Python 3. Which is okay because cursor.execute() will happily take either (utf-8 or current client encoding?) bytes or strings and still work.
I think what I'd really like is an improvement in the documentation where those psycopg2 interfaces that expect or return bytes instead of string in Python 3 are noted as such.
In any case, thank you for your development work on psycopg2.
-
Daniele Varrazzo
- State changed from new to open
- Tag set to documentation
Hello, thank you for the report.
The library is doing what intended:
mogrify()andqueryrepresent the statement to be sent/just sent to the server, hence it is always bytes even if the query was unicode, both in Py2 and Py3. Note that the encoding used is often but not always utf8: it is the connectionencoding.As you say this may need to be better explained in the doc, thank you.
-
Daniele Varrazzo
- Milestone set to psycopg2 2.5.3
-
Daniele Varrazzo
Docs for
mogrify()andqueryfixed: http://initd.org/psycopg/docs/cursor.html#cursor.mogrify -
Daniele Varrazzo
- State changed from open to resolved