Jaymon / dsnparse

Easily parse DSN urls (connection strings) in Python
MIT License
21 stars 8 forks source link

Check fail with python3 -OO #4

Closed cochiseruhulessin closed 6 years ago

cochiseruhulessin commented 6 years ago

When running Python with the -OO optimizations, assert statements are ignored. This causes different behavior in the parse() function.

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dsnparse
>>> dsnparse.parse('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/dsnparse.py", line 33, in parse
    assert re.match("^\S+://\S+", dsn), "{} is invalid, only full dsn urls (scheme://host...) allowed".format(dsn)
AssertionError: a is invalid, only full dsn urls (scheme://host...) allowed
>>> 

With the -OO flags:

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dsnparse
>>> dsnparse.parse('a')
<dsnparse.ParseResult object at 0x101e3ea58>
>>> tuple(dsnparse.parse('a'))
('', 'None', 'a', '', {}, '')
>>> 
Jaymon commented 6 years ago

Sigh, using assert statements was a phase I went through that I'm still trying to recover from.

I think you might be using an older version...

$ python3 -OO
>>> import dsnparse
>>> dsnparse.parse('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jaymon/Projects/dsnparse/_dsnparse/dsnparse.py", line 289, in parse
    r = parse_class(dsn, **defaults)
  File "/Users/jaymon/Projects/dsnparse/_dsnparse/dsnparse.py", line 104, in __init__
    kwargs = self.parse(dsn, **defaults)
  File "/Users/jaymon/Projects/dsnparse/_dsnparse/dsnparse.py", line 42, in parse
    raise ValueError("{dsn} is invalid, only full dsn urls (scheme://host...) allowed".format(dsn=dsn))
ValueError: a is invalid, only full dsn urls (scheme://host...) allowed
>>>
>>> dsnparse.__version__
'0.1.11'

Upgrading your version should fix this as I refactored all the assert checks to use ValueError instead.