Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-40334: Make the PyPegen* and PyParser* APIs more consistent #19839

Merged
merged 2 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions Include/internal/pegen_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,34 @@ extern "C" {
#include "Python.h"
#include "Python-ast.h"

PyAPI_FUNC(mod_ty) PyPegen_ASTFromFile(const char *filename, int mode, PyCompilerFlags*, PyArena *arena);
PyAPI_FUNC(mod_ty) PyPegen_ASTFromString(const char *str, int mode, PyCompilerFlags *flags,
PyArena *arena);
PyAPI_FUNC(mod_ty) PyPegen_ASTFromStringObject(const char *str, PyObject* filename, int mode,
PyCompilerFlags *flags, PyArena *arena);
PyAPI_FUNC(mod_ty) PyPegen_ASTFromFileObject(FILE *fp, PyObject *filename_ob,
int mode, const char *enc, const char *ps1,
const char *ps2, PyCompilerFlags *flags,
int *errcode, PyArena *arena);
PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromFile(const char *filename, int mode, PyCompilerFlags *flags);
PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromString(const char *str, int mode,
PyCompilerFlags *flags);
PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromFileObject(FILE *, PyObject *filename_ob,
int mode,
const char *ps1,
const char *ps2,
PyCompilerFlags *flags,
const char *enc,
int *errcode);
PyAPI_FUNC(mod_ty) PyPegen_ASTFromString(
const char *str,
const char *filename,
int mode,
PyCompilerFlags *flags,
PyArena *arena);
PyAPI_FUNC(mod_ty) PyPegen_ASTFromStringObject(
const char *str,
PyObject* filename,
int mode,
PyCompilerFlags *flags,
PyArena *arena);
PyAPI_FUNC(mod_ty) PyPegen_ASTFromFileObject(
FILE *fp,
PyObject *filename_ob,
int mode,
const char *enc,
const char *ps1,
const char *ps2,
PyCompilerFlags *flags,
int *errcode,
PyArena *arena);
PyAPI_FUNC(mod_ty) PyPegen_ASTFromFilename(
const char *filename,
int mode,
PyCompilerFlags *flags,
PyArena *arena);


#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/_peg_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ _Py_parse_file(PyObject *self, PyObject *args, PyObject *kwds)
PyCompilerFlags flags = _PyCompilerFlags_INIT;
PyObject *result = NULL;

mod_ty res = PyPegen_ASTFromFile(filename, mode, &flags, arena);
mod_ty res = PyPegen_ASTFromFilename(filename, mode, &flags, arena);
if (res == NULL) {
goto error;
}
Expand Down Expand Up @@ -84,7 +84,7 @@ _Py_parse_string(PyObject *self, PyObject *args, PyObject *kwds)
res = PyParser_ASTFromString(the_string, "<string>", mode, &flags, arena);
}
else {
res = PyPegen_ASTFromString(the_string, mode, &flags, arena);
res = PyPegen_ASTFromString(the_string, "<string>", mode, &flags, arena);
}
if (res == NULL) {
goto error;
Expand Down
91 changes: 6 additions & 85 deletions Parser/pegen/peg_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#include "pegen.h"

mod_ty
PyPegen_ASTFromString(const char *str, int mode, PyCompilerFlags *flags, PyArena *arena)
PyPegen_ASTFromString(const char *str, const char *filename, int mode,
PyCompilerFlags *flags, PyArena *arena)
{
PyObject *filename_ob = PyUnicode_FromString("<string>");
PyObject *filename_ob = PyUnicode_FromString(filename);
if (filename_ob == NULL) {
return NULL;
}
Expand All @@ -16,7 +17,8 @@ PyPegen_ASTFromString(const char *str, int mode, PyCompilerFlags *flags, PyArena
}

mod_ty
PyPegen_ASTFromStringObject(const char *str, PyObject* filename, int mode, PyCompilerFlags *flags, PyArena *arena)
PyPegen_ASTFromStringObject(const char *str, PyObject* filename, int mode,
PyCompilerFlags *flags, PyArena *arena)
{
if (PySys_Audit("compile", "yO", str, filename) < 0) {
return NULL;
Expand All @@ -27,7 +29,7 @@ PyPegen_ASTFromStringObject(const char *str, PyObject* filename, int mode, PyCom
}

mod_ty
PyPegen_ASTFromFile(const char *filename, int mode, PyCompilerFlags *flags, PyArena *arena)
PyPegen_ASTFromFilename(const char *filename, int mode, PyCompilerFlags *flags, PyArena *arena)
{
PyObject *filename_ob = PyUnicode_FromString(filename);
if (filename_ob == NULL) {
Expand All @@ -50,84 +52,3 @@ PyPegen_ASTFromFileObject(FILE *fp, PyObject *filename_ob, int mode,
return _PyPegen_run_parser_from_file_pointer(fp, mode, filename_ob, enc, ps1, ps2,
flags, errcode, arena);
}

PyCodeObject *
PyPegen_CodeObjectFromString(const char *str, int mode, PyCompilerFlags *flags)
{
PyArena *arena = PyArena_New();
if (arena == NULL) {
return NULL;
}

PyCodeObject *result = NULL;

PyObject *filename_ob = PyUnicode_FromString("<string>");
if (filename_ob == NULL) {
goto error;
}

mod_ty res = PyPegen_ASTFromString(str, mode, flags, arena);
if (res == NULL) {
goto error;
}

result = PyAST_CompileObject(res, filename_ob, NULL, -1, arena);

error:
Py_XDECREF(filename_ob);
PyArena_Free(arena);
return result;
}

PyCodeObject *
PyPegen_CodeObjectFromFile(const char *filename, int mode, PyCompilerFlags* flags)
{
PyArena *arena = PyArena_New();
if (arena == NULL) {
return NULL;
}

PyCodeObject *result = NULL;

PyObject *filename_ob = PyUnicode_FromString(filename);
if (filename_ob == NULL) {
goto error;
}

mod_ty res = PyPegen_ASTFromFile(filename, mode, flags, arena);
if (res == NULL) {
goto error;
}

result = PyAST_CompileObject(res, filename_ob, NULL, -1, arena);

error:
Py_XDECREF(filename_ob);
PyArena_Free(arena);
return result;
}

PyCodeObject *
PyPegen_CodeObjectFromFileObject(FILE *fp, PyObject *filename_ob, int mode,
const char *ps1, const char *ps2,
PyCompilerFlags *flags, const char *enc, int *errcode)
{
PyArena *arena = PyArena_New();
if (arena == NULL) {
return NULL;
}

PyCodeObject *result = NULL;

mod_ty res = PyPegen_ASTFromFileObject(fp, filename_ob, mode, enc, ps1, ps2,
flags, errcode, arena);
if (res == NULL) {
goto error;
}

result = PyAST_CompileObject(res, filename_ob, NULL, -1, arena);

error:
PyArena_Free(arena);
return result;
}