Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1 | /* parsermodule.c |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 3 | * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic |
| 4 | * Institute and State University, Blacksburg, Virginia, USA. |
| 5 | * Portions copyright 1991-1995 by Stichting Mathematisch Centrum, |
| 6 | * Amsterdam, The Netherlands. Copying is permitted under the terms |
| 7 | * associated with the main Python distribution, with the additional |
| 8 | * restriction that this additional notice be included and maintained |
| 9 | * on all distributed copies. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 10 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 11 | * This module serves to replace the original parser module written |
| 12 | * by Guido. The functionality is not matched precisely, but the |
| 13 | * original may be implemented on top of this. This is desirable |
| 14 | * since the source of the text to be parsed is now divorced from |
| 15 | * this interface. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 16 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 17 | * Unlike the prior interface, the ability to give a parse tree |
| 18 | * produced by Python code as a tuple to the compiler is enabled by |
| 19 | * this module. See the documentation for more details. |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 20 | * |
| 21 | * I've added some annotations that help with the lint code-checking |
| 22 | * program, but they're not complete by a long shot. The real errors |
| 23 | * that lint detects are gone, but there are still warnings with |
| 24 | * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations |
| 25 | * look like "NOTE(...)". |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 26 | * |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 29 | #include "Python.h" /* general Python API */ |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 30 | #include "Python-ast.h" /* mod_ty */ |
Victor Stinner | 3bb183d | 2018-11-22 18:38:38 +0100 | [diff] [blame] | 31 | #undef Yield /* undefine macro conflicting with <winbase.h> */ |
Victor Stinner | 5f2df88 | 2018-11-12 00:56:19 +0100 | [diff] [blame] | 32 | #include "ast.h" |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 33 | #include "graminit.h" /* symbols defined in the grammar */ |
| 34 | #include "node.h" /* internal parser structure */ |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 35 | #include "errcode.h" /* error codes for PyNode_*() */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 36 | #include "token.h" /* token definitions */ |
Victor Stinner | 5f2df88 | 2018-11-12 00:56:19 +0100 | [diff] [blame] | 37 | /* ISTERMINAL() / ISNONTERMINAL() */ |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 38 | #include "grammar.h" |
| 39 | #include "parsetok.h" |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 40 | |
| 41 | extern grammar _PyParser_Grammar; /* From graminit.c */ |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 42 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 43 | #ifdef lint |
| 44 | #include <note.h> |
| 45 | #else |
| 46 | #define NOTE(x) |
| 47 | #endif |
| 48 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 49 | /* String constants used to initialize module attributes. |
| 50 | * |
| 51 | */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 52 | static const char parser_copyright_string[] = |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 53 | "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\ |
Guido van Rossum | 2a28846 | 1996-08-21 21:55:43 +0000 | [diff] [blame] | 54 | University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\ |
| 55 | Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\ |
| 56 | Centrum, Amsterdam, The Netherlands."; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 57 | |
| 58 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 59 | PyDoc_STRVAR(parser_doc_string, |
| 60 | "This is an interface to Python's internal parser."); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 61 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 62 | static const char parser_version_string[] = "0.5"; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 63 | |
| 64 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 65 | typedef PyObject* (*SeqMaker) (Py_ssize_t length); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 66 | typedef int (*SeqInserter) (PyObject* sequence, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 67 | Py_ssize_t index, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 68 | PyObject* element); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 69 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 70 | /* The function below is copyrighted by Stichting Mathematisch Centrum. The |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 71 | * original copyright statement is included below, and continues to apply |
| 72 | * in full to the function immediately following. All other material is |
| 73 | * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic |
| 74 | * Institute and State University. Changes were made to comply with the |
Guido van Rossum | 2a28846 | 1996-08-21 21:55:43 +0000 | [diff] [blame] | 75 | * new naming conventions. Added arguments to provide support for creating |
| 76 | * lists as well as tuples, and optionally including the line numbers. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 77 | */ |
| 78 | |
Guido van Rossum | 52f2c05 | 1993-11-10 12:53:24 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 80 | static PyObject* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 81 | node2tuple(node *n, /* node to convert */ |
| 82 | SeqMaker mkseq, /* create sequence */ |
| 83 | SeqInserter addelem, /* func. to add elem. in seq. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 84 | int lineno, /* include line numbers? */ |
| 85 | int col_offset) /* include column offsets? */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 86 | { |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 87 | PyObject *result = NULL, *w; |
| 88 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 89 | if (n == NULL) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 90 | Py_RETURN_NONE; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 91 | } |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 92 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 93 | if (ISNONTERMINAL(TYPE(n))) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 94 | int i; |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 95 | |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 96 | result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl)); |
| 97 | if (result == NULL) |
| 98 | goto error; |
| 99 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 100 | w = PyLong_FromLong(TYPE(n)); |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 101 | if (w == NULL) |
| 102 | goto error; |
| 103 | (void) addelem(result, 0, w); |
| 104 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 105 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 106 | w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset); |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 107 | if (w == NULL) |
| 108 | goto error; |
| 109 | (void) addelem(result, i+1, w); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 110 | } |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 111 | |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 112 | if (TYPE(n) == encoding_decl) { |
| 113 | w = PyUnicode_FromString(STR(n)); |
| 114 | if (w == NULL) |
| 115 | goto error; |
| 116 | (void) addelem(result, i+1, w); |
| 117 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 118 | } |
| 119 | else if (ISTERMINAL(TYPE(n))) { |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 120 | result = mkseq(2 + lineno + col_offset); |
| 121 | if (result == NULL) |
| 122 | goto error; |
| 123 | |
| 124 | w = PyLong_FromLong(TYPE(n)); |
| 125 | if (w == NULL) |
| 126 | goto error; |
| 127 | (void) addelem(result, 0, w); |
| 128 | |
| 129 | w = PyUnicode_FromString(STR(n)); |
| 130 | if (w == NULL) |
| 131 | goto error; |
| 132 | (void) addelem(result, 1, w); |
| 133 | |
Serhiy Storchaka | e5362ea | 2018-04-19 01:55:37 +0300 | [diff] [blame] | 134 | if (lineno) { |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 135 | w = PyLong_FromLong(n->n_lineno); |
| 136 | if (w == NULL) |
| 137 | goto error; |
| 138 | (void) addelem(result, 2, w); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 139 | } |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 140 | |
Serhiy Storchaka | e5362ea | 2018-04-19 01:55:37 +0300 | [diff] [blame] | 141 | if (col_offset) { |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 142 | w = PyLong_FromLong(n->n_col_offset); |
| 143 | if (w == NULL) |
| 144 | goto error; |
Serhiy Storchaka | e5362ea | 2018-04-19 01:55:37 +0300 | [diff] [blame] | 145 | (void) addelem(result, 2 + lineno, w); |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 146 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 147 | } |
| 148 | else { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 149 | PyErr_SetString(PyExc_SystemError, |
| 150 | "unrecognized parse tree node type"); |
| 151 | return ((PyObject*) NULL); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 152 | } |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 153 | return result; |
| 154 | |
| 155 | error: |
| 156 | Py_XDECREF(result); |
| 157 | return NULL; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 158 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 159 | /* |
| 160 | * End of material copyrighted by Stichting Mathematisch Centrum. |
| 161 | */ |
Guido van Rossum | 52f2c05 | 1993-11-10 12:53:24 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 163 | |
| 164 | |
| 165 | /* There are two types of intermediate objects we're interested in: |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 166 | * 'eval' and 'exec' types. These constants can be used in the st_type |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 167 | * field of the object type to identify which any given object represents. |
| 168 | * These should probably go in an external header to allow other extensions |
| 169 | * to use them, but then, we really should be using C++ too. ;-) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 170 | */ |
| 171 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 172 | #define PyST_EXPR 1 |
| 173 | #define PyST_SUITE 2 |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 174 | |
| 175 | |
| 176 | /* These are the internal objects and definitions required to implement the |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 177 | * ST type. Most of the internal names are more reminiscent of the 'old' |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 178 | * naming style, but the code uses the new naming convention. |
| 179 | */ |
| 180 | |
| 181 | static PyObject* |
| 182 | parser_error = 0; |
| 183 | |
| 184 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 185 | typedef struct { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 186 | PyObject_HEAD /* standard object header */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 187 | node* st_node; /* the node* returned by the parser */ |
| 188 | int st_type; /* EXPR or SUITE ? */ |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 189 | PyCompilerFlags st_flags; /* Parser and compiler flags */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 190 | } PyST_Object; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 191 | |
| 192 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 193 | static void parser_free(PyST_Object *st); |
Jesus Cea | e9c5318 | 2012-08-03 14:28:37 +0200 | [diff] [blame] | 194 | static PyObject* parser_sizeof(PyST_Object *, void *); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 195 | static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op); |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 196 | static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *); |
| 197 | static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *); |
| 198 | static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *); |
| 199 | static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *); |
| 200 | static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 201 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 202 | #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS) |
| 203 | |
| 204 | static PyMethodDef parser_methods[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 205 | {"compile", (PyCFunction)(void(*)(void))parser_compilest, PUBLIC_METHOD_TYPE, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 206 | PyDoc_STR("Compile this ST object into a code object.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 207 | {"isexpr", (PyCFunction)(void(*)(void))parser_isexpr, PUBLIC_METHOD_TYPE, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 208 | PyDoc_STR("Determines if this ST object was created from an expression.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 209 | {"issuite", (PyCFunction)(void(*)(void))parser_issuite, PUBLIC_METHOD_TYPE, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 210 | PyDoc_STR("Determines if this ST object was created from a suite.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 211 | {"tolist", (PyCFunction)(void(*)(void))parser_st2list, PUBLIC_METHOD_TYPE, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 212 | PyDoc_STR("Creates a list-tree representation of this ST.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 213 | {"totuple", (PyCFunction)(void(*)(void))parser_st2tuple, PUBLIC_METHOD_TYPE, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 214 | PyDoc_STR("Creates a tuple-tree representation of this ST.")}, |
Jesus Cea | e9c5318 | 2012-08-03 14:28:37 +0200 | [diff] [blame] | 215 | {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS, |
| 216 | PyDoc_STR("Returns size in memory, in bytes.")}, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 217 | {NULL, NULL, 0, NULL} |
| 218 | }; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 219 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 220 | static |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 221 | PyTypeObject PyST_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 222 | PyVarObject_HEAD_INIT(NULL, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 223 | "parser.st", /* tp_name */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 224 | (int) sizeof(PyST_Object), /* tp_basicsize */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 225 | 0, /* tp_itemsize */ |
| 226 | (destructor)parser_free, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 227 | 0, /* tp_vectorcall_offset */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 228 | 0, /* tp_getattr */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 229 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 230 | 0, /* tp_as_async */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 231 | 0, /* tp_repr */ |
| 232 | 0, /* tp_as_number */ |
| 233 | 0, /* tp_as_sequence */ |
| 234 | 0, /* tp_as_mapping */ |
| 235 | 0, /* tp_hash */ |
| 236 | 0, /* tp_call */ |
| 237 | 0, /* tp_str */ |
| 238 | 0, /* tp_getattro */ |
| 239 | 0, /* tp_setattro */ |
Fred Drake | 69b9ae4 | 1997-05-23 04:04:17 +0000 | [diff] [blame] | 240 | |
| 241 | /* Functions to access object as input/output buffer */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 242 | 0, /* tp_as_buffer */ |
Fred Drake | 69b9ae4 | 1997-05-23 04:04:17 +0000 | [diff] [blame] | 243 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 244 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Fred Drake | 69b9ae4 | 1997-05-23 04:04:17 +0000 | [diff] [blame] | 245 | |
| 246 | /* __doc__ */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 247 | "Intermediate representation of a Python parse tree.", |
| 248 | 0, /* tp_traverse */ |
| 249 | 0, /* tp_clear */ |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 250 | parser_richcompare, /* tp_richcompare */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 251 | 0, /* tp_weaklistoffset */ |
| 252 | 0, /* tp_iter */ |
| 253 | 0, /* tp_iternext */ |
| 254 | parser_methods, /* tp_methods */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 255 | }; /* PyST_Type */ |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 256 | |
| 257 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 258 | /* PyST_Type isn't subclassable, so just check ob_type */ |
| 259 | #define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type) |
| 260 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 261 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 262 | parser_compare_nodes(node *left, node *right) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 263 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 264 | int j; |
Guido van Rossum | 52f2c05 | 1993-11-10 12:53:24 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 266 | if (TYPE(left) < TYPE(right)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 267 | return (-1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 268 | |
| 269 | if (TYPE(right) < TYPE(left)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 270 | return (1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 271 | |
| 272 | if (ISTERMINAL(TYPE(left))) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 273 | return (strcmp(STR(left), STR(right))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 274 | |
| 275 | if (NCH(left) < NCH(right)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 276 | return (-1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 277 | |
| 278 | if (NCH(right) < NCH(left)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 279 | return (1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 280 | |
| 281 | for (j = 0; j < NCH(left); ++j) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 282 | int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 283 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 284 | if (v != 0) |
| 285 | return (v); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 286 | } |
| 287 | return (0); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 288 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 289 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 290 | /* parser_richcompare(PyObject* left, PyObject* right, int op) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 291 | * |
| 292 | * Comparison function used by the Python operators ==, !=, <, >, <=, >= |
| 293 | * This really just wraps a call to parser_compare_nodes() with some easy |
| 294 | * checks and protection code. |
| 295 | * |
| 296 | */ |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 297 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 298 | static PyObject * |
| 299 | parser_richcompare(PyObject *left, PyObject *right, int op) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 300 | { |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 301 | int result; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 302 | |
| 303 | /* neither argument should be NULL, unless something's gone wrong */ |
| 304 | if (left == NULL || right == NULL) { |
| 305 | PyErr_BadInternalCall(); |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | /* both arguments should be instances of PyST_Object */ |
| 310 | if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 311 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 314 | if (left == right) |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 315 | /* if arguments are identical, they're equal */ |
| 316 | result = 0; |
| 317 | else |
| 318 | result = parser_compare_nodes(((PyST_Object *)left)->st_node, |
| 319 | ((PyST_Object *)right)->st_node); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 320 | |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 321 | Py_RETURN_RICHCOMPARE(result, 0, op); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 322 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 323 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 324 | /* parser_newstobject(node* st) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 325 | * |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 326 | * Allocates a new Python object representing an ST. This is simply the |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 327 | * 'wrapper' object that holds a node* and allows it to be passed around in |
| 328 | * Python code. |
| 329 | * |
| 330 | */ |
| 331 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 332 | parser_newstobject(node *st, int type) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 333 | { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 334 | PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 335 | |
| 336 | if (o != 0) { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 337 | o->st_node = st; |
| 338 | o->st_type = type; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 339 | o->st_flags = _PyCompilerFlags_INIT; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 340 | } |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 341 | else { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 342 | PyNode_Free(st); |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 343 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 344 | return ((PyObject*)o); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 345 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 346 | |
| 347 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 348 | /* void parser_free(PyST_Object* st) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 349 | * |
| 350 | * This is called by a del statement that reduces the reference count to 0. |
| 351 | * |
| 352 | */ |
| 353 | static void |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 354 | parser_free(PyST_Object *st) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 355 | { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 356 | PyNode_Free(st->st_node); |
| 357 | PyObject_Del(st); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 358 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 359 | |
Jesus Cea | e9c5318 | 2012-08-03 14:28:37 +0200 | [diff] [blame] | 360 | static PyObject * |
| 361 | parser_sizeof(PyST_Object *st, void *unused) |
| 362 | { |
| 363 | Py_ssize_t res; |
| 364 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 365 | res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node); |
Jesus Cea | e9c5318 | 2012-08-03 14:28:37 +0200 | [diff] [blame] | 366 | return PyLong_FromSsize_t(res); |
| 367 | } |
| 368 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 369 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 370 | /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 371 | * |
| 372 | * This provides conversion from a node* to a tuple object that can be |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 373 | * returned to the Python-level caller. The ST object is not modified. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 374 | * |
| 375 | */ |
| 376 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 377 | parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 378 | { |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 379 | int line_info = 0; |
| 380 | int col_info = 0; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 381 | PyObject *res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 382 | int ok; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 383 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 384 | static char *keywords[] = {"st", "line_info", "col_info", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 385 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 386 | if (self == NULL || PyModule_Check(self)) { |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 387 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords, |
| 388 | &PyST_Type, &self, &line_info, |
| 389 | &col_info); |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 390 | } |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 391 | else |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 392 | ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1], |
| 393 | &line_info, &col_info); |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 394 | if (ok != 0) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 395 | /* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 396 | * Convert ST into a tuple representation. Use Guido's function, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 397 | * since it's known to work already. |
| 398 | */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 399 | res = node2tuple(((PyST_Object*)self)->st_node, |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 400 | PyTuple_New, PyTuple_SetItem, line_info, col_info); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 401 | } |
| 402 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 403 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 404 | |
| 405 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 406 | /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 407 | * |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 408 | * This provides conversion from a node* to a list object that can be |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 409 | * returned to the Python-level caller. The ST object is not modified. |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 410 | * |
| 411 | */ |
| 412 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 413 | parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 414 | { |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 415 | int line_info = 0; |
| 416 | int col_info = 0; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 417 | PyObject *res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 418 | int ok; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 419 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 420 | static char *keywords[] = {"st", "line_info", "col_info", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 421 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 422 | if (self == NULL || PyModule_Check(self)) |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 423 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords, |
| 424 | &PyST_Type, &self, &line_info, |
| 425 | &col_info); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 426 | else |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 427 | ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1], |
| 428 | &line_info, &col_info); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 429 | if (ok) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 430 | /* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 431 | * Convert ST into a tuple representation. Use Guido's function, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 432 | * since it's known to work already. |
| 433 | */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 434 | res = node2tuple(self->st_node, |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 435 | PyList_New, PyList_SetItem, line_info, col_info); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 436 | } |
| 437 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 438 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 439 | |
| 440 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 441 | /* parser_compilest(PyObject* self, PyObject* args) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 442 | * |
| 443 | * This function creates code objects from the parse tree represented by |
| 444 | * the passed-in data object. An optional file name is passed in as well. |
| 445 | * |
| 446 | */ |
| 447 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 448 | parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 449 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 450 | PyObject* res = NULL; |
| 451 | PyArena* arena = NULL; |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 452 | mod_ty mod; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 453 | PyObject* filename = NULL; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 454 | int ok; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 455 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 456 | static char *keywords[] = {"st", "filename", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 457 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 458 | if (self == NULL || PyModule_Check(self)) |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 459 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords, |
| 460 | &PyST_Type, &self, |
| 461 | PyUnicode_FSDecoder, &filename); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 462 | else |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 463 | ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1], |
| 464 | PyUnicode_FSDecoder, &filename); |
| 465 | if (!ok) |
| 466 | goto error; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 467 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 468 | if (filename == NULL) { |
| 469 | filename = PyUnicode_FromString("<syntax-tree>"); |
| 470 | if (filename == NULL) |
| 471 | goto error; |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 472 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 473 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 474 | arena = PyArena_New(); |
| 475 | if (!arena) |
| 476 | goto error; |
| 477 | |
| 478 | mod = PyAST_FromNodeObject(self->st_node, &self->st_flags, |
| 479 | filename, arena); |
| 480 | if (!mod) |
| 481 | goto error; |
| 482 | |
| 483 | res = (PyObject *)PyAST_CompileObject(mod, filename, |
| 484 | &self->st_flags, -1, arena); |
| 485 | error: |
| 486 | Py_XDECREF(filename); |
| 487 | if (arena != NULL) |
| 488 | PyArena_Free(arena); |
| 489 | return res; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 490 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 491 | |
| 492 | |
| 493 | /* PyObject* parser_isexpr(PyObject* self, PyObject* args) |
| 494 | * PyObject* parser_issuite(PyObject* self, PyObject* args) |
| 495 | * |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 496 | * Checks the passed-in ST object to determine if it is an expression or |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 497 | * a statement suite, respectively. The return is a Python truth value. |
| 498 | * |
| 499 | */ |
| 500 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 501 | parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 502 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 503 | PyObject* res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 504 | int ok; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 505 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 506 | static char *keywords[] = {"st", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 507 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 508 | if (self == NULL || PyModule_Check(self)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 509 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 510 | &PyST_Type, &self); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 511 | else |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 512 | ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 513 | |
| 514 | if (ok) { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 515 | /* Check to see if the ST represents an expression or not. */ |
| 516 | res = (self->st_type == PyST_EXPR) ? Py_True : Py_False; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 517 | Py_INCREF(res); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 518 | } |
| 519 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 520 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 521 | |
| 522 | |
| 523 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 524 | parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 525 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 526 | PyObject* res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 527 | int ok; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 528 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 529 | static char *keywords[] = {"st", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 530 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 531 | if (self == NULL || PyModule_Check(self)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 532 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 533 | &PyST_Type, &self); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 534 | else |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 535 | ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 536 | |
| 537 | if (ok) { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 538 | /* Check to see if the ST represents an expression or not. */ |
| 539 | res = (self->st_type == PyST_EXPR) ? Py_False : Py_True; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 540 | Py_INCREF(res); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 541 | } |
| 542 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 543 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 544 | |
| 545 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 546 | /* err_string(const char* message) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 547 | * |
| 548 | * Sets the error string for an exception of type ParserError. |
| 549 | * |
| 550 | */ |
| 551 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 552 | err_string(const char *message) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 553 | { |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 554 | PyErr_SetString(parser_error, message); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 555 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 556 | |
| 557 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 558 | /* PyObject* parser_do_parse(PyObject* args, int type) |
| 559 | * |
| 560 | * Internal function to actually execute the parse and return the result if |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 561 | * successful or set an exception if not. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 562 | * |
| 563 | */ |
| 564 | static PyObject* |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 565 | parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 566 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 567 | char* string = 0; |
| 568 | PyObject* res = 0; |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 569 | int flags = 0; |
| 570 | perrdetail err; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 571 | |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 572 | static char *keywords[] = {"source", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 573 | |
| 574 | if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) { |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 575 | node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL, |
| 576 | &_PyParser_Grammar, |
| 577 | (type == PyST_EXPR) |
| 578 | ? eval_input : file_input, |
| 579 | &err, &flags); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | if (n) { |
| 582 | res = parser_newstobject(n, type); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 583 | if (res) { |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 584 | ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 585 | ((PyST_Object *)res)->st_flags.cf_feature_version = PY_MINOR_VERSION; |
| 586 | } |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 587 | } |
Benjamin Peterson | f719957d | 2011-06-04 22:06:42 -0500 | [diff] [blame] | 588 | else { |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 589 | PyParser_SetError(&err); |
Benjamin Peterson | f719957d | 2011-06-04 22:06:42 -0500 | [diff] [blame] | 590 | } |
Benjamin Peterson | f0cdbad | 2011-06-05 22:14:05 -0500 | [diff] [blame] | 591 | PyParser_ClearError(&err); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 592 | } |
| 593 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 594 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 595 | |
| 596 | |
| 597 | /* PyObject* parser_expr(PyObject* self, PyObject* args) |
| 598 | * PyObject* parser_suite(PyObject* self, PyObject* args) |
| 599 | * |
| 600 | * External interfaces to the parser itself. Which is called determines if |
| 601 | * the parser attempts to recognize an expression ('eval' form) or statement |
| 602 | * suite ('exec' form). The real work is done by parser_do_parse() above. |
| 603 | * |
| 604 | */ |
| 605 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 606 | parser_expr(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 607 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 608 | NOTE(ARGUNUSED(self)) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 609 | return (parser_do_parse(args, kw, "s:expr", PyST_EXPR)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 610 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 611 | |
| 612 | |
| 613 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 614 | parser_suite(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 615 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 616 | NOTE(ARGUNUSED(self)) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 617 | return (parser_do_parse(args, kw, "s:suite", PyST_SUITE)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 618 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 619 | |
| 620 | |
| 621 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 622 | /* This is the messy part of the code. Conversion from a tuple to an ST |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 623 | * object requires that the input tuple be valid without having to rely on |
| 624 | * catching an exception from the compiler. This is done to allow the |
| 625 | * compiler itself to remain fast, since most of its input will come from |
| 626 | * the parser directly, and therefore be known to be syntactically correct. |
| 627 | * This validation is done to ensure that we don't core dump the compile |
| 628 | * phase, returning an exception instead. |
| 629 | * |
| 630 | * Two aspects can be broken out in this code: creating a node tree from |
| 631 | * the tuple passed in, and verifying that it is indeed valid. It may be |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 632 | * advantageous to expand the number of ST types to include funcdefs and |
| 633 | * lambdadefs to take advantage of the optimizer, recognizing those STs |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 634 | * here. They are not necessary, and not quite as useful in a raw form. |
| 635 | * For now, let's get expressions and suites working reliably. |
| 636 | */ |
| 637 | |
| 638 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 639 | static node* build_node_tree(PyObject *tuple); |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 640 | |
| 641 | static int |
| 642 | validate_node(node *tree) |
| 643 | { |
| 644 | int type = TYPE(tree); |
| 645 | int nch = NCH(tree); |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 646 | state *dfa_state; |
| 647 | int pos, arc; |
| 648 | |
| 649 | assert(ISNONTERMINAL(type)); |
| 650 | type -= NT_OFFSET; |
| 651 | if (type >= _PyParser_Grammar.g_ndfas) { |
| 652 | PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree)); |
| 653 | return 0; |
| 654 | } |
Inada Naoki | 09415ff | 2019-04-23 20:39:37 +0900 | [diff] [blame] | 655 | const dfa *nt_dfa = &_PyParser_Grammar.g_dfa[type]; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 656 | REQ(tree, nt_dfa->d_type); |
| 657 | |
| 658 | /* Run the DFA for this nonterminal. */ |
tyomitch | 1b304f9 | 2019-03-09 17:35:50 +0200 | [diff] [blame] | 659 | dfa_state = nt_dfa->d_state; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 660 | for (pos = 0; pos < nch; ++pos) { |
| 661 | node *ch = CHILD(tree, pos); |
| 662 | int ch_type = TYPE(ch); |
tyomitch | cb0748d | 2019-04-03 08:12:07 +0300 | [diff] [blame] | 663 | if ((ch_type >= NT_OFFSET + _PyParser_Grammar.g_ndfas) |
| 664 | || (ISTERMINAL(ch_type) && (ch_type >= N_TOKENS)) |
| 665 | || (ch_type < 0) |
| 666 | ) { |
| 667 | PyErr_Format(parser_error, "Unrecognized node type %d.", ch_type); |
| 668 | return 0; |
| 669 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 670 | if (ch_type == suite && TYPE(tree) == funcdef) { |
| 671 | /* This is the opposite hack of what we do in parser.c |
| 672 | (search for func_body_suite), except we don't ever |
| 673 | support type comments here. */ |
| 674 | ch_type = func_body_suite; |
| 675 | } |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 676 | for (arc = 0; arc < dfa_state->s_narcs; ++arc) { |
| 677 | short a_label = dfa_state->s_arc[arc].a_lbl; |
| 678 | assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels); |
Pablo Galindo | 9a0000d | 2019-03-21 23:33:02 +0000 | [diff] [blame] | 679 | |
| 680 | const char *label_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str; |
| 681 | if ((_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type) |
| 682 | && ((ch->n_str == NULL) || (label_str == NULL) |
| 683 | || (strcmp(ch->n_str, label_str) == 0)) |
| 684 | ) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 685 | /* The child is acceptable; if non-terminal, validate it recursively. */ |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 686 | if (ISNONTERMINAL(ch_type) && !validate_node(ch)) |
| 687 | return 0; |
| 688 | |
| 689 | /* Update the state, and move on to the next child. */ |
| 690 | dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow]; |
| 691 | goto arc_found; |
| 692 | } |
| 693 | } |
| 694 | /* What would this state have accepted? */ |
| 695 | { |
| 696 | short a_label = dfa_state->s_arc->a_lbl; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 697 | if (!a_label) /* Wouldn't accept any more children */ |
| 698 | goto illegal_num_children; |
| 699 | |
Pablo Galindo | 9a0000d | 2019-03-21 23:33:02 +0000 | [diff] [blame] | 700 | int next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type; |
| 701 | const char *expected_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str; |
| 702 | |
| 703 | if (ISNONTERMINAL(next_type)) { |
tyomitch | cb0748d | 2019-04-03 08:12:07 +0300 | [diff] [blame] | 704 | PyErr_Format(parser_error, "Expected %s, got %s.", |
| 705 | _PyParser_Grammar.g_dfa[next_type - NT_OFFSET].d_name, |
| 706 | ISTERMINAL(ch_type) ? _PyParser_TokenNames[ch_type] : |
| 707 | _PyParser_Grammar.g_dfa[ch_type - NT_OFFSET].d_name); |
Pablo Galindo | 9a0000d | 2019-03-21 23:33:02 +0000 | [diff] [blame] | 708 | } |
| 709 | else if (expected_str != NULL) { |
| 710 | PyErr_Format(parser_error, "Illegal terminal: expected '%s'.", |
| 711 | expected_str); |
| 712 | } |
| 713 | else { |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 714 | PyErr_Format(parser_error, "Illegal terminal: expected %s.", |
| 715 | _PyParser_TokenNames[next_type]); |
Pablo Galindo | 9a0000d | 2019-03-21 23:33:02 +0000 | [diff] [blame] | 716 | } |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | arc_found: |
| 721 | continue; |
| 722 | } |
| 723 | /* Are we in a final state? If so, return 1 for successful validation. */ |
| 724 | for (arc = 0; arc < dfa_state->s_narcs; ++arc) { |
| 725 | if (!dfa_state->s_arc[arc].a_lbl) { |
| 726 | return 1; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | illegal_num_children: |
| 731 | PyErr_Format(parser_error, |
| 732 | "Illegal number of children for %s node.", nt_dfa->d_name); |
| 733 | return 0; |
| 734 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 735 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 736 | /* PyObject* parser_tuple2st(PyObject* self, PyObject* args) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 737 | * |
| 738 | * This is the public function, called from the Python code. It receives a |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 739 | * single tuple object from the caller, and creates an ST object if the |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 740 | * tuple can be validated. It does this by checking the first code of the |
| 741 | * tuple, and, if acceptable, builds the internal representation. If this |
| 742 | * step succeeds, the internal representation is validated as fully as |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 743 | * possible with the recursive validate_node() routine defined above. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 744 | * |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 745 | * This function must be changed if support is to be added for PyST_FRAGMENT |
| 746 | * ST objects. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 747 | * |
| 748 | */ |
| 749 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 750 | parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 751 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 752 | NOTE(ARGUNUSED(self)) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 753 | PyObject *st = 0; |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 754 | PyObject *tuple; |
| 755 | node *tree; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 756 | |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 757 | static char *keywords[] = {"sequence", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 758 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 759 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords, |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 760 | &tuple)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 761 | return (0); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 762 | if (!PySequence_Check(tuple)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 763 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 764 | "sequence2st() requires a single sequence argument"); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 765 | return (0); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 766 | } |
| 767 | /* |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 768 | * Convert the tree to the internal form before checking it. |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 769 | */ |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 770 | tree = build_node_tree(tuple); |
| 771 | if (tree != 0) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 772 | node *validation_root = NULL; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 773 | int tree_type = 0; |
| 774 | switch (TYPE(tree)) { |
| 775 | case eval_input: |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 776 | /* Might be an eval form. */ |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 777 | tree_type = PyST_EXPR; |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 778 | validation_root = tree; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 779 | break; |
| 780 | case encoding_decl: |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 781 | /* This looks like an encoding_decl so far. */ |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 782 | if (NCH(tree) == 1) { |
| 783 | tree_type = PyST_SUITE; |
| 784 | validation_root = CHILD(tree, 0); |
| 785 | } |
| 786 | else { |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 787 | err_string("Error Parsing encoding_decl"); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 788 | } |
| 789 | break; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 790 | case file_input: |
| 791 | /* This looks like an exec form so far. */ |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 792 | tree_type = PyST_SUITE; |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 793 | validation_root = tree; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 794 | break; |
| 795 | default: |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 796 | /* This is a fragment, at best. */ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 797 | err_string("parse tree does not use a valid start symbol"); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 798 | } |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 799 | |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 800 | if (validation_root != NULL && validate_node(validation_root)) |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 801 | st = parser_newstobject(tree, tree_type); |
| 802 | else |
| 803 | PyNode_Free(tree); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 804 | } |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 805 | /* Make sure we raise an exception on all errors. We should never |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 806 | * get this, but we'd do well to be sure something is done. |
| 807 | */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 808 | if (st == NULL && !PyErr_Occurred()) |
| 809 | err_string("unspecified ST error occurred"); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 810 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 811 | return st; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 812 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 813 | |
| 814 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 815 | /* node* build_node_children() |
| 816 | * |
| 817 | * Iterate across the children of the current non-terminal node and build |
| 818 | * their structures. If successful, return the root of this portion of |
| 819 | * the tree, otherwise, 0. Any required exception will be specified already, |
| 820 | * and no memory will have been deallocated. |
| 821 | * |
| 822 | */ |
| 823 | static node* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 824 | build_node_children(PyObject *tuple, node *root, int *line_num) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 825 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 826 | Py_ssize_t len = PyObject_Size(tuple); |
| 827 | Py_ssize_t i; |
| 828 | int err; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 829 | |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 830 | if (len < 0) { |
| 831 | return NULL; |
| 832 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 833 | for (i = 1; i < len; ++i) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 834 | /* elem must always be a sequence, however simple */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 835 | PyObject* elem = PySequence_GetItem(tuple, i); |
| 836 | int ok = elem != NULL; |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 837 | int type = 0; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 838 | char *strn = 0; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 839 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 840 | if (ok) |
| 841 | ok = PySequence_Check(elem); |
| 842 | if (ok) { |
| 843 | PyObject *temp = PySequence_GetItem(elem, 0); |
| 844 | if (temp == NULL) |
| 845 | ok = 0; |
| 846 | else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 847 | ok = PyLong_Check(temp); |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 848 | if (ok) { |
| 849 | type = _PyLong_AsInt(temp); |
| 850 | if (type == -1 && PyErr_Occurred()) { |
| 851 | Py_DECREF(temp); |
| 852 | Py_DECREF(elem); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 853 | return NULL; |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 854 | } |
| 855 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 856 | Py_DECREF(temp); |
| 857 | } |
| 858 | } |
| 859 | if (!ok) { |
Victor Stinner | 5f8d485 | 2014-01-02 11:49:27 +0100 | [diff] [blame] | 860 | PyObject *err = Py_BuildValue("Os", elem, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 861 | "Illegal node construct."); |
| 862 | PyErr_SetObject(parser_error, err); |
| 863 | Py_XDECREF(err); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 864 | Py_XDECREF(elem); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 865 | return NULL; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 866 | } |
| 867 | if (ISTERMINAL(type)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 868 | Py_ssize_t len = PyObject_Size(elem); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 869 | PyObject *temp; |
Neal Norwitz | 3fcbea5 | 2007-08-26 04:51:28 +0000 | [diff] [blame] | 870 | const char *temp_str; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 871 | |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 872 | if ((len != 2) && (len != 3)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 873 | err_string("terminal nodes must have 2 or 3 entries"); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 874 | Py_DECREF(elem); |
| 875 | return NULL; |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 876 | } |
| 877 | temp = PySequence_GetItem(elem, 1); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 878 | if (temp == NULL) { |
| 879 | Py_DECREF(elem); |
| 880 | return NULL; |
| 881 | } |
Neal Norwitz | 3fcbea5 | 2007-08-26 04:51:28 +0000 | [diff] [blame] | 882 | if (!PyUnicode_Check(temp)) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 883 | PyErr_Format(parser_error, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 884 | "second item in terminal node must be a string," |
| 885 | " found %s", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 886 | Py_TYPE(temp)->tp_name); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 887 | Py_DECREF(temp); |
Neal Norwitz | 2cde0eb | 2007-08-11 04:58:43 +0000 | [diff] [blame] | 888 | Py_DECREF(elem); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 889 | return NULL; |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 890 | } |
| 891 | if (len == 3) { |
| 892 | PyObject *o = PySequence_GetItem(elem, 2); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 893 | if (o == NULL) { |
| 894 | Py_DECREF(temp); |
| 895 | Py_DECREF(elem); |
| 896 | return NULL; |
| 897 | } |
| 898 | if (PyLong_Check(o)) { |
| 899 | int num = _PyLong_AsInt(o); |
| 900 | if (num == -1 && PyErr_Occurred()) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 901 | Py_DECREF(o); |
| 902 | Py_DECREF(temp); |
Neal Norwitz | 2cde0eb | 2007-08-11 04:58:43 +0000 | [diff] [blame] | 903 | Py_DECREF(elem); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 904 | return NULL; |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 905 | } |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 906 | *line_num = num; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 907 | } |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 908 | else { |
| 909 | PyErr_Format(parser_error, |
| 910 | "third item in terminal node must be an" |
| 911 | " integer, found %s", |
| 912 | Py_TYPE(temp)->tp_name); |
| 913 | Py_DECREF(o); |
| 914 | Py_DECREF(temp); |
| 915 | Py_DECREF(elem); |
| 916 | return NULL; |
| 917 | } |
| 918 | Py_DECREF(o); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 919 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 920 | temp_str = PyUnicode_AsUTF8AndSize(temp, &len); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 921 | if (temp_str == NULL) { |
| 922 | Py_DECREF(temp); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 923 | Py_DECREF(elem); |
| 924 | return NULL; |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 925 | } |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 926 | strn = (char *)PyObject_MALLOC(len + 1); |
Victor Stinner | 3bd6abd | 2013-07-12 01:33:59 +0200 | [diff] [blame] | 927 | if (strn == NULL) { |
| 928 | Py_DECREF(temp); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 929 | Py_DECREF(elem); |
Victor Stinner | 3bd6abd | 2013-07-12 01:33:59 +0200 | [diff] [blame] | 930 | PyErr_NoMemory(); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 931 | return NULL; |
Victor Stinner | 3bd6abd | 2013-07-12 01:33:59 +0200 | [diff] [blame] | 932 | } |
| 933 | (void) memcpy(strn, temp_str, len + 1); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 934 | Py_DECREF(temp); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 935 | } |
| 936 | else if (!ISNONTERMINAL(type)) { |
| 937 | /* |
| 938 | * It has to be one or the other; this is an error. |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 939 | * Raise an exception. |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 940 | */ |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 941 | PyObject *err = Py_BuildValue("Os", elem, "unknown node type."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 942 | PyErr_SetObject(parser_error, err); |
| 943 | Py_XDECREF(err); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 944 | Py_DECREF(elem); |
| 945 | return NULL; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 946 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 947 | err = PyNode_AddChild(root, type, strn, *line_num, 0, *line_num, 0); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 948 | if (err == E_NOMEM) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 949 | Py_DECREF(elem); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 950 | PyObject_FREE(strn); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 951 | PyErr_NoMemory(); |
| 952 | return NULL; |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 953 | } |
| 954 | if (err == E_OVERFLOW) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 955 | Py_DECREF(elem); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 956 | PyObject_FREE(strn); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 957 | PyErr_SetString(PyExc_ValueError, |
| 958 | "unsupported number of child nodes"); |
| 959 | return NULL; |
| 960 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 961 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 962 | if (ISNONTERMINAL(type)) { |
| 963 | node* new_child = CHILD(root, i - 1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 964 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 965 | if (new_child != build_node_children(elem, new_child, line_num)) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 966 | Py_DECREF(elem); |
| 967 | return NULL; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | else if (type == NEWLINE) { /* It's true: we increment the */ |
| 971 | ++(*line_num); /* line number *after* the newline! */ |
| 972 | } |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 973 | Py_DECREF(elem); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 974 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 975 | return root; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 976 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 977 | |
| 978 | |
| 979 | static node* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 980 | build_node_tree(PyObject *tuple) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 981 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 982 | node* res = 0; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 983 | PyObject *temp = PySequence_GetItem(tuple, 0); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 984 | long num = -1; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 985 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 986 | if (temp != NULL) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 987 | num = PyLong_AsLong(temp); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 988 | Py_XDECREF(temp); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 989 | if (ISTERMINAL(num)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 990 | /* |
| 991 | * The tuple is simple, but it doesn't start with a start symbol. |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 992 | * Raise an exception now and be done with it. |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 993 | */ |
Victor Stinner | 6684bdf | 2013-07-17 00:13:52 +0200 | [diff] [blame] | 994 | tuple = Py_BuildValue("Os", tuple, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 995 | "Illegal syntax-tree; cannot start with terminal symbol."); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 996 | PyErr_SetObject(parser_error, tuple); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 997 | Py_XDECREF(tuple); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 998 | } |
| 999 | else if (ISNONTERMINAL(num)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1000 | /* |
| 1001 | * Not efficient, but that can be handled later. |
| 1002 | */ |
| 1003 | int line_num = 0; |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 1004 | PyObject *encoding = NULL; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1005 | |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 1006 | if (num == encoding_decl) { |
| 1007 | encoding = PySequence_GetItem(tuple, 2); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1008 | if (encoding == NULL) { |
| 1009 | PyErr_SetString(parser_error, "missed encoding"); |
| 1010 | return NULL; |
| 1011 | } |
| 1012 | if (!PyUnicode_Check(encoding)) { |
| 1013 | PyErr_Format(parser_error, |
| 1014 | "encoding must be a string, found %.200s", |
| 1015 | Py_TYPE(encoding)->tp_name); |
| 1016 | Py_DECREF(encoding); |
| 1017 | return NULL; |
| 1018 | } |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 1019 | /* tuple isn't borrowed anymore here, need to DECREF */ |
| 1020 | tuple = PySequence_GetSlice(tuple, 0, 2); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1021 | if (tuple == NULL) { |
| 1022 | Py_DECREF(encoding); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1023 | return NULL; |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1024 | } |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 1025 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1026 | res = PyNode_New(num); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 1027 | if (res != NULL) { |
| 1028 | if (res != build_node_children(tuple, res, &line_num)) { |
| 1029 | PyNode_Free(res); |
| 1030 | res = NULL; |
| 1031 | } |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 1032 | if (res && encoding) { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1033 | Py_ssize_t len; |
Neal Norwitz | 3fcbea5 | 2007-08-26 04:51:28 +0000 | [diff] [blame] | 1034 | const char *temp; |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1035 | temp = PyUnicode_AsUTF8AndSize(encoding, &len); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1036 | if (temp == NULL) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1037 | PyNode_Free(res); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1038 | Py_DECREF(encoding); |
| 1039 | Py_DECREF(tuple); |
| 1040 | return NULL; |
| 1041 | } |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 1042 | res->n_str = (char *)PyObject_MALLOC(len + 1); |
Victor Stinner | 3bd6abd | 2013-07-12 01:33:59 +0200 | [diff] [blame] | 1043 | if (res->n_str == NULL) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1044 | PyNode_Free(res); |
Victor Stinner | 3bd6abd | 2013-07-12 01:33:59 +0200 | [diff] [blame] | 1045 | Py_DECREF(encoding); |
| 1046 | Py_DECREF(tuple); |
| 1047 | PyErr_NoMemory(); |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | (void) memcpy(res->n_str, temp, len + 1); |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 1051 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1052 | } |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1053 | if (encoding != NULL) { |
| 1054 | Py_DECREF(encoding); |
| 1055 | Py_DECREF(tuple); |
| 1056 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1057 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1058 | else { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1059 | /* The tuple is illegal -- if the number is neither TERMINAL nor |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 1060 | * NONTERMINAL, we can't use it. Not sure the implementation |
| 1061 | * allows this condition, but the API doesn't preclude it. |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1062 | */ |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1063 | PyObject *err = Py_BuildValue("Os", tuple, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1064 | "Illegal component tuple."); |
| 1065 | PyErr_SetObject(parser_error, err); |
| 1066 | Py_XDECREF(err); |
| 1067 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1068 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1069 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1070 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1071 | |
| 1072 | |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1073 | static PyObject* |
| 1074 | pickle_constructor = NULL; |
| 1075 | |
| 1076 | |
| 1077 | static PyObject* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1078 | parser__pickler(PyObject *self, PyObject *args) |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1079 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 1080 | NOTE(ARGUNUSED(self)) |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1081 | PyObject *result = NULL; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1082 | PyObject *st = NULL; |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 1083 | PyObject *empty_dict = NULL; |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1084 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1085 | if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1086 | PyObject *newargs; |
| 1087 | PyObject *tuple; |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1088 | |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 1089 | if ((empty_dict = PyDict_New()) == NULL) |
| 1090 | goto finally; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1091 | if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1092 | goto finally; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1093 | tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1094 | if (tuple != NULL) { |
| 1095 | result = Py_BuildValue("O(O)", pickle_constructor, tuple); |
| 1096 | Py_DECREF(tuple); |
| 1097 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1098 | Py_DECREF(newargs); |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1099 | } |
| 1100 | finally: |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 1101 | Py_XDECREF(empty_dict); |
| 1102 | |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1103 | return (result); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1104 | } |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1105 | |
| 1106 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1107 | /* Functions exported by this module. Most of this should probably |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1108 | * be converted into an ST object with methods, but that is better |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1109 | * done directly in Python, allowing subclasses to be created directly. |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1110 | * We'd really have to write a wrapper around it all anyway to allow |
| 1111 | * inheritance. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1112 | */ |
| 1113 | static PyMethodDef parser_functions[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1114 | {"compilest", (PyCFunction)(void(*)(void))parser_compilest, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1115 | PyDoc_STR("Compiles an ST object into a code object.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1116 | {"expr", (PyCFunction)(void(*)(void))parser_expr, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1117 | PyDoc_STR("Creates an ST object from an expression.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1118 | {"isexpr", (PyCFunction)(void(*)(void))parser_isexpr, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1119 | PyDoc_STR("Determines if an ST object was created from an expression.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1120 | {"issuite", (PyCFunction)(void(*)(void))parser_issuite, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1121 | PyDoc_STR("Determines if an ST object was created from a suite.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1122 | {"suite", (PyCFunction)(void(*)(void))parser_suite, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1123 | PyDoc_STR("Creates an ST object from a suite.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1124 | {"sequence2st", (PyCFunction)(void(*)(void))parser_tuple2st, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1125 | PyDoc_STR("Creates an ST object from a tree representation.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1126 | {"st2tuple", (PyCFunction)(void(*)(void))parser_st2tuple, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1127 | PyDoc_STR("Creates a tuple-tree representation of an ST.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1128 | {"st2list", (PyCFunction)(void(*)(void))parser_st2list, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1129 | PyDoc_STR("Creates a list-tree representation of an ST.")}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1130 | {"tuple2st", (PyCFunction)(void(*)(void))parser_tuple2st, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1131 | PyDoc_STR("Creates an ST object from a tree representation.")}, |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1132 | |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1133 | /* private stuff: support pickle module */ |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1134 | {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1135 | PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")}, |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1136 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 1137 | {NULL, NULL, 0, NULL} |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1138 | }; |
| 1139 | |
| 1140 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1141 | |
| 1142 | static struct PyModuleDef parsermodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | PyModuleDef_HEAD_INIT, |
| 1144 | "parser", |
| 1145 | NULL, |
| 1146 | -1, |
| 1147 | parser_functions, |
| 1148 | NULL, |
| 1149 | NULL, |
| 1150 | NULL, |
| 1151 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1152 | }; |
| 1153 | |
| 1154 | PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */ |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 1155 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1156 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1157 | PyInit_parser(void) |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 1158 | { |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1159 | PyObject *module, *copyreg; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1160 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 1161 | if (PyType_Ready(&PyST_Type) < 0) |
| 1162 | return NULL; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1163 | module = PyModule_Create(&parsermodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1164 | if (module == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | return NULL; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1166 | |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 1167 | if (parser_error == 0) |
| 1168 | parser_error = PyErr_NewException("parser.ParserError", NULL, NULL); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1169 | |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 1170 | if (parser_error == 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1171 | return NULL; |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 1172 | /* CAUTION: The code next used to skip bumping the refcount on |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1173 | * parser_error. That's a disaster if PyInit_parser() gets called more |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 1174 | * than once. By incref'ing, we ensure that each module dict that |
| 1175 | * gets created owns its reference to the shared parser_error object, |
| 1176 | * and the file static parser_error vrbl owns a reference too. |
| 1177 | */ |
| 1178 | Py_INCREF(parser_error); |
| 1179 | if (PyModule_AddObject(module, "ParserError", parser_error) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1180 | return NULL; |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 1181 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1182 | Py_INCREF(&PyST_Type); |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1183 | PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1184 | |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1185 | PyModule_AddStringConstant(module, "__copyright__", |
| 1186 | parser_copyright_string); |
| 1187 | PyModule_AddStringConstant(module, "__doc__", |
| 1188 | parser_doc_string); |
| 1189 | PyModule_AddStringConstant(module, "__version__", |
| 1190 | parser_version_string); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1191 | |
Fred Drake | 78bdb9b | 2001-07-19 20:17:15 +0000 | [diff] [blame] | 1192 | /* Register to support pickling. |
| 1193 | * If this fails, the import of this module will fail because an |
| 1194 | * exception will be raised here; should we clear the exception? |
| 1195 | */ |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 1196 | copyreg = PyImport_ImportModuleNoBlock("copyreg"); |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1197 | if (copyreg != NULL) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1198 | PyObject *func, *pickler; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1199 | _Py_IDENTIFIER(pickle); |
| 1200 | _Py_IDENTIFIER(sequence2st); |
| 1201 | _Py_IDENTIFIER(_pickler); |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1202 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1203 | func = _PyObject_GetAttrId(copyreg, &PyId_pickle); |
| 1204 | pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st); |
| 1205 | pickler = _PyObject_GetAttrId(module, &PyId__pickler); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1206 | Py_XINCREF(pickle_constructor); |
| 1207 | if ((func != NULL) && (pickle_constructor != NULL) |
| 1208 | && (pickler != NULL)) { |
| 1209 | PyObject *res; |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1210 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1211 | res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler, |
| 1212 | pickle_constructor, NULL); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1213 | Py_XDECREF(res); |
| 1214 | } |
| 1215 | Py_XDECREF(func); |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1216 | Py_XDECREF(pickle_constructor); |
| 1217 | Py_XDECREF(pickler); |
| 1218 | Py_DECREF(copyreg); |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1219 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1220 | return module; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1221 | } |