[3.9] bpo-40939: Fix test_keyword for the old parser (GH-20814)

This commit is contained in:
Pablo Galindo 2020-06-11 19:29:13 +01:00 committed by GitHub
parent dc40105c88
commit 3782497cc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 11 deletions

View file

@ -477,7 +477,7 @@ atom[expr_ty]:
| 'True' { _Py_Constant(Py_True, NULL, EXTRA) } | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
| 'False' { _Py_Constant(Py_False, NULL, EXTRA) } | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
| 'None' { _Py_Constant(Py_None, 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 | &STRING strings
| NUMBER | NUMBER
| &'(' (tuple | group | genexp) | &'(' (tuple | group | genexp)

View file

@ -19,7 +19,7 @@ kwlist = [
'False', 'False',
'None', 'None',
'True', 'True',
'__new_parser__', '__peg_parser__',
'and', 'and',
'as', 'as',
'assert', 'assert',

View file

@ -1817,7 +1817,7 @@ class Helper:
'False': '', 'False': '',
'None': '', 'None': '',
'True': '', 'True': '',
'__new_parser__': '', '__peg_parser__': '',
'and': 'BOOLEAN', 'and': 'BOOLEAN',
'as': 'with', 'as': 'with',
'assert': ('assert', ''), 'assert': ('assert', ''),

View file

@ -1,5 +1,6 @@
import keyword import keyword
import unittest import unittest
from test.support import use_old_parser
class Test_iskeyword(unittest.TestCase): class Test_iskeyword(unittest.TestCase):
@ -21,7 +22,10 @@ class Test_iskeyword(unittest.TestCase):
self.assertFalse(keyword.iskeyword('eggs')) self.assertFalse(keyword.iskeyword('eggs'))
def test_all_keywords_fail_to_be_used_as_names(self): 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): with self.assertRaises(SyntaxError):
exec(f"{key} = 42") exec(f"{key} = 42")

View file

@ -71,7 +71,7 @@ static KeywordToken *reserved_keywords[] = {
NULL, NULL,
NULL, NULL,
(KeywordToken[]) { (KeywordToken[]) {
{"__new_parser__", 530}, {"__peg_parser__", 530},
{NULL, -1}, {NULL, -1},
}, },
}; };
@ -10567,7 +10567,7 @@ slice_rule(Parser *p)
// | 'True' // | 'True'
// | 'False' // | 'False'
// | 'None' // | 'None'
// | '__new_parser__' // | '__peg_parser__'
// | &STRING strings // | &STRING strings
// | NUMBER // | NUMBER
// | &'(' (tuple | group | genexp) // | &'(' (tuple | group | genexp)
@ -10711,18 +10711,18 @@ atom_rule(Parser *p)
D(fprintf(stderr, "%*c%s atom[%d-%d]: %s failed!\n", p->level, ' ', D(fprintf(stderr, "%*c%s atom[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'None'")); p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'None'"));
} }
{ // '__new_parser__' { // '__peg_parser__'
if (p->error_indicator) { if (p->error_indicator) {
D(p->level--); D(p->level--);
return NULL; 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; Token * _keyword;
if ( 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!" ); _res = RAISE_SYNTAX_ERROR ( "You found it!" );
if (_res == NULL && PyErr_Occurred()) { if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1; p->error_indicator = 1;
@ -10733,7 +10733,7 @@ atom_rule(Parser *p)
} }
p->mark = _mark; p->mark = _mark;
D(fprintf(stderr, "%*c%s atom[%d-%d]: %s failed!\n", p->level, ' ', 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 { // &STRING strings
if (p->error_indicator) { if (p->error_indicator) {