mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Issue #999042: The Python compiler now handles explict global statements
correctly (should be assigned using STORE_GLOBAL opcode). This was done by having the system table differentiate between explict and implicit globals.
This commit is contained in:
		
							parent
							
								
									98c3b85bc4
								
							
						
					
					
						commit
						92c3b2190b
					
				
					 4 changed files with 18 additions and 10 deletions
				
			
		|  | @ -4,10 +4,11 @@ | ||||||
| OP_APPLY = 'OP_APPLY' | OP_APPLY = 'OP_APPLY' | ||||||
| 
 | 
 | ||||||
| SC_LOCAL = 1 | SC_LOCAL = 1 | ||||||
| SC_GLOBAL = 2 | SC_GLOBAL_IMPLICIT = 2 | ||||||
| SC_FREE = 3 | SC_GLOBAL_EXPLICT = 3 | ||||||
| SC_CELL = 4 | SC_FREE = 4 | ||||||
| SC_UNKNOWN = 5 | SC_CELL = 5 | ||||||
|  | SC_UNKNOWN = 6 | ||||||
| 
 | 
 | ||||||
| CO_OPTIMIZED = 0x0001 | CO_OPTIMIZED = 0x0001 | ||||||
| CO_NEWLOCALS = 0x0002 | CO_NEWLOCALS = 0x0002 | ||||||
|  |  | ||||||
|  | @ -7,7 +7,8 @@ | ||||||
| 
 | 
 | ||||||
| from compiler import ast, parse, walk, syntax | from compiler import ast, parse, walk, syntax | ||||||
| from compiler import pyassem, misc, future, symbols | from compiler import pyassem, misc, future, symbols | ||||||
| from compiler.consts import SC_LOCAL, SC_GLOBAL, SC_FREE, SC_CELL | from compiler.consts import SC_LOCAL, SC_GLOBAL_IMPLICIT, SC_GLOBAL_EXPLICT, \ | ||||||
|  |      SC_FREE, SC_CELL | ||||||
| from compiler.consts import (CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS, | from compiler.consts import (CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS, | ||||||
|      CO_NESTED, CO_GENERATOR, CO_FUTURE_DIVISION, |      CO_NESTED, CO_GENERATOR, CO_FUTURE_DIVISION, | ||||||
|      CO_FUTURE_ABSIMPORT, CO_FUTURE_WITH_STATEMENT, CO_FUTURE_PRINT_FUNCTION) |      CO_FUTURE_ABSIMPORT, CO_FUTURE_WITH_STATEMENT, CO_FUTURE_PRINT_FUNCTION) | ||||||
|  | @ -282,7 +283,9 @@ def _nameOp(self, prefix, name): | ||||||
|                 self.emit(prefix + '_NAME', name) |                 self.emit(prefix + '_NAME', name) | ||||||
|             else: |             else: | ||||||
|                 self.emit(prefix + '_FAST', name) |                 self.emit(prefix + '_FAST', name) | ||||||
|         elif scope == SC_GLOBAL: |         elif scope == SC_GLOBAL_EXPLICT: | ||||||
|  |             self.emit(prefix + '_GLOBAL', name) | ||||||
|  |         elif scope == SC_GLOBAL_IMPLICIT: | ||||||
|             if not self.optimized: |             if not self.optimized: | ||||||
|                 self.emit(prefix + '_NAME', name) |                 self.emit(prefix + '_NAME', name) | ||||||
|             else: |             else: | ||||||
|  |  | ||||||
|  | @ -1,7 +1,8 @@ | ||||||
| """Module symbol-table generator""" | """Module symbol-table generator""" | ||||||
| 
 | 
 | ||||||
| from compiler import ast | from compiler import ast | ||||||
| from compiler.consts import SC_LOCAL, SC_GLOBAL, SC_FREE, SC_CELL, SC_UNKNOWN | from compiler.consts import SC_LOCAL, SC_GLOBAL_IMPLICIT, SC_GLOBAL_EXPLICT, \ | ||||||
|  |     SC_FREE, SC_CELL, SC_UNKNOWN | ||||||
| from compiler.misc import mangle | from compiler.misc import mangle | ||||||
| import types | import types | ||||||
| 
 | 
 | ||||||
|  | @ -89,7 +90,7 @@ def check_name(self, name): | ||||||
|         The scope of a name could be LOCAL, GLOBAL, FREE, or CELL. |         The scope of a name could be LOCAL, GLOBAL, FREE, or CELL. | ||||||
|         """ |         """ | ||||||
|         if name in self.globals: |         if name in self.globals: | ||||||
|             return SC_GLOBAL |             return SC_GLOBAL_EXPLICT | ||||||
|         if name in self.cells: |         if name in self.cells: | ||||||
|             return SC_CELL |             return SC_CELL | ||||||
|         if name in self.defs: |         if name in self.defs: | ||||||
|  | @ -99,7 +100,7 @@ def check_name(self, name): | ||||||
|         if self.nested: |         if self.nested: | ||||||
|             return SC_UNKNOWN |             return SC_UNKNOWN | ||||||
|         else: |         else: | ||||||
|             return SC_GLOBAL |             return SC_GLOBAL_IMPLICIT | ||||||
| 
 | 
 | ||||||
|     def get_free_vars(self): |     def get_free_vars(self): | ||||||
|         if not self.nested: |         if not self.nested: | ||||||
|  | @ -152,7 +153,7 @@ def add_frees(self, names): | ||||||
|                 if sc == SC_UNKNOWN or sc == SC_FREE \ |                 if sc == SC_UNKNOWN or sc == SC_FREE \ | ||||||
|                    or isinstance(self, ClassScope): |                    or isinstance(self, ClassScope): | ||||||
|                     self.frees[name] = 1 |                     self.frees[name] = 1 | ||||||
|                 elif sc == SC_GLOBAL: |                 elif sc == SC_GLOBAL_IMPLICIT: | ||||||
|                     child_globals.append(name) |                     child_globals.append(name) | ||||||
|                 elif isinstance(self, FunctionScope) and sc == SC_LOCAL: |                 elif isinstance(self, FunctionScope) and sc == SC_LOCAL: | ||||||
|                     self.cells[name] = 1 |                     self.cells[name] = 1 | ||||||
|  |  | ||||||
|  | @ -410,6 +410,9 @@ Library | ||||||
|   protocol numbers are supplied outside the allowed 0-65536 range on bind() |   protocol numbers are supplied outside the allowed 0-65536 range on bind() | ||||||
|   and getservbyport(). |   and getservbyport(). | ||||||
| 
 | 
 | ||||||
|  | - Issue #999042: The Python compiler now handles explict global statements | ||||||
|  |   correctly (should be assigned using STORE_GLOBAL opcode). | ||||||
|  | 
 | ||||||
| Tools/Demos | Tools/Demos | ||||||
| ----------- | ----------- | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Neil Schemenauer
						Neil Schemenauer