Thread: getquoted and unicode

getquoted and unicode

From
"Carl S. Yestrau Jr."
Date:
I'm currently using psycopg2.extensions.adapt without a connection
intentionally.

The following results in a: psycopg2.InterfaceError: can't encode
unicode string to latin-1

import psycopg2

psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
adapted = psycopg2.extensions.adapt(u'test')
adapted.getquoted()

Any tips greatly appreciated!

Re: getquoted and unicode

From
Karsten Hilbert
Date:
On Sat, Jan 22, 2011 at 11:53:23PM -0800, Carl S. Yestrau Jr. wrote:

> import psycopg2
>
> psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
> adapted = psycopg2.extensions.adapt(u'test')
> adapted.getquoted()

That is strange because:

    adapted.adapted.encode(adapted.encoding)

works just fine.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346

Re: getquoted and unicode

From
Karsten Hilbert
Date:
On Sun, Jan 23, 2011 at 12:42:42PM +0100, Karsten Hilbert wrote:

> > import psycopg2
> >
> > psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
> > adapted = psycopg2.extensions.adapt(u'test')
> > adapted.getquoted()
>
> That is strange because:
>
>     adapted.adapted.encode(adapted.encoding)
>
> works just fine.

And

    adapted.adapted being u'test'
    adapted.encoding being 'latin-1'

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346

Re: getquoted and unicode

From
Daniele Varrazzo
Date:
On Sun, Jan 23, 2011 at 7:53 AM, Carl S. Yestrau Jr.
<carl@featureblend.com> wrote:
> I'm currently using psycopg2.extensions.adapt without a connection
> intentionally.
>
> The following results in a: psycopg2.InterfaceError: can't encode
> unicode string to latin-1
>
> import psycopg2
>
> psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
> adapted = psycopg2.extensions.adapt(u'test')
> adapted.getquoted()

You are right, it should work, and it works indeed in the psycopg dev
version, but it looks broken in 2.3.2 and probably in previous
versions.

I can see that the glitch happens when the adapter is not "prepared"
for the connection: if you add that step it will work. Preparation
always happens when the parameters are passed to the query.

    >>>  cnn = psycopg2.connect('dbname=test')
    >>> adapted.prepare(cnn)
    >>> adapted.getquoted()
    "E'test'"

Preparation is probably not mentioned in the docs at all: this is
something else to review. If you are into writing an adapter, you may
want to take a look to SQL_IN or the hstore adapter for complete
examples.

If you need to work without the connection, do you want to try
checking out my dev branch? I've just pushed it on
<https://github.com/dvarrazzo/psycopg>.

Cheers,

-- Daniele

Re: getquoted and unicode

From
"Carl S. Yestrau Jr."
Date:
It does work, using this branch, thank you.

One observation, the string returned is of type str, not unicode.

Example:
import psycopg2
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
adapted = psycopg2.extensions.adapt(u'test')
print adapted.getquoted()
#<type 'str'>

On Sun, Jan 23, 2011 at 4:44 AM, Daniele Varrazzo
<daniele.varrazzo@gmail.com> wrote:
> On Sun, Jan 23, 2011 at 7:53 AM, Carl S. Yestrau Jr.
> <carl@featureblend.com> wrote:
>> I'm currently using psycopg2.extensions.adapt without a connection
>> intentionally.
>>
>> The following results in a: psycopg2.InterfaceError: can't encode
>> unicode string to latin-1
>>
>> import psycopg2
>>
>> psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
>> adapted = psycopg2.extensions.adapt(u'test')
>> adapted.getquoted()
>
> You are right, it should work, and it works indeed in the psycopg dev
> version, but it looks broken in 2.3.2 and probably in previous
> versions.
>
> I can see that the glitch happens when the adapter is not "prepared"
> for the connection: if you add that step it will work. Preparation
> always happens when the parameters are passed to the query.
>
>    >>>  cnn = psycopg2.connect('dbname=test')
>    >>> adapted.prepare(cnn)
>    >>> adapted.getquoted()
>    "E'test'"
>
> Preparation is probably not mentioned in the docs at all: this is
> something else to review. If you are into writing an adapter, you may
> want to take a look to SQL_IN or the hstore adapter for complete
> examples.
>
> If you need to work without the connection, do you want to try
> checking out my dev branch? I've just pushed it on
> <https://github.com/dvarrazzo/psycopg>.
>
> Cheers,
>
> -- Daniele
>

Re: getquoted and unicode

From
Daniele Varrazzo
Date:
On Sun, Jan 23, 2011 at 5:17 PM, Carl S. Yestrau Jr.
<carl@featureblend.com> wrote:
> It does work, using this branch, thank you.

This will likely be included in the next release.

> One observation, the string returned is of type str, not unicode.
>
> Example:
> import psycopg2
> psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
> adapted = psycopg2.extensions.adapt(u'test')
> print adapted.getquoted()
> #<type 'str'>

Yes, this is what expected. Adaptation is taking a Python object and
converting it into a SQL representation: this is always a bytes
string, as it has to be sent to the socket.

-- Daniele