URLValidator raising sre_constants.error

36 views
Skip to first unread message

rectang...@gmail.com

unread,
Mar 4, 2014, 7:13:12 PM3/4/14
to django...@googlegroups.com
When entering two backslashes into the URL validator it raises an `sre_constants.error`. As per the docs, it seems like the validator should catch this and then chain a `ValidationError` to the exception. This appears to be an issue with Django's integration with Python's regex implementation. This may just be my misunderstanding of the way Django handles validation, but it seems like any form of invalid input should raise a `ValidationError`.

I found this while running Django 1.6.2 on Python 3.3.0 on Windows 7.

This session reproduces the bug on my system.

>>> from django.core.validators import URLValidator
>>> URLValidator('\\')
Traceback (most recent call last):
  File "C:\Python33\lib\sre_parse.py", line 195, in __next
    c = self.string[self.index + 1]
IndexError: string index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
  File "C:\Python33\lib\site-packages\django\core\validators.py", line 32, in __init__
    self.regex = re.compile(self.regex)
  File "C:\Python33\lib\re.py", line 214, in compile
    return _compile(pattern, flags)
  File "C:\Python33\lib\functools.py", line 258, in wrapper
    result = user_function(*args, **kwds)
  File "C:\Python33\lib\re.py", line 274, in _compile
    return sre_compile.compile(pattern, flags)
  File "C:\Python33\lib\sre_compile.py", line 493, in compile
    p = sre_parse.parse(p, flags)
  File "C:\Python33\lib\sre_parse.py", line 717, in parse
    source = Tokenizer(str)
  File "C:\Python33\lib\sre_parse.py", line 183, in __init__
    self.__next()
  File "C:\Python33\lib\sre_parse.py", line 197, in __next
    raise error("bogus escape (end of line)")
sre_constants.error: bogus escape (end of line)

rectang...@gmail.com

unread,
Mar 4, 2014, 7:24:52 PM3/4/14
to django...@googlegroups.com
I believe I may have found the issue. I was using the validator incorrectly. I was passing the string to `__init__` like so `URLValidator('\\')`, instead of passing it to the `__call__` method like this `URLValidator()('\\')`.
Reply all
Reply to author
Forward
0 new messages