diff -r 64d9569b4e1f Doc/library/keyword.rst --- a/Doc/library/keyword.rst Sat Oct 12 01:41:49 2013 +0200 +++ b/Doc/library/keyword.rst Sat Oct 12 02:30:18 2013 +0200 @@ -4,8 +4,6 @@ .. module:: keyword :synopsis: Test whether a string is a keyword in Python. -**Source code:** :source:`Lib/keyword.py` - -------------- This module allows a Python program to determine if a string is a keyword. diff -r 64d9569b4e1f Lib/keyword.py --- a/Lib/keyword.py Sat Oct 12 01:41:49 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,94 +0,0 @@ -#! /usr/bin/env python3 - -"""Keywords (from "graminit.c") - -This file is automatically generated; please don't muck it up! - -To update the symbols in this file, 'cd' to the top directory of -the python source tree after building the interpreter and run: - - ./python Lib/keyword.py -""" - -__all__ = ["iskeyword", "kwlist"] - -kwlist = [ -#--start keywords-- - 'False', - 'None', - 'True', - 'and', - 'as', - 'assert', - 'break', - 'class', - 'continue', - 'def', - 'del', - 'elif', - 'else', - 'except', - 'finally', - 'for', - 'from', - 'global', - 'if', - 'import', - 'in', - 'is', - 'lambda', - 'nonlocal', - 'not', - 'or', - 'pass', - 'raise', - 'return', - 'try', - 'while', - 'with', - 'yield', -#--end keywords-- - ] - -iskeyword = frozenset(kwlist).__contains__ - -def main(): - import sys, re - - args = sys.argv[1:] - iptfile = args and args[0] or "Python/graminit.c" - if len(args) > 1: optfile = args[1] - else: optfile = "Lib/keyword.py" - - # load the output skeleton from the target, taking care to preserve its - # newline convention. - with open(optfile, newline='') as fp: - format = fp.readlines() - nl = format[0][len(format[0].strip()):] if format else '\n' - - # scan the source file for keywords - with open(iptfile) as fp: - strprog = re.compile('"([^"]+)"') - lines = [] - for line in fp: - if '{1, "' in line: - match = strprog.search(line) - if match: - lines.append(" '" + match.group(1) + "'," + nl) - lines.sort() - - # insert the lines of keywords into the skeleton - try: - start = format.index("#--start keywords--" + nl) + 1 - end = format.index("#--end keywords--" + nl) - format[start:end] = lines - except ValueError: - sys.stderr.write("target does not contain format markers\n") - sys.exit(1) - - # write the output file - with open(optfile, 'w', newline='') as fp: - fp.writelines(format) - -if __name__ == "__main__": - main() diff -r 64d9569b4e1f Modules/Setup.dist --- a/Modules/Setup.dist Sat Oct 12 01:41:49 2013 +0200 +++ b/Modules/Setup.dist Sat Oct 12 02:30:18 2013 +0200 @@ -132,6 +132,9 @@ zipimport zipimport.c # faulthandler module faulthandler faulthandler.c +# keyword module +keyword keyword.c + # The rest of the modules listed in this file are all commented out by # default. Usually they can be detected and built as dynamically # loaded modules by the new setup.py script added in Python 2.1. If diff -r 64d9569b4e1f Modules/keyword.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Modules/keyword.c Sat Oct 12 02:30:18 2013 +0200 @@ -0,0 +1,86 @@ +/* To update the symbols in Modules/keyword.h, 'cd' to the top directory of + the python source tree after building the interpreter and run: + + ./python Tools/scripts/keyword.py +*/ + +#include "Python.h" +#include "keyword.h" + +static PyObject *keywords_tuple = NULL; +static PyObject *keywords_set = NULL; + +static PyObject* +create_tuple(void) +{ + PyObject *tuple, *str; + size_t i; + const size_t count = Py_ARRAY_LENGTH(keywords); + + tuple = PyTuple_New(count); + if (tuple == NULL) + return NULL; + + for (i=0; i 1: + keyword_h = args[1] + else: + keyword_h = "Modules/keyword.h" + + # scan the source file for keywords + keywords = [] + with open(graminit) as fp: + strprog = re.compile('{1, "([^"]+)"') + lines = [] + for line in fp: + match = strprog.search(line) + if match: + keywords.append(match.group(1)) + keywords.sort() + + # write the output file + with open(keyword_h, 'w') as fp: + fp.write("/* file generated by %s script */\n\n" % sys.argv[0]) + fp.write("const char* keywords[] = {\n") + last_index = len(keywords) - 1 + for index, keyword in enumerate(keywords): + if index != last_index: + fp.write(' "%s",\n' % keyword) + else: + fp.write(' "%s"\n' % keyword) + fp.write("};\n\n") + +if __name__ == "__main__": + main() +