I've encountered CSS file which cssutils library takes lots of seconds to parse (in fact, almost infinite).
After some investigation I've minified problematic sample (as possible) and prepared reproducible code example.
On my laptop (core i7-8550U) it takes 14 seconds to parse file with just one line and 320 bytes of text.
Original file has 20kBytes of text and parser does not finish it in several hours.
Code:
https://gist.github.com/vdmit/ef9007170fa1c616cf5aba1fcebfce87
I'm using:
- latest stable
cssutils release 2.3.0 from pypi repo - python 3.8.10
- Ubuntu 20.04 linux distribution.
Notes:
- original CSS file was malformed and even contained non-printable characters. My minified example is still not valid CSS file, but it is a plain ANSI file.
- Adding line break in any place fixes problem;
- Removing of last style expression (
.s11... reduces execution time from 14 seconds to 3 seconds). - Adding of one more style expression increases execution time from 14 seconds to 50, 2 more styles yields ~260 seconds.
So, there is a exponential complexity somewhere, which is strange.
Traceback of interrupted script is following:
Traceback (mostrecentcalllast):
File"cssutils_infinite_loop_bug_example.py", line10, in<module>sheet=css_parser.parseString(cssText=css_text)
File"/home/vdmit/.local/lib/python3.8/site-packages/cssutils/parse.py", line147, inparseStringsheet._setCssTextWithEncodingOverride(
File"/home/vdmit/.local/lib/python3.8/site-packages/cssutils/css/cssstylesheet.py", line408, in_setCssTextWithEncodingOverrideself.cssText=cssTextFile"/home/vdmit/.local/lib/python3.8/site-packages/cssutils/css/cssstylesheet.py", line331, in_setCssTextwellformed, expected=self._parse(
File"/home/vdmit/.local/lib/python3.8/site-packages/cssutils/util.py", line484, in_parseexpected=p(expected, seq, token, tokenizer)
File"/home/vdmit/.local/lib/python3.8/site-packages/cssutils/css/cssstylesheet.py", line313, inrulesetrule.cssText=self._tokensupto2(tokenizer, token)
File"/home/vdmit/.local/lib/python3.8/site-packages/cssutils/util.py", line343, in_tokensupto2fortokenintokenizer:
File"/home/vdmit/.local/lib/python3.8/site-packages/cssutils/tokenize2.py", line172, intokenizematch=matcher(text, pos) # if no match try next productionKeyboardInterrupt
I've encountered CSS file which
cssutilslibrary takes lots of seconds to parse (in fact, almost infinite).After some investigation I've minified problematic sample (as possible) and prepared reproducible code example.
On my laptop (core i7-8550U) it takes 14 seconds to parse file with just one line and 320 bytes of text.
Original file has 20kBytes of text and parser does not finish it in several hours.
Code:
https://gist.github.com/vdmit/ef9007170fa1c616cf5aba1fcebfce87
I'm using:
cssutilsrelease 2.3.0 from pypi repoNotes:
.s11...reduces execution time from 14 seconds to 3 seconds).So, there is a exponential complexity somewhere, which is strange.
Traceback of interrupted script is following: