mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
prevent import statements from assigning to None
This commit is contained in:
parent
4afbba3d34
commit
565e1b6bb7
3 changed files with 34 additions and 11 deletions
|
@ -270,11 +270,17 @@ if 1:
|
||||||
'(a, None) = 0, 0',
|
'(a, None) = 0, 0',
|
||||||
'for None in range(10): pass',
|
'for None in range(10): pass',
|
||||||
'def f(None): pass',
|
'def f(None): pass',
|
||||||
|
'import None',
|
||||||
|
'import x as None',
|
||||||
|
'from x import None',
|
||||||
|
'from x import y as None'
|
||||||
]
|
]
|
||||||
for stmt in stmts:
|
for stmt in stmts:
|
||||||
stmt += "\n"
|
stmt += "\n"
|
||||||
self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
|
self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
|
||||||
self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
|
self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
|
||||||
|
# This is ok.
|
||||||
|
compile("from None import x", "tmp", "exec")
|
||||||
|
|
||||||
def test_import(self):
|
def test_import(self):
|
||||||
succeed = [
|
succeed = [
|
||||||
|
|
|
@ -12,6 +12,8 @@ What's New in Python 2.7 alpha 1
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Assignment to None using import statements now raises a SyntaxError.
|
||||||
|
|
||||||
- In the slice AST type, the step field will always be None if a step expression
|
- In the slice AST type, the step field will always be None if a step expression
|
||||||
is not specified.
|
is not specified.
|
||||||
|
|
||||||
|
|
37
Python/ast.c
37
Python/ast.c
|
@ -2294,7 +2294,7 @@ ast_for_flow_stmt(struct compiling *c, const node *n)
|
||||||
}
|
}
|
||||||
|
|
||||||
static alias_ty
|
static alias_ty
|
||||||
alias_for_import_name(struct compiling *c, const node *n)
|
alias_for_import_name(struct compiling *c, const node *n, int store)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
import_as_name: NAME ['as' NAME]
|
import_as_name: NAME ['as' NAME]
|
||||||
|
@ -2305,28 +2305,38 @@ alias_for_import_name(struct compiling *c, const node *n)
|
||||||
|
|
||||||
loop:
|
loop:
|
||||||
switch (TYPE(n)) {
|
switch (TYPE(n)) {
|
||||||
case import_as_name:
|
case import_as_name: {
|
||||||
|
node *name_node = CHILD(n, 0);
|
||||||
str = NULL;
|
str = NULL;
|
||||||
if (NCH(n) == 3) {
|
if (NCH(n) == 3) {
|
||||||
str = NEW_IDENTIFIER(CHILD(n, 2));
|
node *str_node = CHILD(n, 2);
|
||||||
|
if (store && !forbidden_check(c, str_node, STR(str_node)))
|
||||||
|
return NULL;
|
||||||
|
str = NEW_IDENTIFIER(str_node);
|
||||||
if (!str)
|
if (!str)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
name = NEW_IDENTIFIER(CHILD(n, 0));
|
if (!forbidden_check(c, name_node, STR(name_node)))
|
||||||
|
return NULL;
|
||||||
|
name = NEW_IDENTIFIER(name_node);
|
||||||
if (!name)
|
if (!name)
|
||||||
return NULL;
|
return NULL;
|
||||||
return alias(name, str, c->c_arena);
|
return alias(name, str, c->c_arena);
|
||||||
|
}
|
||||||
case dotted_as_name:
|
case dotted_as_name:
|
||||||
if (NCH(n) == 1) {
|
if (NCH(n) == 1) {
|
||||||
n = CHILD(n, 0);
|
n = CHILD(n, 0);
|
||||||
goto loop;
|
goto loop;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
alias_ty a = alias_for_import_name(c, CHILD(n, 0));
|
node *asname_node = CHILD(n, 2);
|
||||||
|
alias_ty a = alias_for_import_name(c, CHILD(n, 0), store);
|
||||||
if (!a)
|
if (!a)
|
||||||
return NULL;
|
return NULL;
|
||||||
assert(!a->asname);
|
assert(!a->asname);
|
||||||
a->asname = NEW_IDENTIFIER(CHILD(n, 2));
|
if (store && !forbidden_check(c, asname_node, STR(asname_node)))
|
||||||
|
return NULL;
|
||||||
|
a->asname = NEW_IDENTIFIER(asname_node);
|
||||||
if (!a->asname)
|
if (!a->asname)
|
||||||
return NULL;
|
return NULL;
|
||||||
return a;
|
return a;
|
||||||
|
@ -2334,7 +2344,10 @@ alias_for_import_name(struct compiling *c, const node *n)
|
||||||
break;
|
break;
|
||||||
case dotted_name:
|
case dotted_name:
|
||||||
if (NCH(n) == 1) {
|
if (NCH(n) == 1) {
|
||||||
name = NEW_IDENTIFIER(CHILD(n, 0));
|
node *name_node = CHILD(n, 0);
|
||||||
|
if (store && !forbidden_check(c, name_node, STR(name_node)))
|
||||||
|
return NULL;
|
||||||
|
name = NEW_IDENTIFIER(name_node);
|
||||||
if (!name)
|
if (!name)
|
||||||
return NULL;
|
return NULL;
|
||||||
return alias(name, NULL, c->c_arena);
|
return alias(name, NULL, c->c_arena);
|
||||||
|
@ -2408,7 +2421,7 @@ ast_for_import_stmt(struct compiling *c, const node *n)
|
||||||
if (!aliases)
|
if (!aliases)
|
||||||
return NULL;
|
return NULL;
|
||||||
for (i = 0; i < NCH(n); i += 2) {
|
for (i = 0; i < NCH(n); i += 2) {
|
||||||
alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
|
alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
|
||||||
if (!import_alias)
|
if (!import_alias)
|
||||||
return NULL;
|
return NULL;
|
||||||
asdl_seq_SET(aliases, i / 2, import_alias);
|
asdl_seq_SET(aliases, i / 2, import_alias);
|
||||||
|
@ -2425,7 +2438,9 @@ ast_for_import_stmt(struct compiling *c, const node *n)
|
||||||
optional module name */
|
optional module name */
|
||||||
for (idx = 1; idx < NCH(n); idx++) {
|
for (idx = 1; idx < NCH(n); idx++) {
|
||||||
if (TYPE(CHILD(n, idx)) == dotted_name) {
|
if (TYPE(CHILD(n, idx)) == dotted_name) {
|
||||||
mod = alias_for_import_name(c, CHILD(n, idx));
|
mod = alias_for_import_name(c, CHILD(n, idx), 0);
|
||||||
|
if (!mod)
|
||||||
|
return NULL;
|
||||||
idx++;
|
idx++;
|
||||||
break;
|
break;
|
||||||
} else if (TYPE(CHILD(n, idx)) != DOT) {
|
} else if (TYPE(CHILD(n, idx)) != DOT) {
|
||||||
|
@ -2466,14 +2481,14 @@ ast_for_import_stmt(struct compiling *c, const node *n)
|
||||||
|
|
||||||
/* handle "from ... import *" special b/c there's no children */
|
/* handle "from ... import *" special b/c there's no children */
|
||||||
if (TYPE(n) == STAR) {
|
if (TYPE(n) == STAR) {
|
||||||
alias_ty import_alias = alias_for_import_name(c, n);
|
alias_ty import_alias = alias_for_import_name(c, n, 1);
|
||||||
if (!import_alias)
|
if (!import_alias)
|
||||||
return NULL;
|
return NULL;
|
||||||
asdl_seq_SET(aliases, 0, import_alias);
|
asdl_seq_SET(aliases, 0, import_alias);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (i = 0; i < NCH(n); i += 2) {
|
for (i = 0; i < NCH(n); i += 2) {
|
||||||
alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
|
alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
|
||||||
if (!import_alias)
|
if (!import_alias)
|
||||||
return NULL;
|
return NULL;
|
||||||
asdl_seq_SET(aliases, i / 2, import_alias);
|
asdl_seq_SET(aliases, i / 2, import_alias);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue