Skip to content
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
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ atom[expr_ty]:
| 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
| 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
| 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
| '__new_parser__' { RAISE_SYNTAX_ERROR("You found it!") }
| '__peg_parser__' { RAISE_SYNTAX_ERROR("You found it!") }
| &STRING strings
| NUMBER
| &'(' (tuple | group | genexp)
Expand Down
2 changes: 1 addition & 1 deletion Lib/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'False',
'None',
'True',
'__new_parser__',
'__peg_parser__',
'and',
'as',
'assert',
Expand Down
2 changes: 1 addition & 1 deletion Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ class Helper:
'False': '',
'None': '',
'True': '',
'__new_parser__': '',
'__peg_parser__': '',
'and': 'BOOLEAN',
'as': 'with',
'assert': ('assert', ''),
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_keyword.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import keyword
import unittest
from test.support import use_old_parser


class Test_iskeyword(unittest.TestCase):
Expand All @@ -21,7 +22,10 @@ def test_changing_the_kwlist_does_not_affect_iskeyword(self):
self.assertFalse(keyword.iskeyword('eggs'))

def test_all_keywords_fail_to_be_used_as_names(self):
for key in keyword.kwlist:
all_keywords = set(keyword.kwlist)
if use_old_parser():
all_keywords.discard('__peg_parser__')
for key in all_keywords:
with self.assertRaises(SyntaxError):
exec(f"{key} = 42")

Expand Down
14 changes: 7 additions & 7 deletions Parser/pegen/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static KeywordToken *reserved_keywords[] = {
NULL,
NULL,
(KeywordToken[]) {
{"__new_parser__", 530},
{"__peg_parser__", 530},
{NULL, -1},
},
};
Expand Down Expand Up @@ -10567,7 +10567,7 @@ slice_rule(Parser *p)
// | 'True'
// | 'False'
// | 'None'
// | '__new_parser__'
// | '__peg_parser__'
// | &STRING strings
// | NUMBER
// | &'(' (tuple | group | genexp)
Expand Down Expand Up @@ -10711,18 +10711,18 @@ atom_rule(Parser *p)
D(fprintf(stderr, "%*c%s atom[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'None'"));
}
{ // '__new_parser__'
{ // '__peg_parser__'
if (p->error_indicator) {
D(p->level--);
return NULL;
}
D(fprintf(stderr, "%*c> atom[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'__new_parser__'"));
D(fprintf(stderr, "%*c> atom[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'__peg_parser__'"));
Token * _keyword;
if (
(_keyword = _PyPegen_expect_token(p, 530)) // token='__new_parser__'
(_keyword = _PyPegen_expect_token(p, 530)) // token='__peg_parser__'
)
{
D(fprintf(stderr, "%*c+ atom[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'__new_parser__'"));
D(fprintf(stderr, "%*c+ atom[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'__peg_parser__'"));
_res = RAISE_SYNTAX_ERROR ( "You found it!" );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
Expand All @@ -10733,7 +10733,7 @@ atom_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s atom[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'__new_parser__'"));
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'__peg_parser__'"));
}
{ // &STRING strings
if (p->error_indicator) {
Expand Down