Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11a70c7

Browse files
committed
Upgrade None assignment SyntaxWarning to a SyntaxError.
1 parent 910d8f1 commit 11a70c7

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

Lib/test/test_compile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ def test_sequence_unpacking_error(self):
138138
self.assertEqual(i, 1)
139139
self.assertEqual(j, -1)
140140

141+
def test_none_assignment(self):
142+
stmts = [
143+
'None = 0',
144+
'None += 0',
145+
'__builtins__.None = 0',
146+
'def None(): pass',
147+
'class None: pass',
148+
'(a, None) = 0, 0',
149+
'for None in range(10): pass',
150+
'def f(None): pass',
151+
]
152+
for stmt in stmts:
153+
stmt += "\n"
154+
self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
155+
self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
141156

142157
def test_main():
143158
test_support.run_unittest(TestSpecifics)

Python/compile.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,10 +1227,8 @@ none_assignment_check(struct compiling *c, char *name, int assigning)
12271227
msg = "assignment to None";
12281228
else
12291229
msg = "deleting None";
1230-
if (issue_warning(msg, c->c_filename, c->c_lineno) < 0) {
1231-
c->c_errors++;
1232-
return -1;
1233-
}
1230+
com_error(c, PyExc_SyntaxError, msg);
1231+
return -1;
12341232
}
12351233
return 0;
12361234
}
@@ -1247,7 +1245,6 @@ com_addop_varname(struct compiling *c, int kind, char *name)
12471245
if (kind != VAR_LOAD &&
12481246
none_assignment_check(c, name, kind == VAR_STORE))
12491247
{
1250-
c->c_errors++;
12511248
i = 255;
12521249
goto done;
12531250
}
@@ -5483,8 +5480,10 @@ symtable_add_def(struct symtable *st, char *name, int flag)
54835480
if ((flag & DEF_PARAM) && !(flag & DEF_INTUPLE) &&
54845481
*name == 'N' && strcmp(name, "None") == 0)
54855482
{
5486-
if (symtable_warn(st, "argument named None"))
5487-
return -1;
5483+
PyErr_SetString(PyExc_SyntaxError,
5484+
"Invalid syntax. Assignment to None.");
5485+
symtable_error(st, 0);
5486+
return -1;
54885487
}
54895488
if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
54905489
name = buffer;

0 commit comments

Comments
 (0)