Home | History | Annotate | Download | only in Lib
      1 #! /usr/bin/env python
      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         'and',
     18         'as',
     19         'assert',
     20         'break',
     21         'class',
     22         'continue',
     23         'def',
     24         'del',
     25         'elif',
     26         'else',
     27         'except',
     28         'exec',
     29         'finally',
     30         'for',
     31         'from',
     32         'global',
     33         'if',
     34         'import',
     35         'in',
     36         'is',
     37         'lambda',
     38         'not',
     39         'or',
     40         'pass',
     41         'print',
     42         'raise',
     43         'return',
     44         'try',
     45         'while',
     46         'with',
     47         'yield',
     48 #--end keywords--
     49         ]
     50 
     51 iskeyword = frozenset(kwlist).__contains__
     52 
     53 def main():
     54     import sys, re
     55 
     56     args = sys.argv[1:]
     57     iptfile = args and args[0] or "Python/graminit.c"
     58     if len(args) > 1: optfile = args[1]
     59     else: optfile = "Lib/keyword.py"
     60 
     61     # scan the source file for keywords
     62     fp = open(iptfile)
     63     strprog = re.compile('"([^"]+)"')
     64     lines = []
     65     for line in fp:
     66         if '{1, "' in line:
     67             match = strprog.search(line)
     68             if match:
     69                 lines.append("        '" + match.group(1) + "',\n")
     70     fp.close()
     71     lines.sort()
     72 
     73     # load the output skeleton from the target
     74     fp = open(optfile)
     75     format = fp.readlines()
     76     fp.close()
     77 
     78     # insert the lines of keywords
     79     try:
     80         start = format.index("#--start keywords--\n") + 1
     81         end = format.index("#--end keywords--\n")
     82         format[start:end] = lines
     83     except ValueError:
     84         sys.stderr.write("target does not contain format markers\n")
     85         sys.exit(1)
     86 
     87     # write the output file
     88     fp = open(optfile, 'w')
     89     fp.write(''.join(format))
     90     fp.close()
     91 
     92 if __name__ == "__main__":
     93     main()
     94