Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 1 commit
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jun 24, 2011
@bcandrea bcandrea Minor changes to make unholy compatible with Python 2.7
 * Updated pyasm.rb to support Ruby 1.9.2 and Python 2.7 (WIP)
 * Added a Python script to inspect the generated bytecode
0525a10
Showing with 69 additions and 6 deletions.
  1. +20 −6 lib/unholy/pyasm.rb
  2. +49 −0 show.py
View
26 lib/unholy/pyasm.rb
@@ -204,7 +204,7 @@ def opt_ltlt
build_list 1
inplace_add
end
- def opt_plus
+ def opt_plus(arg)
binary_add
end
def putnil
@@ -217,6 +217,18 @@ def putstring(str)
def putobject(obj)
load_const(obj)
end
+ def trace(flag)
+ end
+ def tostring
+ end
+ def concatstrings(str)
+ putstring(str)
+ end
+ def putspecialobject(obj)
+ end
+ def setconstant(const)
+ store_name(const)
+ end
def message(meth, op_argc, blockiseq, op_flag, ic)
# args = @stack.slice! -op_argc, op_argc
# bytes = @bytecode.slice! -op_argc, op_argc
@@ -342,9 +354,10 @@ def branchunless label
def load_iseq iseq
iseq = iseq.to_a
- @varsyms += iseq[8].reverse
+ @varsyms += iseq[10].reverse
- iseq[11].each do |inst|
+ iseq[13].each do |inst|
+ puts inst.inspect
case inst
when Integer # line no
line inst
@@ -359,10 +372,10 @@ def load_iseq iseq
end
def eval src
begin
- iseq = VM::InstructionSequence.compile(src)
+ iseq = RubyVM::InstructionSequence.compile(src)
load_iseq iseq
rescue NameError => e
- puts "*** Are you sure you're running Ruby 1.9?"
+ puts "*** Are you sure you are running Ruby 1.9?"
throw e
end
end
@@ -428,7 +441,8 @@ def to_pickle
def compile(fname)
# dump the bytecode
File.open(fname, "wb") do |f|
- f << "\xB3\xF2\r\n" # magic number for python 2.5
+ #f << "\xB3\xF2\r\n" # magic number for python 2.5
+ f << "\x03\xF3\r\n" # magic number for python 2.7
f << Time.now.to_i.to_plong
# code object
View
49 show.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+
+import dis, marshal, struct, sys, time, types
+
+def show_file(fname):
+ f = open(fname, "rb")
+ magic = f.read(4)
+ moddate = f.read(4)
+ modtime = time.asctime(time.localtime(struct.unpack('L', moddate)[0]))
+ print "magic %s" % (magic.encode('hex'))
+ print "moddate %s (%s)" % (moddate.encode('hex'), modtime)
+ code = marshal.load(f)
+ show_code(code)
+
+def show_code(code, indent=''):
+ print "%scode" % indent
+ indent += ' '
+ print "%sargcount %d" % (indent, code.co_argcount)
+ print "%snlocals %d" % (indent, code.co_nlocals)
+ print "%sstacksize %d" % (indent, code.co_stacksize)
+ print "%sflags %04x" % (indent, code.co_flags)
+ show_hex("code", code.co_code, indent=indent)
+ dis.disassemble(code)
+ print "%sconsts" % indent
+ for const in code.co_consts:
+ if type(const) == types.CodeType:
+ show_code(const, indent+' ')
+ else:
+ print " %s%r" % (indent, const)
+ print "%snames %r" % (indent, code.co_names)
+ print "%svarnames %r" % (indent, code.co_varnames)
+ print "%sfreevars %r" % (indent, code.co_freevars)
+ print "%scellvars %r" % (indent, code.co_cellvars)
+ print "%sfilename %r" % (indent, code.co_filename)
+ print "%sname %r" % (indent, code.co_name)
+ print "%sfirstlineno %d" % (indent, code.co_firstlineno)
+ show_hex("lnotab", code.co_lnotab, indent=indent)
+
+def show_hex(label, h, indent):
+ h = h.encode('hex')
+ if len(h) < 60:
+ print "%s%s %s" % (indent, label, h)
+ else:
+ print "%s%s" % (indent, label)
+ for i in range(0, len(h), 60):
+ print "%s %s" % (indent, h[i:i+60])
+
+show_file(sys.argv[1])
+

No commit comments for this range

Something went wrong with that request. Please try again.