1. Pypy
  2. Untitled project
  3. pypy

Source

pypy / pypy / tool / pydis.py

Diff from to

File pypy/tool/pydis.py

 import autopath
 import sys
 
-from pypy.tool.opcode import *
-from pypy.tool.opcode import __all__ as _opcodes_all
+from pypy.tool import stdlib_opcode
+from pypy.tool.stdlib_opcode import *
 
-__all__ = ["dis","pydisassemble","distb","disco"] + _opcodes_all
-del _opcodes_all
+__all__ = ["dis","pydisassemble","distb","disco"] + stdlib_opcode.__all__
+
+EXTENDED_ARG = stdlib_opcode.opcodedesc.EXTENDED_ARG.index
+
 
 class Bytecode:
     def __init__(self, disresult, bytecodeindex, oparg, lineno):
     def __ne__(self, other):
         return not (self == other)
 
-    def reprargstring(self):
+    def reprargstring(self, space = None):
         """ return a string representation of any arguments. (empty for no args)"""
         oparg = self.oparg
         if oparg is None:
         
         s = repr(oparg).rjust(5) + " "
         if op in hasconst:
-            # support both real code objects and PyCode objects
-            try:
-                consts = co.co_consts
-            except AttributeError:
-                consts = co.co_consts_w
-            s += '(' + `consts[oparg]` + ')'
+            consts = self.get_consts(space)
+            s += '(' + consts[oparg] + ')'
         elif op in hasname:
             s +=  '(' + co.co_names[oparg] + ')'
         elif op in hasjrel:
             s +=  '(' + free[oparg] + ')'
         return s 
 
+    def get_consts(self, space=None):
+        # support both real code objects and PyCode objects
+        co = self.disresult.code
+        if hasattr(co, "co_consts"):
+            return [repr(c) for c in co.co_consts]
+
+        if space is None:
+            return [repr(c) for c in co.co_consts_w]
+        
+        r = lambda x: space.str_w(space.repr(x))
+        return [r(c) for c in co.co_consts_w]
+
+    def repr_with_space(self, space):
+        return self.name + self.reprargstring(space)
+
     def __repr__(self):
         return self.name + self.reprargstring()
 
     if hasattr(co, 'func_code'):
         co = co.func_code 
 
+    if hasattr(co, 'code'):
+        co = co.code 
+
     disresult = DisResult(co)
     code = co.co_code
 
     byte_increments = [ord(c) for c in co.co_lnotab[0::2]]
     line_increments = [ord(c) for c in co.co_lnotab[1::2]]
-    table_length = len(byte_increments) # == len(line_increments)
+    table_length = len(byte_increments)
 
     lineno = co.co_firstlineno
     table_index = 0