New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backslash escaping should depend on connection parameters #562

Closed
cbay opened this Issue Jun 7, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@cbay
Copy link

cbay commented Jun 7, 2017

This is related to #394, which complains that backslash escaping changes after a connection is created. As explained, this is "due" to libpq, and this is pretty normal to me: until libpq has "seen" a connection, it has to use default values (for encoding and standard conforming strings). After a connection has been established, libpq can use its parameters rather than default ones.

However, the underlying issue remains when multiple connections are used simultaneously (which is a supported use case by psycopg2, as far as I know). Example:

>>> import psycopg2
>>> from psycopg2.extras import Json
>>> conn1 = psycopg2.connect(...) # server with standard_conforming_strings=on
>>> print Json({'a': '"'})
'{"a": "\""}'
>>> conn2 = psycopg2.connect(...) # server with standard_conforming_strings=off
>>> print Json({'a': '"'})
'{"a": "\\""}'

Since the Json object has no access to the connection, it ends up using PQescapeString rather than the correct PQescapeStringConn. So as I understand it, this is a limitation of psycopg2, not libpq. I guess Json could accept a connection object (probably optionally).

@dvarrazzo

This comment has been minimized.

Copy link
Member

dvarrazzo commented Jun 7, 2017

We should probably implement prepare() for the Json adapter.

@dvarrazzo dvarrazzo added this to the psycopg 2.7.2 milestone Jun 8, 2017

@dvarrazzo dvarrazzo closed this in 789eb64 Jun 16, 2017

@dvarrazzo

This comment has been minimized.

Copy link
Member

dvarrazzo commented Jun 16, 2017

@cbay From 2.7.2 you will be able to use the prepare() method (which is what used behind the scenes by psycopg to instruct the adapters about what connection they are used for:

>>> import psycopg2

>>> from psycopg2.extras import Json
>>> j = Json('"')

>>> cnn = psycopg2.connect("options='-c standard_conforming_strings=off'")
>>> j.prepare(cnn)
>>> print j
E'"\\""'

>>> cnn = psycopg2.connect("options='-c standard_conforming_strings=on'")
>>> j.prepare(cnn)
>>> print j
'"\""'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment