Skip to content

Commit 91f2950

Browse files
author
neil.schemenauer
committed
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. git-svn-id: http://svn.python.org/projects/python/trunk@69394 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent a8ee0eb commit 91f2950

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

Lib/compiler/consts.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
OP_APPLY = 'OP_APPLY'
55

66
SC_LOCAL = 1
7-
SC_GLOBAL = 2
8-
SC_FREE = 3
9-
SC_CELL = 4
10-
SC_UNKNOWN = 5
7+
SC_GLOBAL_IMPLICIT = 2
8+
SC_GLOBAL_EXPLICT = 3
9+
SC_FREE = 4
10+
SC_CELL = 5
11+
SC_UNKNOWN = 6
1112

1213
CO_OPTIMIZED = 0x0001
1314
CO_NEWLOCALS = 0x0002

Lib/compiler/pycodegen.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
from compiler import ast, parse, walk, syntax
99
from compiler import pyassem, misc, future, symbols
10-
from compiler.consts import SC_LOCAL, SC_GLOBAL, SC_FREE, SC_CELL
10+
from compiler.consts import SC_LOCAL, SC_GLOBAL_IMPLICIT, SC_GLOBAL_EXPLICT, \
11+
SC_FREE, SC_CELL
1112
from compiler.consts import (CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS,
1213
CO_NESTED, CO_GENERATOR, CO_FUTURE_DIVISION,
1314
CO_FUTURE_ABSIMPORT, CO_FUTURE_WITH_STATEMENT, CO_FUTURE_PRINT_FUNCTION)
@@ -282,7 +283,9 @@ def _nameOp(self, prefix, name):
282283
self.emit(prefix + '_NAME', name)
283284
else:
284285
self.emit(prefix + '_FAST', name)
285-
elif scope == SC_GLOBAL:
286+
elif scope == SC_GLOBAL_EXPLICT:
287+
self.emit(prefix + '_GLOBAL', name)
288+
elif scope == SC_GLOBAL_IMPLICIT:
286289
if not self.optimized:
287290
self.emit(prefix + '_NAME', name)
288291
else:

Lib/compiler/symbols.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Module symbol-table generator"""
22

33
from compiler import ast
4-
from compiler.consts import SC_LOCAL, SC_GLOBAL, SC_FREE, SC_CELL, SC_UNKNOWN
4+
from compiler.consts import SC_LOCAL, SC_GLOBAL_IMPLICIT, SC_GLOBAL_EXPLICT, \
5+
SC_FREE, SC_CELL, SC_UNKNOWN
56
from compiler.misc import mangle
67
import types
78

@@ -89,7 +90,7 @@ def check_name(self, name):
8990
The scope of a name could be LOCAL, GLOBAL, FREE, or CELL.
9091
"""
9192
if name in self.globals:
92-
return SC_GLOBAL
93+
return SC_GLOBAL_EXPLICT
9394
if name in self.cells:
9495
return SC_CELL
9596
if name in self.defs:
@@ -99,7 +100,7 @@ def check_name(self, name):
99100
if self.nested:
100101
return SC_UNKNOWN
101102
else:
102-
return SC_GLOBAL
103+
return SC_GLOBAL_IMPLICIT
103104

104105
def get_free_vars(self):
105106
if not self.nested:
@@ -152,7 +153,7 @@ def add_frees(self, names):
152153
if sc == SC_UNKNOWN or sc == SC_FREE \
153154
or isinstance(self, ClassScope):
154155
self.frees[name] = 1
155-
elif sc == SC_GLOBAL:
156+
elif sc == SC_GLOBAL_IMPLICIT:
156157
child_globals.append(name)
157158
elif isinstance(self, FunctionScope) and sc == SC_LOCAL:
158159
self.cells[name] = 1

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ Library
410410
protocol numbers are supplied outside the allowed 0-65536 range on bind()
411411
and getservbyport().
412412

413+
- Issue #999042: The Python compiler now handles explict global statements
414+
correctly (should be assigned using STORE_GLOBAL opcode).
415+
413416
Tools/Demos
414417
-----------
415418

0 commit comments

Comments
 (0)