Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Python 3, cursor.query returns bytes instead of str

#197

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)'

Reported by bernardy · March 8th, 2014 @ 01:40 AM

State: resolved
Milestone: psycopg2 2.5.3
Assigned to: nobody

Activity

  1. bernardy
    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.

    March 8th, 2014 @ 02:00 AM

  2. bernardy
    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.

    March 8th, 2014 @ 02:59 AM

  3. Daniele Varrazzo
    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() 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.

    March 8th, 2014 @ 10:56 AM

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

    March 10th, 2014 @ 05:25 PM

  5. Daniele Varrazzo
  6. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from open to resolved

    April 3rd, 2014 @ 01:50 AM