Skip to content

parse_js_objects actually can raise ValueError #54

@Nykakin

Description

@Nykakin

The documentation states that parse_js_objects doesn't raise. This isn't necessarily true, as I've encountered some botched inputs that can raise ValueError:

>>> list(chompjs.parse_js_objects("\0"))
Traceback (most recent call last):
    ...
ValueError: embedded null character

UnicodeEncodeError can be raised on some inputs:

>>> next(chompjs.parse_js_objects('\ud83f'))
Traceback (most recent call last):
    ...
UnicodeEncodeError: 'utf-8' codec can't encode character '\ud83f' in position 0: surrogates not allowed

UnicodeEncodeError can be sometimes raised when unicode_escape=True is True:

>>> list(chompjs.parse_js_objects(chr(92), unicode_escape=False))
[]
>>> list(chompjs.parse_js_objects(chr(92), unicode_escape=True))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mariusz/Documents/Projekty/chompjs/chompjs/chompjs.py", line 127, in parse_js_objects
    string = _preprocess(string, unicode_escape)
  File "/home/mariusz/Documents/Projekty/chompjs/chompjs/chompjs.py", line 10, in _preprocess
    string = string.encode().decode('unicode_escape')
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 0: \ at end of string

Finally, trying to run:

$ python -c "import chompjs; next(chompjs.parse_js_objects('[{]'), None)"

This botched input sometimes raises UnicodeDecodeError, sometimes not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions