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
This commit is contained in:
Jeremy Hylton 2001-08-30 20:25:55 +00:00
parent 017cb2c7d8
commit 71ebc3359b
6 changed files with 30 additions and 30 deletions

View file

@ -1,6 +1,5 @@
# code flags from new import * # import all the CO_xxx flags
CO_VARARGS = 1 del classobj, code, function, instance, instancemethod, module
CO_VARKEYWORDS = 2
# operation flags # operation flags
OP_ASSIGN = 'OP_ASSIGN' OP_ASSIGN = 'OP_ASSIGN'

View file

@ -7,6 +7,8 @@ import sys
import types import types
from compiler import misc from compiler import misc
from compiler.consts import CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, \
CO_VARKEYWORDS
def xxx_sort(l): def xxx_sort(l):
l = l[:] l = l[:]
@ -311,11 +313,6 @@ class Block:
return contained return contained
# flags for code objects # flags for code objects
CO_OPTIMIZED = 0x0001
CO_NEWLOCALS = 0x0002
CO_VARARGS = 0x0004
CO_VARKEYWORDS = 0x0008
CO_NESTED = 0x0010
# the FlowGraph is transformed in place; it exists in one of these states # the FlowGraph is transformed in place; it exists in one of these states
RAW = "RAW" RAW = "RAW"
@ -503,7 +500,8 @@ class PyFlowGraph(FlowGraph):
return self._lookupName(arg, self.names) return self._lookupName(arg, self.names)
def _convert_NAME(self, arg): def _convert_NAME(self, arg):
self._lookupName(arg, self.varnames) if self.klass is None:
self._lookupName(arg, self.varnames)
return self._lookupName(arg, self.names) return self._lookupName(arg, self.names)
_convert_STORE_NAME = _convert_NAME _convert_STORE_NAME = _convert_NAME
_convert_DELETE_NAME = _convert_NAME _convert_DELETE_NAME = _convert_NAME
@ -739,9 +737,8 @@ class StackDepthTracker:
'DELETE_SUBSCR': -2, 'DELETE_SUBSCR': -2,
# PRINT_EXPR? # PRINT_EXPR?
'PRINT_ITEM': -1, 'PRINT_ITEM': -1,
'LOAD_LOCALS': 1,
'RETURN_VALUE': -1, 'RETURN_VALUE': -1,
'EXEC_STMT': -2, 'EXEC_STMT': -3,
'BUILD_CLASS': -2, 'BUILD_CLASS': -2,
'STORE_NAME': -1, 'STORE_NAME': -1,
'STORE_ATTR': -2, 'STORE_ATTR': -2,
@ -756,6 +753,7 @@ class StackDepthTracker:
# close enough... # close enough...
'SETUP_EXCEPT': 3, 'SETUP_EXCEPT': 3,
'SETUP_FINALLY': 3, 'SETUP_FINALLY': 3,
'FOR_ITER': 1,
} }
# use pattern match # use pattern match
patterns = [ patterns = [

View file

@ -11,8 +11,9 @@ from cStringIO import StringIO
from compiler import ast, parse, walk from compiler import ast, parse, walk
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, SC_FREE, SC_CELL
from compiler.pyassem import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS,\ from compiler.consts import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS,\
CO_NESTED, TupleArg CO_NESTED, CO_GENERATOR
from compiler.pyassem import TupleArg
# Do we have Python 1.x or Python 2.x? # Do we have Python 1.x or Python 2.x?
try: try:
@ -495,10 +496,10 @@ class CodeGenerator:
anchor = self.newBlock() anchor = self.newBlock()
self.visit(node.list) self.visit(node.list)
self.visit(ast.Const(0)) self.emit('GET_ITER')
self.nextBlock(start) self.nextBlock(start)
self.emit('SET_LINENO', node.lineno) self.emit('SET_LINENO', node.lineno)
self.emit('FOR_LOOP', anchor) self.emit('FOR_ITER', anchor)
self.nextBlock() self.nextBlock()
self.visit(node.assign) self.visit(node.assign)
return start, anchor return start, anchor
@ -1199,6 +1200,8 @@ class NestedFunctionCodeGenerator(AbstractFunctionCode,
self.__super_init(func, filename, scopes, isLambda, class_name) self.__super_init(func, filename, scopes, isLambda, class_name)
self.graph.setFreeVars(self.scope.get_free_vars()) self.graph.setFreeVars(self.scope.get_free_vars())
self.graph.setCellVars(self.scope.get_cell_vars()) self.graph.setCellVars(self.scope.get_cell_vars())
if self.scope.generator is not None:
self.graph.setFlag(CO_GENERATOR)
## self.graph.setFlag(CO_NESTED) ## self.graph.setFlag(CO_NESTED)
class AbstractClassCode: class AbstractClassCode:

View file

@ -1,6 +1,5 @@
# code flags from new import * # import all the CO_xxx flags
CO_VARARGS = 1 del classobj, code, function, instance, instancemethod, module
CO_VARKEYWORDS = 2
# operation flags # operation flags
OP_ASSIGN = 'OP_ASSIGN' OP_ASSIGN = 'OP_ASSIGN'

View file

@ -7,6 +7,8 @@ import sys
import types import types
from compiler import misc from compiler import misc
from compiler.consts import CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, \
CO_VARKEYWORDS
def xxx_sort(l): def xxx_sort(l):
l = l[:] l = l[:]
@ -311,11 +313,6 @@ class Block:
return contained return contained
# flags for code objects # flags for code objects
CO_OPTIMIZED = 0x0001
CO_NEWLOCALS = 0x0002
CO_VARARGS = 0x0004
CO_VARKEYWORDS = 0x0008
CO_NESTED = 0x0010
# the FlowGraph is transformed in place; it exists in one of these states # the FlowGraph is transformed in place; it exists in one of these states
RAW = "RAW" RAW = "RAW"
@ -503,7 +500,8 @@ class PyFlowGraph(FlowGraph):
return self._lookupName(arg, self.names) return self._lookupName(arg, self.names)
def _convert_NAME(self, arg): def _convert_NAME(self, arg):
self._lookupName(arg, self.varnames) if self.klass is None:
self._lookupName(arg, self.varnames)
return self._lookupName(arg, self.names) return self._lookupName(arg, self.names)
_convert_STORE_NAME = _convert_NAME _convert_STORE_NAME = _convert_NAME
_convert_DELETE_NAME = _convert_NAME _convert_DELETE_NAME = _convert_NAME
@ -739,9 +737,8 @@ class StackDepthTracker:
'DELETE_SUBSCR': -2, 'DELETE_SUBSCR': -2,
# PRINT_EXPR? # PRINT_EXPR?
'PRINT_ITEM': -1, 'PRINT_ITEM': -1,
'LOAD_LOCALS': 1,
'RETURN_VALUE': -1, 'RETURN_VALUE': -1,
'EXEC_STMT': -2, 'EXEC_STMT': -3,
'BUILD_CLASS': -2, 'BUILD_CLASS': -2,
'STORE_NAME': -1, 'STORE_NAME': -1,
'STORE_ATTR': -2, 'STORE_ATTR': -2,
@ -756,6 +753,7 @@ class StackDepthTracker:
# close enough... # close enough...
'SETUP_EXCEPT': 3, 'SETUP_EXCEPT': 3,
'SETUP_FINALLY': 3, 'SETUP_FINALLY': 3,
'FOR_ITER': 1,
} }
# use pattern match # use pattern match
patterns = [ patterns = [

View file

@ -11,8 +11,9 @@ from cStringIO import StringIO
from compiler import ast, parse, walk from compiler import ast, parse, walk
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, SC_FREE, SC_CELL
from compiler.pyassem import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS,\ from compiler.consts import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS,\
CO_NESTED, TupleArg CO_NESTED, CO_GENERATOR
from compiler.pyassem import TupleArg
# Do we have Python 1.x or Python 2.x? # Do we have Python 1.x or Python 2.x?
try: try:
@ -495,10 +496,10 @@ class CodeGenerator:
anchor = self.newBlock() anchor = self.newBlock()
self.visit(node.list) self.visit(node.list)
self.visit(ast.Const(0)) self.emit('GET_ITER')
self.nextBlock(start) self.nextBlock(start)
self.emit('SET_LINENO', node.lineno) self.emit('SET_LINENO', node.lineno)
self.emit('FOR_LOOP', anchor) self.emit('FOR_ITER', anchor)
self.nextBlock() self.nextBlock()
self.visit(node.assign) self.visit(node.assign)
return start, anchor return start, anchor
@ -1199,6 +1200,8 @@ class NestedFunctionCodeGenerator(AbstractFunctionCode,
self.__super_init(func, filename, scopes, isLambda, class_name) self.__super_init(func, filename, scopes, isLambda, class_name)
self.graph.setFreeVars(self.scope.get_free_vars()) self.graph.setFreeVars(self.scope.get_free_vars())
self.graph.setCellVars(self.scope.get_cell_vars()) self.graph.setCellVars(self.scope.get_cell_vars())
if self.scope.generator is not None:
self.graph.setFlag(CO_GENERATOR)
## self.graph.setFlag(CO_NESTED) ## self.graph.setFlag(CO_NESTED)
class AbstractClassCode: class AbstractClassCode: