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

Skip to content

Commit 3ac99d4

Browse files
committed
Add warnings for assignment or deletion of variables and attributes
named 'None'. Still to do: function definition parameter lists, and function call keyword arguments.
1 parent b081e0c commit 3ac99d4

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Python/compile.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ block_pop(struct compiling *c, int type)
574574

575575
/* Prototype forward declarations */
576576

577+
static int issue_warning(char *, char *, int);
577578
static int com_init(struct compiling *, char *);
578579
static void com_free(struct compiling *);
579580
static void com_push(struct compiling *, int);
@@ -988,6 +989,23 @@ com_lookup_arg(PyObject *dict, PyObject *name)
988989
return PyInt_AS_LONG(v);
989990
}
990991

992+
static int
993+
none_assignment_check(struct compiling *c, char *name, int assigning)
994+
{
995+
if (name[0] == 'N' && strcmp(name, "None") == 0) {
996+
char *msg;
997+
if (assigning)
998+
msg = "assigment to None";
999+
else
1000+
msg = "deleting None";
1001+
if (issue_warning(msg, c->c_filename, c->c_lineno) < 0) {
1002+
c->c_errors++;
1003+
return -1;
1004+
}
1005+
}
1006+
return 0;
1007+
}
1008+
9911009
static void
9921010
com_addop_varname(struct compiling *c, int kind, char *name)
9931011
{
@@ -997,6 +1015,13 @@ com_addop_varname(struct compiling *c, int kind, char *name)
9971015
int op = STOP_CODE;
9981016
char buffer[MANGLE_LEN];
9991017

1018+
if (kind != VAR_LOAD &&
1019+
none_assignment_check(c, name, kind == VAR_STORE))
1020+
{
1021+
c->c_errors++;
1022+
i = 255;
1023+
goto done;
1024+
}
10001025
if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
10011026
name = buffer;
10021027
if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
@@ -2489,6 +2514,8 @@ com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
24892514
static void
24902515
com_assign_attr(struct compiling *c, node *n, int assigning)
24912516
{
2517+
if (none_assignment_check(c, STR(n), assigning))
2518+
return;
24922519
com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
24932520
com_pop(c, assigning ? 2 : 1);
24942521
}

0 commit comments

Comments
 (0)