Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

CSS file of 320 bytes takes 10+ seconds to parse

未关闭
#18 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
performance

调研方向

从链接的可复现示例开始,跟踪报告的路径经过 cssutils/parse.py、cssutils/css/cssstylesheet.py、util.py 和 tokenize2.py。改变换行符和样式表达式,对示例进行基准测试,然后添加一个回归测试,表明该输入不再具有指数级的解析时间。

由索引模型根据 Issue 内容生成。

描述

help wanted

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 (most recent call last):
  File "cssutils_infinite_loop_bug_example.py", line 10, in <module>
    sheet = css_parser.parseString(cssText=css_text)
  File "/home/vdmit/.local/lib/python3.8/site-packages/cssutils/parse.py", line 147, in parseString
    sheet._setCssTextWithEncodingOverride(
  File "/home/vdmit/.local/lib/python3.8/site-packages/cssutils/css/cssstylesheet.py", line 408, in _setCssTextWithEncodingOverride
    self.cssText = cssText
  File "/home/vdmit/.local/lib/python3.8/site-packages/cssutils/css/cssstylesheet.py", line 331, in _setCssText
    wellformed, expected = self._parse(
  File "/home/vdmit/.local/lib/python3.8/site-packages/cssutils/util.py", line 484, in _parse
    expected = p(expected, seq, token, tokenizer)
  File "/home/vdmit/.local/lib/python3.8/site-packages/cssutils/css/cssstylesheet.py", line 313, in ruleset
    rule.cssText = self._tokensupto2(tokenizer, token)
  File "/home/vdmit/.local/lib/python3.8/site-packages/cssutils/util.py", line 343, in _tokensupto2
    for token in tokenizer:
  File "/home/vdmit/.local/lib/python3.8/site-packages/cssutils/tokenize2.py", line 172, in tokenize
    match = matcher(text, pos)  # if no match try next production
KeyboardInterrupt
主要语言
Python
星标
92
派生
30
PR 合并指标
30 天内没有已合并 PR

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

jaraco/cssutils 的其他 Issue

查看 jaraco/cssutils 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。