You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Trailing whitespace makes tokenize.generate_tokens pathological #60356
When tokenizing with tokenize.generate_tokens, if the code ends with whitespace (no newline), the tokenizer produces an ERRORTOKEN for each space. Additionally, the regex that fails to find tokens in those spaces is linear in the number of spaces, so the overall performance is O(n**2).
I found this while tokenizing code samples uploaded to a public web site. One sample for some reason ended with 40,000 spaces, which was taking two hours to tokenize.
Demonstration:
{{{
import token
import tokenize
try:
from cStringIO import StringIO
except:
from io import StringIO
New changeset eb7ea51e658e by Ezio Melotti in branch '2.7': bpo-16152: fix tokenize to ignore whitespace at the end of the code when no newline is found. Patch by Ned Batchelder. http://hg.python.org/cpython/rev/eb7ea51e658e
New changeset 3ffff1798ed5 by Ezio Melotti in branch '3.2': bpo-16152: fix tokenize to ignore whitespace at the end of the code when no newline is found. Patch by Ned Batchelder. http://hg.python.org/cpython/rev/3ffff1798ed5
Activity
nedbat commentedon Oct 6, 2012
When tokenizing with tokenize.generate_tokens, if the code ends with whitespace (no newline), the tokenizer produces an ERRORTOKEN for each space. Additionally, the regex that fails to find tokens in those spaces is linear in the number of spaces, so the overall performance is O(n**2).
I found this while tokenizing code samples uploaded to a public web site. One sample for some reason ended with 40,000 spaces, which was taking two hours to tokenize.
Demonstration:
{{{
import token
import tokenize
try:
from cStringIO import StringIO
except:
from io import StringIO
code = "@"+" "*10000
code_reader = StringIO(code).readline
nedbat commentedon Oct 6, 2012
Here's a patch for 3.3.
I would like to also fix 2.7...
nedbat commentedon Oct 7, 2012
Updated with new (better) patch, for v2.7 and v3.3. They are the same except for the test.
jcea commentedon Oct 10, 2012
Ned, could you possibly send a Contributor Form Agreement? http://www.python.org/psf/contrib/
nedbat commentedon Oct 10, 2012
Jesús, done!
python-dev commentedon Nov 3, 2012
New changeset eb7ea51e658e by Ezio Melotti in branch '2.7':
bpo-16152: fix tokenize to ignore whitespace at the end of the code when no newline is found. Patch by Ned Batchelder.
http://hg.python.org/cpython/rev/eb7ea51e658e
New changeset 3ffff1798ed5 by Ezio Melotti in branch '3.2':
bpo-16152: fix tokenize to ignore whitespace at the end of the code when no newline is found. Patch by Ned Batchelder.
http://hg.python.org/cpython/rev/3ffff1798ed5
New changeset 1fdeddabddda by Ezio Melotti in branch '3.3':
bpo-16152: merge with 3.2.
http://hg.python.org/cpython/rev/1fdeddabddda
New changeset ed091424f230 by Ezio Melotti in branch 'default':
bpo-16152: merge with 3.3.
http://hg.python.org/cpython/rev/ed091424f230
ezio-melotti commentedon Nov 3, 2012
Fixed, thanks for the report and the patch!
nedbat commentedon Apr 23, 2018
PR 6273 is mentioned, but I think 6573 is the correct number.