Commit cdf610df authored by Jeremy Hylton's avatar Jeremy Hylton

satisfy the tabnanny (thanks to MH for noticing the problem)

parent 870be35e
...@@ -11,25 +11,25 @@ def flatten(tup): ...@@ -11,25 +11,25 @@ def flatten(tup):
class Set: class Set:
def __init__(self): def __init__(self):
self.elts = {} self.elts = {}
def __len__(self): def __len__(self):
return len(self.elts) return len(self.elts)
def add(self, elt): def add(self, elt):
self.elts[elt] = elt self.elts[elt] = elt
def items(self): def items(self):
return self.elts.keys() return self.elts.keys()
def has_elt(self, elt): def has_elt(self, elt):
return self.elts.has_key(elt) return self.elts.has_key(elt)
def remove(self, elt): def remove(self, elt):
del self.elts[elt] del self.elts[elt]
class Stack: class Stack:
def __init__(self): def __init__(self):
self.stack = [] self.stack = []
self.pop = self.stack.pop self.pop = self.stack.pop
def __len__(self): def __len__(self):
return len(self.stack) return len(self.stack)
def push(self, elt): def push(self, elt):
self.stack.append(elt) self.stack.append(elt)
def top(self): def top(self):
return self.stack[-1] return self.stack[-1]
...@@ -60,7 +60,7 @@ class PyAssembler: ...@@ -60,7 +60,7 @@ class PyAssembler:
def __init__(self, args=(), name='?', filename='<?>', def __init__(self, args=(), name='?', filename='<?>',
docstring=None): docstring=None):
# XXX why is the default value for flags 3? # XXX why is the default value for flags 3?
self.insts = [] self.insts = []
# used by makeCodeObject # used by makeCodeObject
self._getArgCount(args) self._getArgCount(args)
self.code = '' self.code = ''
...@@ -107,10 +107,10 @@ class PyAssembler: ...@@ -107,10 +107,10 @@ class PyAssembler:
self.flags = self.flags | CO_VARKEYWORDS self.flags = self.flags | CO_VARKEYWORDS
def getCurInst(self): def getCurInst(self):
return len(self.insts) return len(self.insts)
def getNextInst(self): def getNextInst(self):
return len(self.insts) + 1 return len(self.insts) + 1
def dump(self, io=sys.stdout): def dump(self, io=sys.stdout):
i = 0 i = 0
...@@ -162,7 +162,7 @@ class PyAssembler: ...@@ -162,7 +162,7 @@ class PyAssembler:
# XXX danger! can't pass through here twice # XXX danger! can't pass through here twice
if self.flags & CO_VARKEYWORDS: if self.flags & CO_VARKEYWORDS:
self.argcount = self.argcount - 1 self.argcount = self.argcount - 1
stacksize = findDepth(self.insts) stacksize = findDepth(self.insts)
try: try:
co = new.code(self.argcount, nlocals, stacksize, co = new.code(self.argcount, nlocals, stacksize,
self.flags, lnotab.getCode(), self._getConsts(), self.flags, lnotab.getCode(), self._getConsts(),
...@@ -193,9 +193,9 @@ class PyAssembler: ...@@ -193,9 +193,9 @@ class PyAssembler:
""" """
l = [] l = []
for elt in self.consts: for elt in self.consts:
# XXX might be clearer to just as isinstance(CodeGen) # XXX might be clearer to just as isinstance(CodeGen)
if hasattr(elt, 'asConst'): if hasattr(elt, 'asConst'):
l.append(elt.asConst()) l.append(elt.asConst())
else: else:
l.append(elt) l.append(elt)
return tuple(l) return tuple(l)
...@@ -286,14 +286,14 @@ class PyAssembler: ...@@ -286,14 +286,14 @@ class PyAssembler:
opnum = {} opnum = {}
for num in range(len(dis.opname)): for num in range(len(dis.opname)):
opnum[dis.opname[num]] = num opnum[dis.opname[num]] = num
# this version of emit + arbitrary hooks might work, but it's damn # this version of emit + arbitrary hooks might work, but it's damn
# messy. # messy.
def emit(self, *args): def emit(self, *args):
self._emitDispatch(args[0], args[1:]) self._emitDispatch(args[0], args[1:])
self.insts.append(args) self.insts.append(args)
def _emitDispatch(self, type, args): def _emitDispatch(self, type, args):
for func in self._emit_hooks.get(type, []): for func in self._emit_hooks.get(type, []):
...@@ -363,115 +363,115 @@ class StackRef: ...@@ -363,115 +363,115 @@ class StackRef:
count = 0 count = 0
def __init__(self, id=None, val=None): def __init__(self, id=None, val=None):
if id is None: if id is None:
id = StackRef.count id = StackRef.count
StackRef.count = StackRef.count + 1 StackRef.count = StackRef.count + 1
self.id = id self.id = id
self.val = val self.val = val
def __repr__(self): def __repr__(self):
if self.val: if self.val:
return "StackRef(val=%d)" % self.val return "StackRef(val=%d)" % self.val
else: else:
return "StackRef(id=%d)" % self.id return "StackRef(id=%d)" % self.id
def bind(self, inst): def bind(self, inst):
self.val = inst self.val = inst
def resolve(self): def resolve(self):
if self.val is None: if self.val is None:
print "UNRESOLVE REF", self print "UNRESOLVE REF", self
return 0 return 0
return self.val return self.val
class StackDepthTracker: class StackDepthTracker:
# XXX need to keep track of stack depth on jumps # XXX need to keep track of stack depth on jumps
def findDepth(self, insts): def findDepth(self, insts):
depth = 0 depth = 0
maxDepth = 0 maxDepth = 0
for i in insts: for i in insts:
opname = i[0] opname = i[0]
delta = self.effect.get(opname, 0) delta = self.effect.get(opname, 0)
if delta > 1: if delta > 1:
depth = depth + delta depth = depth + delta
elif delta < 0: elif delta < 0:
if depth > maxDepth: if depth > maxDepth:
maxDepth = depth maxDepth = depth
depth = depth + delta depth = depth + delta
else: else:
if depth > maxDepth: if depth > maxDepth:
maxDepth = depth maxDepth = depth
# now check patterns # now check patterns
for pat, delta in self.patterns: for pat, delta in self.patterns:
if opname[:len(pat)] == pat: if opname[:len(pat)] == pat:
depth = depth + delta depth = depth + delta
break break
# if we still haven't found a match # if we still haven't found a match
if delta == 0: if delta == 0:
meth = getattr(self, opname) meth = getattr(self, opname)
depth = depth + meth(i[1]) depth = depth + meth(i[1])
if depth < 0: if depth < 0:
depth = 0 depth = 0
return maxDepth return maxDepth
effect = { effect = {
'POP_TOP': -1, 'POP_TOP': -1,
'DUP_TOP': 1, 'DUP_TOP': 1,
'SLICE+1': -1, 'SLICE+1': -1,
'SLICE+2': -1, 'SLICE+2': -1,
'SLICE+3': -2, 'SLICE+3': -2,
'STORE_SLICE+0': -1, 'STORE_SLICE+0': -1,
'STORE_SLICE+1': -2, 'STORE_SLICE+1': -2,
'STORE_SLICE+2': -2, 'STORE_SLICE+2': -2,
'STORE_SLICE+3': -3, 'STORE_SLICE+3': -3,
'DELETE_SLICE+0': -1, 'DELETE_SLICE+0': -1,
'DELETE_SLICE+1': -2, 'DELETE_SLICE+1': -2,
'DELETE_SLICE+2': -2, 'DELETE_SLICE+2': -2,
'DELETE_SLICE+3': -3, 'DELETE_SLICE+3': -3,
'STORE_SUBSCR': -3, 'STORE_SUBSCR': -3,
'DELETE_SUBSCR': -2, 'DELETE_SUBSCR': -2,
# PRINT_EXPR? # PRINT_EXPR?
'PRINT_ITEM': -1, 'PRINT_ITEM': -1,
'LOAD_LOCALS': 1, 'LOAD_LOCALS': 1,
'RETURN_VALUE': -1, 'RETURN_VALUE': -1,
'EXEC_STMT': -2, 'EXEC_STMT': -2,
'BUILD_CLASS': -2, 'BUILD_CLASS': -2,
'STORE_NAME': -1, 'STORE_NAME': -1,
'STORE_ATTR': -2, 'STORE_ATTR': -2,
'DELETE_ATTR': -1, 'DELETE_ATTR': -1,
'STORE_GLOBAL': -1, 'STORE_GLOBAL': -1,
'BUILD_MAP': 1, 'BUILD_MAP': 1,
'COMPARE_OP': -1, 'COMPARE_OP': -1,
'STORE_FAST': -1, 'STORE_FAST': -1,
} }
# use pattern match # use pattern match
patterns = [ patterns = [
('BINARY_', -1), ('BINARY_', -1),
('LOAD_', 1), ('LOAD_', 1),
('IMPORT_', 1), ('IMPORT_', 1),
] ]
# special cases # special cases
#: UNPACK_TUPLE, UNPACK_LIST, BUILD_TUPLE, #: UNPACK_TUPLE, UNPACK_LIST, BUILD_TUPLE,
# BUILD_LIST, CALL_FUNCTION, MAKE_FUNCTION, BUILD_SLICE # BUILD_LIST, CALL_FUNCTION, MAKE_FUNCTION, BUILD_SLICE
def UNPACK_TUPLE(self, count): def UNPACK_TUPLE(self, count):
return count return count
def UNPACK_LIST(self, count): def UNPACK_LIST(self, count):
return count return count
def BUILD_TUPLE(self, count): def BUILD_TUPLE(self, count):
return -count return -count
def BUILD_LIST(self, count): def BUILD_LIST(self, count):
return -count return -count
def CALL_FUNCTION(self, argc): def CALL_FUNCTION(self, argc):
hi, lo = divmod(argc, 256) hi, lo = divmod(argc, 256)
return lo + hi * 2 return lo + hi * 2
def MAKE_FUNCTION(self, argc): def MAKE_FUNCTION(self, argc):
return -argc return -argc
def BUILD_SLICE(self, argc): def BUILD_SLICE(self, argc):
if argc == 2: if argc == 2:
return -1 return -1
elif argc == 3: elif argc == 3:
return -2 return -2
findDepth = StackDepthTracker().findDepth findDepth = StackDepthTracker().findDepth
...@@ -11,25 +11,25 @@ def flatten(tup): ...@@ -11,25 +11,25 @@ def flatten(tup):
class Set: class Set:
def __init__(self): def __init__(self):
self.elts = {} self.elts = {}
def __len__(self): def __len__(self):
return len(self.elts) return len(self.elts)
def add(self, elt): def add(self, elt):
self.elts[elt] = elt self.elts[elt] = elt
def items(self): def items(self):
return self.elts.keys() return self.elts.keys()
def has_elt(self, elt): def has_elt(self, elt):
return self.elts.has_key(elt) return self.elts.has_key(elt)
def remove(self, elt): def remove(self, elt):
del self.elts[elt] del self.elts[elt]
class Stack: class Stack:
def __init__(self): def __init__(self):
self.stack = [] self.stack = []
self.pop = self.stack.pop self.pop = self.stack.pop
def __len__(self): def __len__(self):
return len(self.stack) return len(self.stack)
def push(self, elt): def push(self, elt):
self.stack.append(elt) self.stack.append(elt)
def top(self): def top(self):
return self.stack[-1] return self.stack[-1]
...@@ -60,7 +60,7 @@ class PyAssembler: ...@@ -60,7 +60,7 @@ class PyAssembler:
def __init__(self, args=(), name='?', filename='<?>', def __init__(self, args=(), name='?', filename='<?>',
docstring=None): docstring=None):
# XXX why is the default value for flags 3? # XXX why is the default value for flags 3?
self.insts = [] self.insts = []
# used by makeCodeObject # used by makeCodeObject
self._getArgCount(args) self._getArgCount(args)
self.code = '' self.code = ''
...@@ -107,10 +107,10 @@ class PyAssembler: ...@@ -107,10 +107,10 @@ class PyAssembler:
self.flags = self.flags | CO_VARKEYWORDS self.flags = self.flags | CO_VARKEYWORDS
def getCurInst(self): def getCurInst(self):
return len(self.insts) return len(self.insts)
def getNextInst(self): def getNextInst(self):
return len(self.insts) + 1 return len(self.insts) + 1
def dump(self, io=sys.stdout): def dump(self, io=sys.stdout):
i = 0 i = 0
...@@ -162,7 +162,7 @@ class PyAssembler: ...@@ -162,7 +162,7 @@ class PyAssembler:
# XXX danger! can't pass through here twice # XXX danger! can't pass through here twice
if self.flags & CO_VARKEYWORDS: if self.flags & CO_VARKEYWORDS:
self.argcount = self.argcount - 1 self.argcount = self.argcount - 1
stacksize = findDepth(self.insts) stacksize = findDepth(self.insts)
try: try:
co = new.code(self.argcount, nlocals, stacksize, co = new.code(self.argcount, nlocals, stacksize,
self.flags, lnotab.getCode(), self._getConsts(), self.flags, lnotab.getCode(), self._getConsts(),
...@@ -193,9 +193,9 @@ class PyAssembler: ...@@ -193,9 +193,9 @@ class PyAssembler:
""" """
l = [] l = []
for elt in self.consts: for elt in self.consts:
# XXX might be clearer to just as isinstance(CodeGen) # XXX might be clearer to just as isinstance(CodeGen)
if hasattr(elt, 'asConst'): if hasattr(elt, 'asConst'):
l.append(elt.asConst()) l.append(elt.asConst())
else: else:
l.append(elt) l.append(elt)
return tuple(l) return tuple(l)
...@@ -286,14 +286,14 @@ class PyAssembler: ...@@ -286,14 +286,14 @@ class PyAssembler:
opnum = {} opnum = {}
for num in range(len(dis.opname)): for num in range(len(dis.opname)):
opnum[dis.opname[num]] = num opnum[dis.opname[num]] = num
# this version of emit + arbitrary hooks might work, but it's damn # this version of emit + arbitrary hooks might work, but it's damn
# messy. # messy.
def emit(self, *args): def emit(self, *args):
self._emitDispatch(args[0], args[1:]) self._emitDispatch(args[0], args[1:])
self.insts.append(args) self.insts.append(args)
def _emitDispatch(self, type, args): def _emitDispatch(self, type, args):
for func in self._emit_hooks.get(type, []): for func in self._emit_hooks.get(type, []):
...@@ -363,115 +363,115 @@ class StackRef: ...@@ -363,115 +363,115 @@ class StackRef:
count = 0 count = 0
def __init__(self, id=None, val=None): def __init__(self, id=None, val=None):
if id is None: if id is None:
id = StackRef.count id = StackRef.count
StackRef.count = StackRef.count + 1 StackRef.count = StackRef.count + 1
self.id = id self.id = id
self.val = val self.val = val
def __repr__(self): def __repr__(self):
if self.val: if self.val:
return "StackRef(val=%d)" % self.val return "StackRef(val=%d)" % self.val
else: else:
return "StackRef(id=%d)" % self.id return "StackRef(id=%d)" % self.id
def bind(self, inst): def bind(self, inst):
self.val = inst self.val = inst
def resolve(self): def resolve(self):
if self.val is None: if self.val is None:
print "UNRESOLVE REF", self print "UNRESOLVE REF", self
return 0 return 0
return self.val return self.val
class StackDepthTracker: class StackDepthTracker:
# XXX need to keep track of stack depth on jumps # XXX need to keep track of stack depth on jumps
def findDepth(self, insts): def findDepth(self, insts):
depth = 0 depth = 0
maxDepth = 0 maxDepth = 0
for i in insts: for i in insts:
opname = i[0] opname = i[0]
delta = self.effect.get(opname, 0) delta = self.effect.get(opname, 0)
if delta > 1: if delta > 1:
depth = depth + delta depth = depth + delta
elif delta < 0: elif delta < 0:
if depth > maxDepth: if depth > maxDepth:
maxDepth = depth maxDepth = depth
depth = depth + delta depth = depth + delta
else: else:
if depth > maxDepth: if depth > maxDepth:
maxDepth = depth maxDepth = depth
# now check patterns # now check patterns
for pat, delta in self.patterns: for pat, delta in self.patterns:
if opname[:len(pat)] == pat: if opname[:len(pat)] == pat:
depth = depth + delta depth = depth + delta
break break
# if we still haven't found a match # if we still haven't found a match
if delta == 0: if delta == 0:
meth = getattr(self, opname) meth = getattr(self, opname)
depth = depth + meth(i[1]) depth = depth + meth(i[1])
if depth < 0: if depth < 0:
depth = 0 depth = 0
return maxDepth return maxDepth
effect = { effect = {
'POP_TOP': -1, 'POP_TOP': -1,
'DUP_TOP': 1, 'DUP_TOP': 1,
'SLICE+1': -1, 'SLICE+1': -1,
'SLICE+2': -1, 'SLICE+2': -1,
'SLICE+3': -2, 'SLICE+3': -2,
'STORE_SLICE+0': -1, 'STORE_SLICE+0': -1,
'STORE_SLICE+1': -2, 'STORE_SLICE+1': -2,
'STORE_SLICE+2': -2, 'STORE_SLICE+2': -2,
'STORE_SLICE+3': -3, 'STORE_SLICE+3': -3,
'DELETE_SLICE+0': -1, 'DELETE_SLICE+0': -1,
'DELETE_SLICE+1': -2, 'DELETE_SLICE+1': -2,
'DELETE_SLICE+2': -2, 'DELETE_SLICE+2': -2,
'DELETE_SLICE+3': -3, 'DELETE_SLICE+3': -3,
'STORE_SUBSCR': -3, 'STORE_SUBSCR': -3,
'DELETE_SUBSCR': -2, 'DELETE_SUBSCR': -2,
# PRINT_EXPR? # PRINT_EXPR?
'PRINT_ITEM': -1, 'PRINT_ITEM': -1,
'LOAD_LOCALS': 1, 'LOAD_LOCALS': 1,
'RETURN_VALUE': -1, 'RETURN_VALUE': -1,
'EXEC_STMT': -2, 'EXEC_STMT': -2,
'BUILD_CLASS': -2, 'BUILD_CLASS': -2,
'STORE_NAME': -1, 'STORE_NAME': -1,
'STORE_ATTR': -2, 'STORE_ATTR': -2,
'DELETE_ATTR': -1, 'DELETE_ATTR': -1,
'STORE_GLOBAL': -1, 'STORE_GLOBAL': -1,
'BUILD_MAP': 1, 'BUILD_MAP': 1,
'COMPARE_OP': -1, 'COMPARE_OP': -1,
'STORE_FAST': -1, 'STORE_FAST': -1,
} }
# use pattern match # use pattern match
patterns = [ patterns = [
('BINARY_', -1), ('BINARY_', -1),
('LOAD_', 1), ('LOAD_', 1),
('IMPORT_', 1), ('IMPORT_', 1),
] ]
# special cases # special cases
#: UNPACK_TUPLE, UNPACK_LIST, BUILD_TUPLE, #: UNPACK_TUPLE, UNPACK_LIST, BUILD_TUPLE,
# BUILD_LIST, CALL_FUNCTION, MAKE_FUNCTION, BUILD_SLICE # BUILD_LIST, CALL_FUNCTION, MAKE_FUNCTION, BUILD_SLICE
def UNPACK_TUPLE(self, count): def UNPACK_TUPLE(self, count):
return count return count
def UNPACK_LIST(self, count): def UNPACK_LIST(self, count):
return count return count
def BUILD_TUPLE(self, count): def BUILD_TUPLE(self, count):
return -count return -count
def BUILD_LIST(self, count): def BUILD_LIST(self, count):
return -count return -count
def CALL_FUNCTION(self, argc): def CALL_FUNCTION(self, argc):
hi, lo = divmod(argc, 256) hi, lo = divmod(argc, 256)
return lo + hi * 2 return lo + hi * 2
def MAKE_FUNCTION(self, argc): def MAKE_FUNCTION(self, argc):
return -argc return -argc
def BUILD_SLICE(self, argc): def BUILD_SLICE(self, argc):
if argc == 2: if argc == 2:
return -1 return -1
elif argc == 3: elif argc == 3:
return -2 return -2
findDepth = StackDepthTracker().findDepth findDepth = StackDepthTracker().findDepth
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment