|
1 | 1 | #include <Python.h>
|
2 | 2 | #include "pegen_interface.h"
|
3 | 3 |
|
4 |
| -PyObject * |
5 |
| -_Py_parse_file(PyObject *self, PyObject *args, PyObject *kwds) |
| 4 | +static int |
| 5 | +_mode_str_to_int(char *mode_str) |
6 | 6 | {
|
7 |
| - static char *keywords[] = {"file", "mode", NULL}; |
8 |
| - char *filename; |
9 |
| - char *mode_str = "exec"; |
10 |
| - |
11 |
| - if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", keywords, &filename, &mode_str)) { |
12 |
| - return NULL; |
13 |
| - } |
14 |
| - |
15 | 7 | int mode;
|
16 | 8 | if (strcmp(mode_str, "exec") == 0) {
|
17 | 9 | mode = Py_file_input;
|
18 | 10 | }
|
| 11 | + else if (strcmp(mode_str, "eval") == 0) { |
| 12 | + mode = Py_eval_input; |
| 13 | + } |
19 | 14 | else if (strcmp(mode_str, "single") == 0) {
|
20 | 15 | mode = Py_single_input;
|
21 | 16 | }
|
22 | 17 | else {
|
23 |
| - return PyErr_Format(PyExc_ValueError, "mode must be either 'exec' or 'single'"); |
| 18 | + mode = -1; |
24 | 19 | }
|
| 20 | + return mode; |
| 21 | +} |
25 | 22 |
|
26 |
| - PyArena *arena = PyArena_New(); |
27 |
| - if (arena == NULL) { |
| 23 | +static mod_ty |
<
6D4E
td data-grid-cell-id="diff-eff75977216acfbf58d51cf4e06cbcd558be39011196c414bef5bc632f209e46-27-24-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
24 | +_run_parser(char *str, char *filename, int mode, PyCompilerFlags *flags, PyArena *arena, int oldparser) |
| 25 | +{ |
| 26 | + mod_ty mod; |
| 27 | + if (!oldparser) { |
| 28 | + mod = PyPegen_ASTFromString(str, filename, mode, flags, arena); |
| 29 | + } |
| 30 | + else { |
| 31 | + mod = PyParser_ASTFromString(str, filename, mode, flags, arena); |
| 32 | + } |
| 33 | + return mod; |
| 34 | +} |
| 35 | + |
| 36 | +PyObject * |
| 37 | +_Py_compile_string(PyObject *self, PyObject *args, PyObject *kwds) |
| 38 | +{ |
| 39 | + static char *keywords[] = {"string", "filename", "mode", "oldparser", NULL}; |
| 40 | + char *the_string; |
| 41 | + char *filename = "<string>"; |
| 42 | + char *mode_str = "exec"; |
| 43 | + int oldparser = 0; |
| 44 | + |
| 45 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|ssp", keywords, |
| 46 | + &the_string, &filename, &mode_str, &oldparser)) { |
28 | 47 | return NULL;
|
29 | 48 | }
|
30 | 49 |
|
| 50 | + int mode = _mode_str_to_int(mode_str); |
| 51 | + if (mode == -1) { |
| 52 | + return PyErr_Format(PyExc_ValueError, "mode must be either 'exec' or 'eval' or 'single'"); |
| 53 | + } |
| 54 | + |
31 | 55 | PyCompilerFlags flags = _PyCompilerFlags_INIT;
|
32 |
| - PyObject *result = NULL; |
| 56 | + flags.cf_flags = PyCF_IGNORE_COOKIE; |
33 | 57 |
|
34 |
| - mod_ty res = PyPegen_ASTFromFilename(filename, mode, &flags, arena); |
35 |
| - if (res == NULL) { |
36 |
| - goto error; |
| 58 | + PyArena *arena = PyArena_New(); |
| 59 | + if (arena == NULL) { |
| 60 | + return NULL; |
| 61 | + } |
| 62 | + |
| 63 | + mod_ty mod = _run_parser(the_string, filename, mode, &flags, arena, oldparser); |
| 64 | + if (mod == NULL) { |
| 65 | + PyArena_Free(arena); |
| 66 | + return NULL; |
37 | 67 | }
|
38 |
| - result = PyAST_mod2obj(res); |
39 | 68 |
|
40 |
| -error: |
| 69 | + PyObject *filename_ob = PyUnicode_DecodeFSDefault(filename); |
| 70 | + if (filename_ob == NULL) { |
| 71 | + PyArena_Free(arena); |
| 72 | + return NULL; |
| 73 | + } |
| 74 | + PyCodeObject *result = PyAST_CompileObject(mod, filename_ob, &flags, -1, arena); |
| 75 | + Py_XDECREF(filename_ob); |
41 | 76 | PyArena_Free(arena);
|
42 |
| - return result; |
| 77 | + return (PyObject *)result; |
43 | 78 | }
|
44 | 79 |
|
45 | 80 | PyObject *
|
46 | 81 | _Py_parse_string(PyObject *self, PyObject *args, PyObject *kwds)
|
47 | 82 | {
|
48 |
| - static char *keywords[] = {"string", "mode", "oldparser", NULL}; |
| 83 | + static char *keywords[] = {"string", "filename", "mode", "oldparser", NULL}; |
49 | 84 | char *the_string;
|
| 85 | + char *filename = "<string>"; |
50 | 86 | char *mode_str = "exec";
|
51 | 87 | int oldparser = 0;
|
52 | 88 |
|
53 |
| - if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|sp", keywords, |
54 |
| - &the_string, &mode_str, &oldparser)) { |
| 89 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|ssp", keywords, |
| 90 | + &the_string, &filename, &mode_str, &oldparser)) { |
55 | 91 | return NULL;
|
56 | 92 | }
|
57 | 93 |
|
58 |
| - int mode; |
59 |
| - if (strcmp(mode_str, "exec") == 0) { |
60 |
| - mode = Py_file_input; |
61 |
| - } |
62 |
| - else if (strcmp(mode_str, "eval") == 0) { |
63 |
| - mode = Py_eval_input; |
64 |
| - } |
65 |
| - else if (strcmp(mode_str, "single") == 0) { |
66 |
| - mode = Py_single_input; |
67 |
| - } |
68 |
| - else { |
| 94 | + int mode = _mode_str_to_int(mode_str); |
| 95 | + if (mode == -1) { |
69 | 96 | return PyErr_Format(PyExc_ValueError, "mode must be either 'exec' or 'eval' or 'single'");
|
70 | 97 | }
|
71 | 98 |
|
| 99 | + PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 100 | + flags.cf_flags = PyCF_IGNORE_COOKIE; |
| 101 | + |
72 | 102 | PyArena *arena = PyArena_New();
|
73 | 103 | if (arena == NULL) {
|
74 | 104 | return NULL;
|
75 | 105 | }
|
76 | 106 |
|
77 |
| - PyObject *result = NULL; |
78 |
| - |
79 |
| - PyCompilerFlags flags = _PyCompilerFlags_INIT; |
80 |
| - flags.cf_flags = PyCF_IGNORE_COOKIE; |
81 |
| - |
82 |
| - mod_ty res; |
83 |
| - if (oldparser) { |
84 |
| - res = PyParser_ASTFromString(the_string, "<string>", mode, &flags, arena); |
85 |
| - } |
86 |
| - else { |
87 |
| - res = PyPegen_ASTFromString(the_string, "<string>", mode, &flags, arena); |
88 |
| - } |
89 |
| - if (res == NULL) { |
90 |
| - goto error; |
| 107 | + mod_ty mod = _run_parser(the_string, filename, mode, &flags, arena, oldparser); |
| 108 | + if (mod == NULL) { |
| 109 | + PyArena_Free(arena); |
| 110 | + return NULL; |
91 | 111 | }
|
92 |
| - result = PyAST_mod2obj(res); |
93 | 112 |
|
94 |
| -error: |
| 113 | + PyObject *result = PyAST_mod2obj(mod); |
95 | 114 | PyArena_Free(arena);
|
96 | 115 | return result;
|
97 | 116 | }
|
98 | 117 |
|
99 | 118 | static PyMethodDef ParseMethods[] = {
|
100 |
| - {"parse_file", (PyCFunction)(void (*)(void))_Py_parse_file, METH_VARARGS|METH_KEYWORDS, "Parse a file."}, |
101 |
| - {"parse_string", (PyCFunction)(void (*)(void))_Py_parse_string, METH_VARARGS|METH_KEYWORDS,"Parse a string."}, |
| 119 | + { |
| 120 | + "parse_string", |
| 121 | + (PyCFunction)(void (*)(void))_Py_parse_string, |
| 122 | + METH_VARARGS|METH_KEYWORDS, |
| 123 | + "Parse a string, return an AST." |
| 124 | + }, |
| 125 | + { |
| 126 | + "compile_string", |
| 127 | + (PyCFunction)(void (*)(void))_Py_compile_string, |
| 128 | + METH_VARARGS|METH_KEYWORDS, |
| 129 | + "Compile a string, return a code object." |
| 130 | + }, |
102 | 131 | {NULL, NULL, 0, NULL} /* Sentinel */
|
103 | 132 | };
|
104 | 133 |
|
|
0 commit comments