Merge pull request #1213 from RustPython/import-syntax

Fix import syntax to require at least one imported symbol. Fixes #1211.
This commit is contained in:
Noah
2019-08-07 09:33:23 -05:00
committed by GitHub
2 changed files with 15 additions and 8 deletions

View File

@@ -202,18 +202,19 @@ RaiseStatement: ast::Statement = {
};
ImportStatement: ast::Statement = {
<location:@L> "import" <names: Comma<ImportPart<<DottedName>>>> => {
<location:@L> "import" <names: OneOrMore<ImportAsAlias<DottedName>>> => {
ast::Statement {
location,
node: ast::StatementType::Import { names },
}
},
<location:@L> "from" <n:ImportFromLocation> "import" <names: ImportAsNames> => {
<location:@L> "from" <source:ImportFromLocation> "import" <names: ImportAsNames> => {
let (level, module) = source;
ast::Statement {
location,
node: ast::StatementType::ImportFrom {
level: n.0,
module: n.1,
level,
module,
names
},
}
@@ -235,8 +236,8 @@ ImportDots: usize = {
};
ImportAsNames: Vec<ast::ImportSymbol> = {
<i:Comma<ImportPart<Identifier>>> => i,
"(" <i:Comma<ImportPart<Identifier>>> ")" => i,
<i:OneOrMore<ImportAsAlias<Identifier>>> => i,
"(" <i:OneOrMore<ImportAsAlias<Identifier>>> ")" => i,
"*" => {
// Star import all
vec![ast::ImportSymbol { symbol: "*".to_string(), alias: None }]
@@ -245,7 +246,7 @@ ImportAsNames: Vec<ast::ImportSymbol> = {
#[inline]
ImportPart<I>: ast::ImportSymbol = {
ImportAsAlias<I>: ast::ImportSymbol = {
<symbol:I> <a: ("as" Identifier)?> => ast::ImportSymbol { symbol, alias: a.map(|a| a.1) },
};
@@ -608,7 +609,7 @@ Decorator: ast::Expression = {
};
YieldExpr: ast::Expression = {
<location:@L> "yield" <value:TestList?> => ast::Expression {
<location:@L> "yield" <value:TestList?> => ast::Expression {
location,
node: ast::ExpressionType::Yield { value: value.map(Box::new) }
},

View File

@@ -64,3 +64,9 @@ with OverrideImportContext():
# pass
#else:
# raise AssertionError('X should not be imported')
from testutils import assertRaises
with assertRaises(SyntaxError):
exec('import')