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

DSN parsing overly sensitive to string type #679

Closed
stub42 opened this Issue Feb 20, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@stub42

stub42 commented Feb 20, 2018

parse_dsn() insists that the connection string is a str or bytes, and does not accept subclasses or objects defining a str method. I think this is a backwards incompatible change from the 2.6 series. I have a ConnectionString subclass of str that should be usable as a stringallows access to the individual components and provides conversion methods to other formats. Code using ConnectionString works with 2.6.1 but is failing with 2.7.4.

    con = psycopg2.connect(ConnectionString(**c))
  File "/usr/lib/python3/dist-packages/psycopg2/__init__.py", line 129, in connect
    dsn = _ext.make_dsn(dsn, **kwargs)
  File "/usr/lib/python3/dist-packages/psycopg2/extensions.py", line 155, in make_dsn
    parse_dsn(dsn)
TypeError: Expected bytes or unicode string, got ConnectionString instead

With 2.6.1:

$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.6.1 (dt dec pq3 ext lo64)'
>>> class C(str): pass
... 
>>> c = C('')
>>> con = psycopg2.connect(c)
>>> 

With 2.7.4:

Python 3.6.4+ (default, Feb 12 2018, 08:25:03) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.7.4 (dt dec pq3 ext lo64)'
>>> class C(str): pass
... 
>>> c = C('')
>>> con = psycopg2.connect(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/psycopg2/__init__.py", line 129, in connect
    dsn = _ext.make_dsn(dsn, **kwargs)
  File "/usr/lib/python3/dist-packages/psycopg2/extensions.py", line 155, in make_dsn
    parse_dsn(dsn)
TypeError: Expected bytes or unicode string, got C instead
>>> 
@dvarrazzo

This comment has been minimized.

Member

dvarrazzo commented Feb 20, 2018

mmm... I guess it is psycopg_ensure_bytes the function to be excessively strict. It uses PyUnicode_CheckExact() and similar functions. I think it can relaxed to use the *_Check() functions instead.

@dvarrazzo dvarrazzo added this to the psycopg 2.7.5 milestone Feb 20, 2018

dvarrazzo added a commit that referenced this issue Feb 20, 2018

@dvarrazzo dvarrazzo closed this in ea923b6 Feb 21, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment