chtd / psycopg2cffi

Port to cffi with some speed improvements
Other
177 stars 43 forks source link

Python 3 type caster is throwing an error for string casting. #33

Open amjith opened 9 years ago

amjith commented 9 years ago
>>> import psycopg2cffi
>>> c = psycopg2cffi.connect(database='postgres')
>>> cur = c.cursor()
>>> cur.execute('create table binarydata(c bytea)')
>>> psycopg2cffi.extensions.register_type(psycopg2cffi.extensions.new_type((17,), 'BYTEA_TEXT', psycopg
2cffi.STRING))
>>> cur.execute("insert into binarydata (c) values (decode('DEADBEEF', 'hex'))")
>>> cur.execute('select * FROM binarydata')
>>> cur.fetchall()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/amjith/.virtualenvs/py3_pgcli/lib/python3.3/site-packages/psycopg2cffi/_impl/cursor.py",
 line 30, in check_closed_
    return func(self, *args, **kwargs)
  File "/Users/amjith/.virtualenvs/py3_pgcli/lib/python3.3/site-packages/psycopg2cffi/_impl/cursor.py",
 line 43, in check_no_tuples_
    return func(self, *args, **kwargs)
  File "/Users/amjith/.virtualenvs/py3_pgcli/lib/python3.3/site-packages/psycopg2cffi/_impl/cursor.py",
 line 377, in fetchall
    return [self._build_row() for _ in xrange(size)]
  File "/Users/amjith/.virtualenvs/py3_pgcli/lib/python3.3/site-packages/psycopg2cffi/_impl/cursor.py",
 line 377, in <listcomp>
    return [self._build_row() for _ in xrange(size)]
  File "/Users/amjith/.virtualenvs/py3_pgcli/lib/python3.3/site-packages/psycopg2cffi/_impl/cursor.py",
 line 891, in _build_row
    self._casts[i], val, length, self)
  File "/Users/amjith/.virtualenvs/py3_pgcli/lib/python3.3/site-packages/psycopg2cffi/_impl/typecasts.p
y", line 70, in typecast
    return caster.cast(value, cursor, length)
  File "/Users/amjith/.virtualenvs/py3_pgcli/lib/python3.3/site-packages/psycopg2cffi/_impl/typecasts.p
y", line 37, in cast
    return self.py_caster(value, cursor)
  File "/Users/amjith/.virtualenvs/py3_pgcli/lib/python3.3/site-packages/psycopg2cffi/_impl/typecasts.p
y", line 38, in cast
    return self.caster(value, length, cursor)
  File "/Users/amjith/.virtualenvs/py3_pgcli/lib/python3.3/site-packages/psycopg2cffi/_impl/typecasts.p
y", line 88, in parse_string
    if value is not None else None
AttributeError: 'str' object has no attribute 'decode'
>>>

The error is triggered from https://github.com/chtd/psycopg2cffi/blob/master/psycopg2cffi/_impl/typecasts.py#L87 but I'm not sure what's the correct fix here. I presume there won't be a change between PY2 and PY3 for strings, but that seems wrong, somehow.

lopuhin commented 9 years ago

At first sight looks very similar to #32 - will look in to it on the weekend, I hope.