Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71ebc33

Browse files
committed
Fix _convert_NAME() so that it doesn't store locals for class bodies.
Fix list comp code generation -- emit GET_ITER instead of Const(0) after the list. Add CO_GENERATOR flag to generators. Get CO_xxx flags from the new module
1 parent 017cb2c commit 71ebc33

6 files changed

Lines changed: 30 additions & 30 deletions

File tree

Lib/compiler/consts.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# code flags
2-
CO_VARARGS = 1
3-
CO_VARKEYWORDS = 2
1+
from new import * # import all the CO_xxx flags
2+
del classobj, code, function, instance, instancemethod, module
43

54
# operation flags
65
OP_ASSIGN = 'OP_ASSIGN'

Lib/compiler/pyassem.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import types
88

99
from compiler import misc
10+
from compiler.consts import CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, \
11+
CO_VARKEYWORDS
1012

1113
def xxx_sort(l):
1214
l = l[:]
@@ -311,11 +313,6 @@ def getContainedGraphs(self):
311313
return contained
312314

313315
# flags for code objects
314-
CO_OPTIMIZED = 0x0001
315-
CO_NEWLOCALS = 0x0002
316-
CO_VARARGS = 0x0004
317-
CO_VARKEYWORDS = 0x0008
318-
CO_NESTED = 0x0010
319316

320317
# the FlowGraph is transformed in place; it exists in one of these states
321318
RAW = "RAW"
@@ -503,7 +500,8 @@ def _convert_LOAD_NAME(self, arg):
503500
return self._lookupName(arg, self.names)
504501

505502
def _convert_NAME(self, arg):
506-
self._lookupName(arg, self.varnames)
503+
if self.klass is None:
504+
self._lookupName(arg, self.varnames)
507505
return self._lookupName(arg, self.names)
508506
_convert_STORE_NAME = _convert_NAME
509507
_convert_DELETE_NAME = _convert_NAME
@@ -739,9 +737,8 @@ def findDepth(self, insts):
739737
'DELETE_SUBSCR': -2,
740738
# PRINT_EXPR?
741739
'PRINT_ITEM': -1,
742-
'LOAD_LOCALS': 1,
743740
'RETURN_VALUE': -1,
744-
'EXEC_STMT': -2,
741+
'EXEC_STMT': -3,
745742
'BUILD_CLASS': -2,
746743
'STORE_NAME': -1,
747744
'STORE_ATTR': -2,
@@ -756,6 +753,7 @@ def findDepth(self, insts):
756753
# close enough...
757754
'SETUP_EXCEPT': 3,
758755
'SETUP_FINALLY': 3,
756+
'FOR_ITER': 1,
759757
}
760758
# use pattern match
761759
patterns = [

Lib/compiler/pycodegen.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
from compiler import ast, parse, walk
1212
from compiler import pyassem, misc, future, symbols
1313
from compiler.consts import SC_LOCAL, SC_GLOBAL, SC_FREE, SC_CELL
14-
from compiler.pyassem import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS,\
15-
CO_NESTED, TupleArg
14+
from compiler.consts import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS,\
15+
CO_NESTED, CO_GENERATOR
16+
from compiler.pyassem import TupleArg
1617

1718
# Do we have Python 1.x or Python 2.x?
1819
try:
@@ -495,10 +496,10 @@ def visitListCompFor(self, node):
495496
anchor = self.newBlock()
496497

497498
self.visit(node.list)
498-
self.visit(ast.Const(0))
499+
self.emit('GET_ITER')
499500
self.nextBlock(start)
500501
self.emit('SET_LINENO', node.lineno)
501-
self.emit('FOR_LOOP', anchor)
502+
self.emit('FOR_ITER', anchor)
502503
self.nextBlock()
503504
self.visit(node.assign)
504505
return start, anchor
@@ -1199,6 +1200,8 @@ def __init__(self, func, filename, scopes, isLambda, class_name):
11991200
self.__super_init(func, filename, scopes, isLambda, class_name)
12001201
self.graph.setFreeVars(self.scope.get_free_vars())
12011202
self.graph.setCellVars(self.scope.get_cell_vars())
1203+
if self.scope.generator is not None:
1204+
self.graph.setFlag(CO_GENERATOR)
12021205
## self.graph.setFlag(CO_NESTED)
12031206

12041207
class AbstractClassCode:

Tools/compiler/compiler/consts.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# code flags
2-
CO_VARARGS = 1
3-
CO_VARKEYWORDS = 2
1+
from new import * # import all the CO_xxx flags
2+
del classobj, code, function, instance, instancemethod, module
43

54
# operation flags
65
OP_ASSIGN = 'OP_ASSIGN'

Tools/compiler/compiler/pyassem.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import types
88

99
from compiler import misc
10+
from compiler.consts import CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, \
11+
CO_VARKEYWORDS
1012

1113
def xxx_sort(l):
1214
l = l[:]
@@ -311,11 +313,6 @@ def getContainedGraphs(self):
311313
return contained
312314

313315
# flags for code objects
314-
CO_OPTIMIZED = 0x0001
315-
CO_NEWLOCALS = 0x0002
316-
CO_VARARGS = 0x0004
317-
CO_VARKEYWORDS = 0x0008
318-
CO_NESTED = 0x0010
319316

320317
# the FlowGraph is transformed in place; it exists in one of these states
321318
RAW = "RAW"
@@ -503,7 +500,8 @@ def _convert_LOAD_NAME(self, arg):
503500
return self._lookupName(arg, self.names)
504501

505502
def _convert_NAME(self, arg):
506-
self._lookupName(arg, self.varnames)
503+
if self.klass is None:
504+
self._lookupName(arg, self.varnames)
507505
return self._lookupName(arg, self.names)
508506
_convert_STORE_NAME = _convert_NAME
509507
_convert_DELETE_NAME = _convert_NAME
@@ -739,9 +737,8 @@ def findDepth(self, insts):
739737
'DELETE_SUBSCR': -2,
740738
# PRINT_EXPR?
741739
'PRINT_ITEM': -1,
742-
'LOAD_LOCALS': 1,
743740
'RETURN_VALUE': -1,
744-
'EXEC_STMT': -2,
741+
'EXEC_STMT': -3,
745742
'BUILD_CLASS': -2,
746743
'STORE_NAME': -1,
747744
'STORE_ATTR': -2,
@@ -756,6 +753,7 @@ def findDepth(self, insts):
756753
# close enough...
757754
'SETUP_EXCEPT': 3,
758755
'SETUP_FINALLY': 3,
756+
'FOR_ITER': 1,
759757
}
760758
# use pattern match
761759
patterns = [

Tools/compiler/compiler/pycodegen.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
from compiler import ast, parse, walk
1212
from compiler import pyassem, misc, future, symbols
1313
from compiler.consts import SC_LOCAL, SC_GLOBAL, SC_FREE, SC_CELL
14-
from compiler.pyassem import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS,\
15-
CO_NESTED, TupleArg
14+
from compiler.consts import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS,\
15+
CO_NESTED, CO_GENERATOR
16+
from compiler.pyassem import TupleArg
1617

1718
# Do we have Python 1.x or Python 2.x?
1819
try:
@@ -495,10 +496,10 @@ def visitListCompFor(self, node):
495496
anchor = self.newBlock()
496497

497498
self.visit(node.list)
498-
self.visit(ast.Const(0))
499+
self.emit('GET_ITER')
499500
self.nextBlock(start)
500501
self.emit('SET_LINENO', node.lineno)
501-
self.emit('FOR_LOOP', anchor)
502+
self.emit('FOR_ITER', anchor)
502503
self.nextBlock()
503504
self.visit(node.assign)
504505
return start, anchor
@@ -1199,6 +1200,8 @@ def __init__(self, func, filename, scopes, isLambda, class_name):
11991200
self.__super_init(func, filename, scopes, isLambda, class_name)
12001201
self.graph.setFreeVars(self.scope.get_free_vars())
12011202
self.graph.setCellVars(self.scope.get_cell_vars())
1203+
if self.scope.generator is not None:
1204+
self.graph.setFlag(CO_GENERATOR)
12021205
## self.graph.setFlag(CO_NESTED)
12031206

12041207
class AbstractClassCode:

0 commit comments

Comments
 (0)