-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-120317: Lock around global state in the tokenize module #120318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
900dd6a
abf568a
9dfe174
5fd07b6
814d9af
c1093be
117d256
975f01a
ca466fa
3abb8ab
601e539
6290f19
0ddde2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import io | ||
import time | ||
import unittest | ||
import tokenize | ||
from functools import partial | ||
from threading import Thread | ||
|
||
from test.support import threading_helper | ||
|
||
|
||
@threading_helper.requires_working_threading() | ||
class TestTokenize(unittest.TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested this locally: it crashes spectacularly without the fix :) |
||
def test_tokenizer_iter(self): | ||
source = io.StringIO("for _ in a:\n pass") | ||
it = tokenize._tokenize.TokenizerIter(source.readline, extra_tokens=False) | ||
|
||
tokens = [] | ||
def next_token(it): | ||
while True: | ||
try: | ||
r = next(it) | ||
tokens.append(tokenize.TokenInfo._make(r)) | ||
time.sleep(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this test faster? It's going to run on every CI job. I think we should aim for 0.1 seconds or less for these sorts of tests. (It's currently ~3 seconds). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed the amount of time each thread sleeps to 0.03 secs, which means that the whole test now takes about 0.1 seconds. Also verified that the test still fails without the fix and succeeds with it. |
||
except StopIteration: | ||
return | ||
|
||
threads = [] | ||
for _ in range(5): | ||
threads.append(Thread(target=partial(next_token, it))) | ||
|
||
for thread in threads: | ||
thread.start() | ||
|
||
for thread in threads: | ||
thread.join() | ||
|
||
expected_tokens = [ | ||
tokenize.TokenInfo(type=1, string='for', start=(1, 0), end=(1, 3), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=1, string='_', start=(1, 4), end=(1, 5), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=1, string='in', start=(1, 6), end=(1, 8), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=1, string='a', start=(1, 9), end=(1, 10), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=11, string=':', start=(1, 10), end=(1, 11), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=4, string='', start=(1, 11), end=(1, 11), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=5, string='', start=(2, -1), end=(2, -1), line=' pass'), | ||
tokenize.TokenInfo(type=1, string='pass', start=(2, 2), end=(2, 6), line=' pass'), | ||
tokenize.TokenInfo(type=4, string='', start=(2, 6), end=(2, 6), line=' pass'), | ||
tokenize.TokenInfo(type=6, string='', start=(2, -1), end=(2, -1), line=' pass'), | ||
tokenize.TokenInfo(type=0, string='', start=(2, -1), end=(2, -1), line=' pass'), | ||
] | ||
|
||
tokens.sort() | ||
expected_tokens.sort() | ||
self.assertListEqual(tokens, expected_tokens) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Uh oh!
There was an error while loading. Please reload this page.