Skip to content

Commit 2b74c83

Browse files
authored
bpo-40334: Support CO_FUTURE_BARRY_AS_BDFL in the new parser (GH-19721)
This commit also allows to pass flags to the new parser in all interfaces and fixes a bug in the parser generator that was causing to inline rules with actions, making them disappear.
1 parent 9adccc1 commit 2b74c83

File tree

12 files changed

+437
-327
lines changed

12 files changed

+437
-327
lines changed

Grammar/python.gram

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ compare_op_bitwise_or_pair[CmpopExprPair*]:
323323
| isnot_bitwise_or
324324
| is_bitwise_or
325325
eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
326-
noteq_bitwise_or[CmpopExprPair*]: '!=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotEq, a) }
326+
noteq_bitwise_or[CmpopExprPair*]:
327+
| (tok='!=' {_PyPegen_check_barry_as_flufl(p) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
327328
lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
328329
lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
329330
gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }

Include/internal/pegen_interface.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@ extern "C" {
1111
#include "Python.h"
1212
#include "Python-ast.h"
1313

14-
PyAPI_FUNC(mod_ty) PyPegen_ASTFromFile(const char *filename, int mode, PyArena *arena);
14+
PyAPI_FUNC(mod_ty) PyPegen_ASTFromFile(const char *filename, int mode, PyCompilerFlags*, PyArena *arena);
1515
PyAPI_FUNC(mod_ty) PyPegen_ASTFromString(const char *str, int mode, PyCompilerFlags *flags,
1616
PyArena *arena);
1717
PyAPI_FUNC(mod_ty) PyPegen_ASTFromStringObject(const char *str, PyObject* filename, int mode,
1818
PyCompilerFlags *flags, PyArena *arena);
1919
PyAPI_FUNC(mod_ty) PyPegen_ASTFromFileObject(FILE *fp, PyObject *filename_ob,
2020
int mode, const char *enc, const char *ps1,
21-
const char *ps2, int *errcode, PyArena *arena);
22-
PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromFile(const char *filename, int mode);
21+
const char *ps2, PyCompilerFlags *flags,
22+
int *errcode, PyArena *arena);
23+
PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromFile(const char *filename, int mode, PyCompilerFlags *flags);
2324
PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromString(const char *str, int mode,
2425
PyCompilerFlags *flags);
2526
PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromFileObject(FILE *, PyObject *filename_ob,
26-
int mode, const char *enc,
27+
int mode,
2728
const char *ps1,
2829
const char *ps2,
30+
PyCompilerFlags *flags,
31+
const char *enc,
2932
int *errcode);
3033

3134
#ifdef __cplusplus

Lib/test/test_flufl.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from test import support
55

66

7-
@support.skip_if_new_parser("Not supported by pegen yet")
87
class FLUFLTests(unittest.TestCase):
98

109
def test_barry_as_bdfl(self):
@@ -16,21 +15,26 @@ def test_barry_as_bdfl(self):
1615
__future__.CO_FUTURE_BARRY_AS_BDFL)
1716
self.assertRegex(str(cm.exception),
1817
"with Barry as BDFL, use '<>' instead of '!='")
19-
self.assertEqual(cm.exception.text, '2 != 3\n')
18+
self.assertIn('2 != 3', cm.exception.text)
2019
self.assertEqual(cm.exception.filename, '<FLUFL test>')
21-
self.assertEqual(cm.exception.lineno, 2)
22-
self.assertEqual(cm.exception.offset, 4)
20+
21+
self.assertTrue(cm.exception.lineno, 2)
22+
# The old parser reports the end of the token and the new
23+
# parser reports the start of the token
24+
self.assertEqual(cm.exception.offset, 4 if support.use_old_parser() else 3)
2325

2426
def test_guido_as_bdfl(self):
2527
code = '2 {0} 3'
2628
compile(code.format('!='), '<BDFL test>', 'exec')
2729
with self.assertRaises(SyntaxError) as cm:
2830
compile(code.format('<>'), '<FLUFL test>', 'exec')
2931
self.assertRegex(str(cm.exception), "invalid syntax")
30-
self.assertEqual(cm.exception.text, '2 <> 3\n')
32+
self.assertIn('2 <> 3', cm.exception.text)
3133
self.assertEqual(cm.exception.filename, '<FLUFL test>')
3234
self.assertEqual(cm.exception.lineno, 1)
33-
self.assertEqual(cm.exception.offset, 4)
35+
# The old parser reports the end of the token and the new
36+
# parser reports the start of the token
37+
self.assertEqual(cm.exception.offset, 4 if support.use_old_parser() else 3)
3438

3539

3640
if __name__ == '__main__':

Modules/_peg_parser.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ _Py_parse_file(PyObject *self, PyObject *args, PyObject *kwds)
2828
return NULL;
2929
}
3030

31+
PyCompilerFlags flags = _PyCompilerFlags_INIT;
3132
PyObject *result = NULL;
3233

33-
mod_ty res = PyPegen_ASTFromFile(filename, mode, arena);
34+
mod_ty res = PyPegen_ASTFromFile(filename, mode, &flags, arena);
3435
if (res == NULL) {
3536
goto error;
3637
}

0 commit comments

Comments
 (0)