-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
gh-136757: Reuse static strings for exact operator tokens #151838
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1885,6 +1885,45 @@ def test_exact_type(self): | |
| token.NAME, token.AMPER, token.NUMBER, | ||
| token.RPAR) | ||
|
|
||
| def test_exact_operator_token_strings_are_reused(self): | ||
| operators = ( | ||
| '...', '->', '!=', '%=', '&=', '**', '**=', '*=', | ||
| '+=', '-=', '//', '//=', '/=', ':=', '<<', '<<=', | ||
| '<=', '==', '>=', '>>', '>>=', '@=', '^=', '|=', | ||
| ) | ||
| for op in operators: | ||
| with self.subTest(op=op): | ||
| source = f'{op} {op}\n'.encode() | ||
| tokens = list(tokenize.tokenize(BytesIO(source).readline)) | ||
| matches = [tok.string for tok in tokens if tok.string == op] | ||
| self.assertEqual(len(matches), 2) | ||
| self.assertIs(matches[0], matches[1]) | ||
|
|
||
| def test_exact_operator_token_strings_are_reused_by_c_tokenizer(self): | ||
| operators = ( | ||
| '...', '->', '!=', '%=', '&=', '**', '**=', '*=', | ||
| '+=', '-=', '//', '//=', '/=', ':=', '<<', '<<=', | ||
| '<=', '==', '>=', '>>', '>>=', '@=', '^=', '|=', | ||
| ) | ||
| for op in operators: | ||
| with self.subTest(op=op): | ||
| source = BytesIO(f'{op} {op}\n'.encode()) | ||
| tokens = list(tokenize._tokenize.TokenizerIter( | ||
| source.readline, encoding='utf-8', extra_tokens=True)) | ||
|
Comment on lines
+1911
to
+1912
Member
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. I'm not a fan of getting |
||
| matches = [tok[1] for tok in tokens if tok[1] == op] | ||
| self.assertEqual(len(matches), 2) | ||
| self.assertIs(matches[0], matches[1]) | ||
|
|
||
| def test_old_not_equal_spelling_is_not_rewritten(self): | ||
| source = BytesIO( | ||
| b'from __future__ import barry_as_FLUFL\n' | ||
| b'a <> b\n' | ||
| ) | ||
| tokens = list(tokenize._tokenize.TokenizerIter( | ||
| source.readline, encoding='utf-8', extra_tokens=True)) | ||
| self.assertIn('<>', [tok[1] for tok in tokens]) | ||
| self.assertNotIn('!=', [tok[1] for tok in tokens]) | ||
|
Comment on lines
+1917
to
+1925
Member
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. This test passes on |
||
|
|
||
| def test_pathological_trailing_whitespace(self): | ||
| # See http://bugs.python.org/issue16152 | ||
| self.assertExactTypeEqual('@ ', token.AT) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Reduce duplicate string allocations in :mod:`tokenize` by reusing static | ||
| strings for exact multi-character operator tokens. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
sys._is_internedinstead of relying onis.