bpo-28936: Detect lexically first syntax error first (#4097) · python/cpython@8c83c23 (original) (raw)

`@@ -9,6 +9,12 @@

`

9

9

`#include "structmember.h"

`

10

10

``

11

11

`/* error strings used for warnings */

`

``

12

`+

#define GLOBAL_PARAM \

`

``

13

`+

"name '%U' is parameter and global"

`

``

14

+

``

15

`+

#define NONLOCAL_PARAM \

`

``

16

`+

"name '%U' is parameter and nonlocal"

`

``

17

+

12

18

`#define GLOBAL_AFTER_ASSIGN \

`

13

19

`"name '%U' is assigned to before global declaration"

`

14

20

``

`@@ -465,12 +471,6 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,

`

465

471

`PyObject *global)

`

466

472

`{

`

467

473

`if (flags & DEF_GLOBAL) {

`

468

``

`-

if (flags & DEF_PARAM) {

`

469

``

`-

PyErr_Format(PyExc_SyntaxError,

`

470

``

`-

"name '%U' is parameter and global",

`

471

``

`-

name);

`

472

``

`-

return error_at_directive(ste, name);

`

473

``

`-

}

`

474

474

`if (flags & DEF_NONLOCAL) {

`

475

475

`PyErr_Format(PyExc_SyntaxError,

`

476

476

`"name '%U' is nonlocal and global",

`

`@@ -485,12 +485,6 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,

`

485

485

`return 1;

`

486

486

` }

`

487

487

`if (flags & DEF_NONLOCAL) {

`

488

``

`-

if (flags & DEF_PARAM) {

`

489

``

`-

PyErr_Format(PyExc_SyntaxError,

`

490

``

`-

"name '%U' is parameter and nonlocal",

`

491

``

`-

name);

`

492

``

`-

return error_at_directive(ste, name);

`

493

``

`-

}

`

494

488

`if (!bound) {

`

495

489

`PyErr_Format(PyExc_SyntaxError,

`

496

490

`"nonlocal declaration not allowed at module level");

`

`@@ -1284,9 +1278,11 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)

`

1284

1278

`long cur = symtable_lookup(st, name);

`

1285

1279

`if (cur < 0)

`

1286

1280

`VISIT_QUIT(st, 0);

`

1287

``

`-

if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) {

`

1288

``

`-

char* msg;

`

1289

``

`-

if (cur & USE) {

`

``

1281

`+

if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {

`

``

1282

`+

const char* msg;

`

``

1283

`+

if (cur & DEF_PARAM) {

`

``

1284

`+

msg = GLOBAL_PARAM;

`

``

1285

`+

} else if (cur & USE) {

`

1290

1286

`msg = GLOBAL_AFTER_USE;

`

1291

1287

` } else if (cur & DEF_ANNOT) {

`

1292

1288

`msg = GLOBAL_ANNOT;

`

`@@ -1315,9 +1311,11 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)

`

1315

1311

`long cur = symtable_lookup(st, name);

`

1316

1312

`if (cur < 0)

`

1317

1313

`VISIT_QUIT(st, 0);

`

1318

``

`-

if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) {

`

1319

``

`-

char* msg;

`

1320

``

`-

if (cur & USE) {

`

``

1314

`+

if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {

`

``

1315

`+

const char* msg;

`

``

1316

`+

if (cur & DEF_PARAM) {

`

``

1317

`+

msg = NONLOCAL_PARAM;

`

``

1318

`+

} else if (cur & USE) {

`

1321

1319

`msg = NONLOCAL_AFTER_USE;

`

1322

1320

` } else if (cur & DEF_ANNOT) {

`

1323

1321

`msg = NONLOCAL_ANNOT;

`