1 #! /usr/bin/env python3 2 3 """Keywords (from "graminit.c") 4 5 This file is automatically generated; please don't muck it up! 6 7 To update the symbols in this file, 'cd' to the top directory of 8 the python source tree after building the interpreter and run: 9 10 ./python Lib/keyword.py 11 """ 12 13 __all__ = ["iskeyword", "kwlist"] 14 15 kwlist = [ 16 #--start keywords-- 17 'False', 18 'None', 19 'True', 20 'and', 21 'as', 22 'assert', 23 'async', 24 'await', 25 'break', 26 'class', 27 'continue', 28 'def', 29 'del', 30 'elif', 31 'else', 32 'except', 33 'finally', 34 'for', 35 'from', 36 'global', 37 'if', 38 'import', 39 'in', 40 'is', 41 'lambda', 42 'nonlocal', 43 'not', 44 'or', 45 'pass', 46 'raise', 47 'return', 48 'try', 49 'while', 50 'with', 51 'yield', 52 #--end keywords-- 53 ] 54 55 iskeyword = frozenset(kwlist).__contains__ 56 57 def main(): 58 import sys, re 59 60 args = sys.argv[1:] 61 iptfile = args and args[0] or "Python/graminit.c" 62 if len(args) > 1: optfile = args[1] 63 else: optfile = "Lib/keyword.py" 64 65 # load the output skeleton from the target, taking care to preserve its 66 # newline convention. 67 with open(optfile, newline='') as fp: 68 format = fp.readlines() 69 nl = format[0][len(format[0].strip()):] if format else '\n' 70 71 # scan the source file for keywords 72 with open(iptfile) as fp: 73 strprog = re.compile('"([^"]+)"') 74 lines = [] 75 for line in fp: 76 if '{1, "' in line: 77 match = strprog.search(line) 78 if match: 79 lines.append(" '" + match.group(1) + "'," + nl) 80 lines.sort() 81 82 # insert the lines of keywords into the skeleton 83 try: 84 start = format.index("#--start keywords--" + nl) + 1 85 end = format.index("#--end keywords--" + nl) 86 format[start:end] = lines 87 except ValueError: 88 sys.stderr.write("target does not contain format markers\n") 89 sys.exit(1) 90 91 # write the output file 92 with open(optfile, 'w', newline='') as fp: 93 fp.writelines(format) 94 95 if __name__ == "__main__": 96 main() 97