Skip to content

Commit 03b7642

Browse files
authored
bpo-40334: Make the PyPegen* and PyParser* APIs more consistent (GH-19839)
This commit makes both APIs more consistent by doing the following: - Remove the `PyPegen_CodeObjectFrom*` functions, which weren't used and will probably not be needed. Functions like `Py_CompileStringObject` can be used instead. - Include a `const char *filename` parameter in `PyPegen_ASTFromString`. - Rename `PyPegen_ASTFromFile` to `PyPegen_ASTFromFilename`, because its signature is not the same with `PyParser_ASTFromFile`.
1 parent d9d6ead commit 03b7642

File tree

3 files changed

+36
-106
lines changed

3 files changed

+36
-106
lines changed

‎Include/internal/pegen_interface.h‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,34 @@ extern "C" {
1111
#include "Python.h"
1212
#include "Python-ast.h"
1313

14-
PyAPI_FUNC(mod_ty) PyPegen_ASTFromFile(const char *filename, int mode, PyCompilerFlags*, PyArena *arena);
15-
PyAPI_FUNC(mod_ty) PyPegen_ASTFromString(const char *str, int mode, PyCompilerFlags *flags,
16-
PyArena *arena);
17-
PyAPI_FUNC(mod_ty) PyPegen_ASTFromStringObject(const char *str, PyObject* filename, int mode,
18-
PyCompilerFlags *flags, PyArena *arena);
19-
PyAPI_FUNC(mod_ty) PyPegen_ASTFromFileObject(FILE *fp, PyObject *filename_ob,
20-
int mode, const char *enc, const char *ps1,
21-
const char *ps2, PyCompilerFlags *flags,
22-
int *errcode, PyArena *arena);
23-
PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromFile(const char *filename, int mode, PyCompilerFlags *flags);
24-
PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromString(const char *str, int mode,
25-
PyCompilerFlags *flags);
26-
PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromFileObject(FILE *, PyObject *filename_ob,
27-
int mode,
28-
const char *ps1,
29-
const char *ps2,
30-
PyCompilerFlags *flags,
31-
const char *enc,
32-
int *errcode);
14+
PyAPI_FUNC(mod_ty) PyPegen_ASTFromString(
15+
const char *str,
16+
const char *filename,
17+
int mode,
18+
PyCompilerFlags *flags,
19+
PyArena *arena);
20+
PyAPI_FUNC(mod_ty) PyPegen_ASTFromStringObject(
21+
const char *str,
22+
PyObject* filename,
23+
int mode,
24+
PyCompilerFlags *flags,
25+
PyArena *arena);
26+
PyAPI_FUNC(mod_ty) PyPegen_ASTFromFileObject(
27+
FILE *fp,
28+
PyObject *filename_ob,
29+
int mode,
30+
const char *enc,
31+
const char *ps1,
32+
const char *ps2,
33+
PyCompilerFlags *flags,
34+
int *errcode,
35+
PyArena *arena);
36+
PyAPI_FUNC(mod_ty) PyPegen_ASTFromFilename(
37+
const char *filename,
38+
int mode,
39+
PyCompilerFlags *flags,
40+
PyArena *arena);
41+
3342

3443
#ifdef __cplusplus
3544
}

‎Modules/_peg_parser.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ _Py_parse_file(PyObject *self, PyObject *args, PyObject *kwds)
3131
PyCompilerFlags flags = _PyCompilerFlags_INIT;
3232
PyObject *result = NULL;
3333

34-
mod_ty res = PyPegen_ASTFromFile(filename, mode, &flags, arena);
34+
mod_ty res = PyPegen_ASTFromFilename(filename, mode, &flags, arena);
3535
if (res == NULL) {
3636
goto error;
3737
}
@@ -84,7 +84,7 @@ _Py_parse_string(PyObject *self, PyObject *args, PyObject *kwds)
8484
res = PyParser_ASTFromString(the_string, "<string>", mode, &flags, arena);
8585
}
8686
else {
87-
res = PyPegen_ASTFromString(the_string, mode, &flags, arena);
87+
res = PyPegen_ASTFromString(the_string, "<string>", mode, &flags, arena);
8888
}
8989
if (res == NULL) {
9090
goto error;

‎Parser/pegen/peg_api.c‎

Lines changed: 6 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
#include "pegen.h"
55

66
mod_ty
7-
PyPegen_ASTFromString(const char *str, int mode, PyCompilerFlags *flags, PyArena *arena)
7+
PyPegen_ASTFromString(const char *str, const char *filename, int mode,
8+
PyCompilerFlags *flags, PyArena *arena)
89
{
9-
PyObject *filename_ob = PyUnicode_FromString("<string>");
10+
PyObject *filename_ob = PyUnicode_FromString(filename);
1011
if (filename_ob == NULL) {
1112
return NULL;
1213
}
@@ -16,7 +17,8 @@ PyPegen_ASTFromString(const char *str, int mode, PyCompilerFlags *flags, PyArena
1617
}
1718

1819
mod_ty
19-
PyPegen_ASTFromStringObject(const char *str, PyObject* filename, int mode, PyCompilerFlags *flags, PyArena *arena)
20+
PyPegen_ASTFromStringObject(const char *str, PyObject* filename, int mode,
21+
PyCompilerFlags *flags, PyArena *arena)
2022
{
2123
if (PySys_Audit("compile", "yO", str, filename) < 0) {
2224
return NULL;
@@ -27,7 +29,7 @@ PyPegen_ASTFromStringObject(const char *str, PyObject* filename, int mode, PyCom
2729
}
2830

2931
mod_ty
30-
PyPegen_ASTFromFile(const char *filename, int mode, PyCompilerFlags *flags, PyArena *arena)
32+
PyPegen_ASTFromFilename(const char *filename, int mode, PyCompilerFlags *flags, PyArena *arena)
3133
{
3234
PyObject *filename_ob = PyUnicode_FromString(filename);
3335
if (filename_ob == NULL) {
@@ -50,84 +52,3 @@ PyPegen_ASTFromFileObject(FILE *fp, PyObject *filename_ob, int mode,
5052
return _PyPegen_run_parser_from_file_pointer(fp, mode, filename_ob, enc, ps1, ps2,
5153
flags, errcode, arena);
5254
}
53-
54-
PyCodeObject *
55-
PyPegen_CodeObjectFromString(const char *str, int mode, PyCompilerFlags *flags)
56-
{
57-
PyArena *arena = PyArena_New();
58-
if (arena == NULL) {
59-
return NULL;
60-
}
61-
62-
PyCodeObject *result = NULL;
63-
64-
PyObject *filename_ob = PyUnicode_FromString("<string>");
65-
if (filename_ob == NULL) {
66-
goto error;
67-
}
68-
69-
mod_ty res = PyPegen_ASTFromString(str, mode, flags, arena);
70-
if (res == NULL) {
71-
goto error;
72-
}
73-
74-
result = PyAST_CompileObject(res, filename_ob, NULL, -1, arena);
75-
76-
error:
77-
Py_XDECREF(filename_ob);
78-
PyArena_Free(arena);
79-
return result;
80-
}
81-
82-
PyCodeObject *
83-
PyPegen_CodeObjectFromFile(const char *filename, int mode, PyCompilerFlags* flags)
84-
{
85-
PyArena *arena = PyArena_New();
86-
if (arena == NULL) {
87-
return NULL;
88-
}
89-
90-
PyCodeObject *result = NULL;
91-
92-
PyObject *filename_ob = PyUnicode_FromString(filename);
93-
if (filename_ob == NULL) {
94-
goto error;
95-
}
96-
97-
mod_ty res = PyPegen_ASTFromFile(filename, mode, flags, arena);
98-
if (res == NULL) {
99-
goto error;
100-
}
101-
102-
result = PyAST_CompileObject(res, filename_ob, NULL, -1, arena);
103-
104-
error:
105-
Py_XDECREF(filename_ob);
106-
PyArena_Free(arena);
107-
return result;
108-
}
109-
110-
PyCodeObject *
111-
PyPegen_CodeObjectFromFileObject(FILE *fp, PyObject *filename_ob, int mode,
112-
const char *ps1, const char *ps2,
113-
PyCompilerFlags *flags, const char *enc, int *errcode)
114-
{
115-
PyArena *arena = PyArena_New();
116-
if (arena == NULL) {
117-
return NULL;
118-
}
119-
120-
PyCodeObject *result = NULL;
121-
122-
mod_ty res = PyPegen_ASTFromFileObject(fp, filename_ob, mode, enc, ps1, ps2,
123-
flags, errcode, arena);
124-
if (res == NULL) {
125-
goto error;
126-
}
127-
128-
result = PyAST_CompileObject(res, filename_ob, NULL, -1, arena);
129-
130-
error:
131-
PyArena_Free(arena);
132-
return result;
133-
}

0 commit comments

Comments
 (0)