This project is archived and is in readonly mode.

#197 ✓resolved
bernardy

Python 3, cursor.query returns bytes instead of str

Reported by bernardy | March 8th, 2014 @ 01:40 AM | in psycopg2 2.5.3 (closed)

In python 3.3.4, cursor.query returns a bytes sequence, not a string.
Interestingly, cursor.statusmessage returns a string.

If this is intentional behavior on Python 3, I couldn't find mention of it in the documentation (i.e. a documentation bug)
If it's not, here's a bug report.

Python 3.3.4 (default, Feb 24 2014, 00:49:03) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> cnn=psycopg2.connect(database='public')
>>> cur=cnn.cursor()
>>> cur.execute('select 1')
>>> cur.query
b'select 1'
>>> cur.statusmessage
'SELECT 1'
>>> psycopg2.__version__
'2.5.2 (dt dec pq3 ext)'

Comments and changes to this ticket

  • bernardy

    bernardy March 8th, 2014 @ 02:00 AM

    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

    bernardy March 8th, 2014 @ 02:59 AM

    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

    Daniele Varrazzo March 8th, 2014 @ 10:56 AM

    • State changed from “new” to “open”
    • Tag set to documentation

    Hello, thank you for the report.

    The library is doing what intended: mogrify() and query represent 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 connection encoding.

    As you say this may need to be better explained in the doc, thank you.

  • Daniele Varrazzo

    Daniele Varrazzo March 10th, 2014 @ 05:25 PM

    • Milestone set to psycopg2 2.5.3
  • Daniele Varrazzo
  • Daniele Varrazzo

    Daniele Varrazzo April 3rd, 2014 @ 01:50 AM

    • State changed from “open” to “resolved”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

<b>WARNING:</b> the informations in this tracker are archived. Please submit new tickets or comments to <a href="https://github.com/psycopg/psycopg2/issues">the new tracker</a>.
<br/>
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

Shared Ticket Bins

Pages