mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	Fix SF bug #1519018: 'as' is now validated properly in import statements
This commit is contained in:
		
							parent
							
								
									b6b175229b
								
							
						
					
					
						commit
						fb48afa708
					
				
					 3 changed files with 18 additions and 1 deletions
				
			
		|  | @ -238,6 +238,8 @@ def test_import(self): | ||||||
|         succeed = [ |         succeed = [ | ||||||
|             'import sys', |             'import sys', | ||||||
|             'import os, sys', |             'import os, sys', | ||||||
|  |             'import os as bar', | ||||||
|  |             'import os.path as bar', | ||||||
|             'from __future__ import nested_scopes, generators', |             'from __future__ import nested_scopes, generators', | ||||||
|             'from __future__ import (nested_scopes,\ngenerators)', |             'from __future__ import (nested_scopes,\ngenerators)', | ||||||
|             'from __future__ import (nested_scopes,\ngenerators,)', |             'from __future__ import (nested_scopes,\ngenerators,)', | ||||||
|  | @ -257,6 +259,8 @@ def test_import(self): | ||||||
|             'import (sys', |             'import (sys', | ||||||
|             'import sys)', |             'import sys)', | ||||||
|             'import (os,)', |             'import (os,)', | ||||||
|  |             'import os As bar', | ||||||
|  |             'import os.path a bar', | ||||||
|             'from (sys) import stdin', |             'from (sys) import stdin', | ||||||
|             'from __future__ import (nested_scopes', |             'from __future__ import (nested_scopes', | ||||||
|             'from __future__ import nested_scopes)', |             'from __future__ import nested_scopes)', | ||||||
|  |  | ||||||
|  | @ -22,6 +22,8 @@ Core and builtins | ||||||
|   omit a default "error" argument for NULL pointer.  This allows |   omit a default "error" argument for NULL pointer.  This allows | ||||||
|   the parser to take a codec from cjkcodecs again. |   the parser to take a codec from cjkcodecs again. | ||||||
| 
 | 
 | ||||||
|  | - Bug #1519018: 'as' is now validated properly in import statements. | ||||||
|  | 
 | ||||||
| Library | Library | ||||||
| ------- | ------- | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										13
									
								
								Python/ast.c
									
										
									
									
									
								
							
							
						
						
									
										13
									
								
								Python/ast.c
									
										
									
									
									
								
							|  | @ -2142,7 +2142,14 @@ 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: | ||||||
|             str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL; |             str = NULL; | ||||||
|  |             if (NCH(n) == 3) { | ||||||
|  |                 if (strcmp(STR(CHILD(n, 1)), "as") != 0) { | ||||||
|  |                     ast_error(n, "must use 'as' in import"); | ||||||
|  |                     return NULL; | ||||||
|  |                 } | ||||||
|  |                 str = NEW_IDENTIFIER(CHILD(n, 2)); | ||||||
|  |             } | ||||||
|             return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena); |             return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena); | ||||||
|         case dotted_as_name: |         case dotted_as_name: | ||||||
|             if (NCH(n) == 1) { |             if (NCH(n) == 1) { | ||||||
|  | @ -2151,6 +2158,10 @@ alias_for_import_name(struct compiling *c, const node *n) | ||||||
|             } |             } | ||||||
|             else { |             else { | ||||||
|                 alias_ty a = alias_for_import_name(c, CHILD(n, 0)); |                 alias_ty a = alias_for_import_name(c, CHILD(n, 0)); | ||||||
|  |                 if (strcmp(STR(CHILD(n, 1)), "as") != 0) { | ||||||
|  |                     ast_error(n, "must use 'as' in import"); | ||||||
|  |                     return NULL; | ||||||
|  |                 } | ||||||
|                 assert(!a->asname); |                 assert(!a->asname); | ||||||
|                 a->asname = NEW_IDENTIFIER(CHILD(n, 2)); |                 a->asname = NEW_IDENTIFIER(CHILD(n, 2)); | ||||||
|                 return a; |                 return a; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Neal Norwitz
						Neal Norwitz