Skip to content

Commit d8a82e2

Browse files
msullivanmiss-islington
authored andcommitted
bpo-36878: Only allow text after # type: ignore if first character ASCII (GH-13504)
This disallows things like `# type: ignoreé`, which seems wrong. Also switch to using Py_ISALNUM for the alnum check, for consistency with other code (and maybe correctness re: locale issues?). https://bugs.python.org/issue36878
1 parent 0c2b6a3 commit d8a82e2

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

Lib/test/test_type_comments.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ def check_both_ways(source):
334334
check_both_ways("try: # type: int\n pass\nfinally:\n pass\n")
335335
check_both_ways("try:\n pass\nfinally: # type: int\n pass\n")
336336
check_both_ways("pass # type: ignorewhatever\n")
337+
check_both_ways("pass # type: ignoreé\n")
337338

338339
def test_func_type_input(self):
339340

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Only accept text after `# type: ignore` if the first character is ASCII.
2+
This is to disallow things like `# type: ignoreé`.

Parser/tokenizer.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,10 +1275,11 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
12751275
type_start = p;
12761276

12771277
/* A TYPE_IGNORE is "type: ignore" followed by the end of the token
1278-
* or anything non-alphanumeric. */
1278+
* or anything ASCII and non-alphanumeric. */
12791279
is_type_ignore = (
12801280
tok->cur >= ignore_end && memcmp(p, "ignore", 6) == 0
1281-
&& !(tok->cur > ignore_end && isalnum(p[6])));
1281+
&& !(tok->cur > ignore_end
1282+
&& ((unsigned char)ignore_end[0] >= 128 || Py_ISALNUM(ignore_end[0]))));
12821283

12831284
if (is_type_ignore) {
12841285
*p_start = (char *) ignore_end;

0 commit comments

Comments
 (0)