Skip to content

Commit

Permalink
PEP 227 implementation
Browse files Browse the repository at this point in the history
Track changes to new opcodes.  Add hasfree list that applies to all
ops that use the closure.
  • Loading branch information
jeremyhylton committed Jan 25, 2001
1 parent 903f654 commit a39414b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

__all__ = ["dis","disassemble","distb","disco","opname","cmp_op",
"hasconst","hasname","hasjrel","hasjabs","haslocal",
"hascompare"]
"hascompare", "hasfree"]

def dis(x=None):
"""Disassemble classes, methods, functions, or code.
Expand Down Expand Up @@ -60,6 +60,7 @@ def disassemble(co, lasti=-1):
n = len(code)
i = 0
extended_arg = 0
free = None
while i < n:
c = code[i]
op = ord(c)
Expand Down Expand Up @@ -88,6 +89,10 @@ def disassemble(co, lasti=-1):
print '(' + co.co_varnames[oparg] + ')',
elif op in hascompare:
print '(' + cmp_op[oparg] + ')',
elif op in hasfree:
if free is None:
free = co.co_cellvars + co.co_freevars
print '(' + free[oparg] + ')',
print

disco = disassemble # XXX For backwards compatibility
Expand Down Expand Up @@ -127,6 +132,7 @@ def findlabels(code):
hasjabs = []
haslocal = []
hascompare = []
hasfree = []

opname = [''] * 256
for op in range(256): opname[op] = '<' + `op` + '>'
Expand Down Expand Up @@ -272,6 +278,14 @@ def jabs_op(name, op):
def_op('MAKE_FUNCTION', 132) # Number of args with default values
def_op('BUILD_SLICE', 133) # Number of items

def_op('MAKE_CLOSURE', 134)
def_op('LOAD_CLOSURE', 135)
hasfree.append(135)
def_op('LOAD_DEREF', 136)
hasfree.append(136)
def_op('STORE_DEREF', 137)
hasfree.append(137)

def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
Expand Down

0 comments on commit a39414b

Please sign in to comment.