cinap_lenrekadd hg and python4bb9e2215 years agoHistory
3279 lines · 93.5 KB · C
RawDownload
          case while_stmt:            res = validate_while(tree);            break;          case for_stmt:            res = validate_for(tree);            break;          case try_stmt:            res = validate_try(tree);            break;          case suite:            res = validate_suite(tree);            break;            /*             *  Expression nodes.             */          case testlist:            res = validate_testlist(tree);            break;          case yield_expr:            res = validate_yield_expr(tree);            break;          case testlist1:            res = validate_testlist1(tree);            break;          case test:            res = validate_test(tree);            break;          case and_test:            res = validate_and_test(tree);            break;          case not_test:            res = validate_not_test(tree);            break;          case comparison:            res = validate_comparison(tree);            break;          case exprlist:            res = validate_exprlist(tree);            break;          case comp_op:            res = validate_comp_op(tree);            break;          case expr:            res = validate_expr(tree);            break;          case xor_expr:            res = validate_xor_expr(tree);            break;          case and_expr:            res = validate_and_expr(tree);            break;          case shift_expr:            res = validate_shift_expr(tree);            break;          case arith_expr:            res = validate_arith_expr(tree);            break;          case term:            res = validate_term(tree);            break;          case factor:            res = validate_factor(tree);            break;          case power:            res = validate_power(tree);            break;          case atom:            res = validate_atom(tree);            break;           default:            /* Hopefully never reached! */            err_string("unrecognized node type");            res = 0;            break;        }        tree = next;    }    return (res);}  static intvalidate_expr_tree(node *tree){    int res = validate_eval_input(tree);     if (!res && !PyErr_Occurred())        err_string("could not validate expression tuple");     return (res);}  /*  file_input: *      (NEWLINE | stmt)* ENDMARKER */static intvalidate_file_input(node *tree){    int j;    int nch = NCH(tree) - 1;    int res = ((nch >= 0)               && validate_ntype(CHILD(tree, nch), ENDMARKER));     for (j = 0; res && (j < nch); ++j) {        if (TYPE(CHILD(tree, j)) == stmt)            res = validate_stmt(CHILD(tree, j));        else            res = validate_newline(CHILD(tree, j));    }    /*  This stays in to prevent any internal failures from getting to the     *  user.  Hopefully, this won't be needed.  If a user reports getting     *  this, we have some debugging to do.     */    if (!res && !PyErr_Occurred())        err_string("VALIDATION FAILURE: report this to the maintainer!");     return (res);} static intvalidate_encoding_decl(node *tree){    int nch = NCH(tree);    int res = ((nch == 1)        && validate_file_input(CHILD(tree, 0)));     if (!res && !PyErr_Occurred())        err_string("Error Parsing encoding_decl");     return res;} static PyObject*pickle_constructor = NULL;  static PyObject*parser__pickler(PyObject *self, PyObject *args){    NOTE(ARGUNUSED(self))    PyObject *result = NULL;    PyObject *st = NULL;    PyObject *empty_dict = NULL;     if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {        PyObject *newargs;        PyObject *tuple;         if ((empty_dict = PyDict_New()) == NULL)            goto finally;        if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)            goto finally;        tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);        if (tuple != NULL) {            result = Py_BuildValue("O(O)", pickle_constructor, tuple);            Py_DECREF(tuple);        }        Py_DECREF(empty_dict);        Py_DECREF(newargs);    }  finally:    Py_XDECREF(empty_dict);     return (result);}  /*  Functions exported by this module.  Most of this should probably *  be converted into an ST object with methods, but that is better *  done directly in Python, allowing subclasses to be created directly. *  We'd really have to write a wrapper around it all anyway to allow *  inheritance. */static PyMethodDef parser_functions[] =  {    {"ast2tuple",       (PyCFunction)parser_st2tuple,  PUBLIC_METHOD_TYPE,        PyDoc_STR("Creates a tuple-tree representation of an ST.")},    {"ast2list",        (PyCFunction)parser_st2list,   PUBLIC_METHOD_TYPE,        PyDoc_STR("Creates a list-tree representation of an ST.")},    {"compileast",      (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,        PyDoc_STR("Compiles an ST object into a code object.")},    {"compilest",      (PyCFunction)parser_compilest,  PUBLIC_METHOD_TYPE,        PyDoc_STR("Compiles an ST object into a code object.")},    {"expr",            (PyCFunction)parser_expr,      PUBLIC_METHOD_TYPE,        PyDoc_STR("Creates an ST object from an expression.")},    {"isexpr",          (PyCFunction)parser_isexpr,    PUBLIC_METHOD_TYPE,        PyDoc_STR("Determines if an ST object was created from an expression.")},    {"issuite",         (PyCFunction)parser_issuite,   PUBLIC_METHOD_TYPE,        PyDoc_STR("Determines if an ST object was created from a suite.")},    {"suite",           (PyCFunction)parser_suite,     PUBLIC_METHOD_TYPE,        PyDoc_STR("Creates an ST object from a suite.")},    {"sequence2ast",    (PyCFunction)parser_tuple2st,  PUBLIC_METHOD_TYPE,        PyDoc_STR("Creates an ST object from a tree representation.")},    {"sequence2st",     (PyCFunction)parser_tuple2st,  PUBLIC_METHOD_TYPE,        PyDoc_STR("Creates an ST object from a tree representation.")},    {"st2tuple",        (PyCFunction)parser_st2tuple,  PUBLIC_METHOD_TYPE,        PyDoc_STR("Creates a tuple-tree representation of an ST.")},    {"st2list",         (PyCFunction)parser_st2list,   PUBLIC_METHOD_TYPE,        PyDoc_STR("Creates a list-tree representation of an ST.")},    {"tuple2ast",       (PyCFunction)parser_tuple2st,  PUBLIC_METHOD_TYPE,        PyDoc_STR("Creates an ST object from a tree representation.")},    {"tuple2st",        (PyCFunction)parser_tuple2st,  PUBLIC_METHOD_TYPE,        PyDoc_STR("Creates an ST object from a tree representation.")},     /* private stuff: support pickle module */    {"_pickler",        (PyCFunction)parser__pickler,  METH_VARARGS,        PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},     {NULL, NULL, 0, NULL}    };  PyMODINIT_FUNC initparser(void);  /* supply a prototype */ PyMODINIT_FUNCinitparser(void){    PyObject *module, *copyreg;     PyST_Type.ob_type = &PyType_Type;    module = Py_InitModule("parser", parser_functions);    if (module == NULL)    	return;     if (parser_error == 0)        parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);     if (parser_error == 0)        /* caller will check PyErr_Occurred() */        return;    /* CAUTION:  The code next used to skip bumping the refcount on     * parser_error.  That's a disaster if initparser() gets called more     * than once.  By incref'ing, we ensure that each module dict that     * gets created owns its reference to the shared parser_error object,     * and the file static parser_error vrbl owns a reference too.     */    Py_INCREF(parser_error);    if (PyModule_AddObject(module, "ParserError", parser_error) != 0)        return;     Py_INCREF(&PyST_Type);    PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);    Py_INCREF(&PyST_Type);    PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);     PyModule_AddStringConstant(module, "__copyright__",                               parser_copyright_string);    PyModule_AddStringConstant(module, "__doc__",                               parser_doc_string);    PyModule_AddStringConstant(module, "__version__",                               parser_version_string);     /* Register to support pickling.     * If this fails, the import of this module will fail because an     * exception will be raised here; should we clear the exception?     */    copyreg = PyImport_ImportModule("copy_reg");    if (copyreg != NULL) {        PyObject *func, *pickler;         func = PyObject_GetAttrString(copyreg, "pickle");        pickle_constructor = PyObject_GetAttrString(module, "sequence2st");        pickler = PyObject_GetAttrString(module, "_pickler");        Py_XINCREF(pickle_constructor);        if ((func != NULL) && (pickle_constructor != NULL)            && (pickler != NULL)) {            PyObject *res;             res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,                                               pickle_constructor, NULL);            Py_XDECREF(res);        }        Py_XDECREF(func);        Py_XDECREF(pickle_constructor);        Py_XDECREF(pickler);        Py_DECREF(copyreg);    }}

Lines 3001–3279 of 3279. Previous lines