Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
import token

class TokenTest(unittest.TestCase):
def test_terminal_validations(self):
# Regression test for gh-142968
self.assertRaises(TypeError, token.ISTERMINAL, 0.5)
self.assertRaises(TypeError, token.ISNONTERMINAL, 0.5)
self.assertRaises(TypeError, token.ISTERMINAL, "string")
self.assertRaises(TypeError, token.ISNONTERMINAL, "string")

# (sanity check)
self.assertIs(token.ISTERMINAL(1), True)
self.assertIs(token.ISNONTERMINAL(1), False)

if __name__ == "__main__":
unittest.main()
4 changes: 4 additions & 0 deletions Lib/token.py

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
@@ -0,0 +1 @@
Add type checks to :func:`token.ISTERMINAL` and :func:`token.ISNONTERMINAL`.
4 changes: 4 additions & 0 deletions Tools/build/generate_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,13 @@ def make_rst(infile, outfile='Doc/library/token-list.inc',
}

def ISTERMINAL(x: int) -> bool:
if not isinstance(x,int):
raise TypeError(f"expected integer, got {type(x).__name__}")
return x < NT_OFFSET

def ISNONTERMINAL(x: int) -> bool:
if not isinstance(x,int):
raise TypeError(f"expected integer, got {type(x).__name__}")
return x >= NT_OFFSET

def ISEOF(x: int) -> bool:
Expand Down
Loading