diff -r a57dfbba91f9 Doc/library/keyword.rst --- a/Doc/library/keyword.rst Sat Oct 12 12:32:21 2013 +0200 +++ b/Doc/library/keyword.rst Sat Oct 12 13:30:30 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 a57dfbba91f9 Lib/keyword.py --- a/Lib/keyword.py Sat Oct 12 12:32:21 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 a57dfbba91f9 Modules/Setup.dist --- a/Modules/Setup.dist Sat Oct 12 12:32:21 2013 +0200 +++ b/Modules/Setup.dist Sat Oct 12 13:30:30 2013 +0200 @@ -132,6 +132,9 @@ # 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 a57dfbba91f9 Modules/keyword.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Modules/keyword.c Sat Oct 12 13:30:30 2013 +0200 @@ -0,0 +1,76 @@ +#include "Python.h" +#include "grammar.h" + +extern grammar _PyParser_Grammar; /* From graminit.c */ + +static PyObject* +make_keywords(void) +{ + int i; + char *lb_str; + PyObject *keywords, *olabel; + labellist ll = _PyParser_Grammar.g_ll; + + if ((keywords = PyList_New(0)) == NULL) { + return NULL; + } + + for (i=0; i < ll.ll_nlabels; i++) { + lb_str = ll.ll_label[i].lb_str; + if (!lb_str || strcmp(lb_str, "EMPTY") == 0) { + continue; + } + if ((olabel = PyUnicode_FromString(lb_str)) == NULL) { + Py_DECREF(keywords); + return NULL; + } + if (PyList_Append(keywords, olabel) < 0) { + Py_DECREF(keywords); + Py_DECREF(olabel); + return NULL; + } + } + return keywords; +} + +static struct PyModuleDef keyword_module = { + PyModuleDef_HEAD_INIT, + "keyword", + PyDoc_STR("Keywords from grammar"), + -1, + NULL, +}; + +PyMODINIT_FUNC +PyInit_keyword(void) +{ + PyObject *m, *keywords, *fset, *iskeyword; + + m = PyModule_Create(&keyword_module); + if (m == NULL) + goto fail; + + keywords = make_keywords(); + if (keywords == NULL) { + goto fail; + } + PyModule_AddObject(m, "kwlist", keywords); + + fset = PyFrozenSet_New(keywords); + if (fset == NULL) { + goto fail; + } + + iskeyword = PyObject_GetAttrString(fset, "__contains__"); + Py_DECREF(fset); + if (iskeyword == NULL) { + goto fail; + } + PyModule_AddObject(m, "iskeyword", iskeyword); + + return m; + fail: + Py_DECREF(m); + return NULL; +} +