diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2013-09-11 20:56:38 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2013-09-11 20:56:38 -0400 |
commit | ee08f60231fcd609f14c86b860d7602bbd7a294f (patch) | |
tree | 96551d799a863fde1b56f267d81d4bf1de3755cd | |
parent | bcbe9da43a5e6564a33ec3d78098393cb5ecb3d0 (diff) | |
download | python-coveragepy-git-ee08f60231fcd609f14c86b860d7602bbd7a294f.tar.gz |
Fix some line endings and whitespace.
-rw-r--r-- | .hgignore | 64 | ||||
-rw-r--r-- | igor.py | 1 | ||||
-rw-r--r-- | lab/hack_pyc.py | 164 | ||||
-rw-r--r-- | lab/sample.py | 10 | ||||
-rw-r--r-- | lab/show_pyc.py | 140 | ||||
-rw-r--r-- | lab/trace_sample.py | 114 |
6 files changed, 247 insertions, 246 deletions
@@ -1,32 +1,32 @@ -syntax: glob
-
-# Files that can appear anywhere in the tree.
-*.pyc
-*.pyo
-*$py.class
-*.pyd
-*.so
-*.bak
-.coverage
-.coverage.*
-*.swp
-
-# Stuff generated by editors.
-.idea/
-
-# Stuff in the root.
-build
-*.egg-info
-dist
-htmlcov
-MANIFEST
-setuptools-*.egg
-.tox
-.tox_kits
-
-# Stuff in the test directory.
-zipmods.zip
-
-# Stuff in the doc directory.
-_build
-sample_html_beta
+syntax: glob + +# Files that can appear anywhere in the tree. +*.pyc +*.pyo +*$py.class +*.pyd +*.so +*.bak +.coverage +.coverage.* +*.swp + +# Stuff generated by editors. +.idea/ + +# Stuff in the root. +build +*.egg-info +dist +htmlcov +MANIFEST +setuptools-*.egg +.tox +.tox_kits + +# Stuff in the test directory. +zipmods.zip + +# Stuff in the doc directory. +_build +sample_html_beta @@ -195,6 +195,7 @@ def do_check_eol(): check_file("setup.py") check_file("igor.py") check_file("Makefile") + check_file(".hgignore") check_files("doc", ["*.rst"]) check_files(".", ["*.txt"]) diff --git a/lab/hack_pyc.py b/lab/hack_pyc.py index e8992b96..1cdc4765 100644 --- a/lab/hack_pyc.py +++ b/lab/hack_pyc.py @@ -1,82 +1,82 @@ -""" Wicked hack to get .pyc files to do bytecode tracing instead of
- line tracing.
-"""
-
-import marshal, new, opcode, sys, types
-
-from lnotab import lnotab_numbers, lnotab_string
-
-class PycFile:
- def read(self, f):
- if isinstance(f, basestring):
- f = open(f, "rb")
- self.magic = f.read(4)
- self.modtime = f.read(4)
- self.code = marshal.load(f)
-
- def write(self, f):
- if isinstance(f, basestring):
- f = open(f, "wb")
- f.write(self.magic)
- f.write(self.modtime)
- marshal.dump(self.code, f)
-
- def hack_line_numbers(self):
- self.code = hack_line_numbers(self.code)
-
-def hack_line_numbers(code):
- """ Replace a code object's line number information to claim that every
- byte of the bytecode is a new source line. Returns a new code
- object. Also recurses to hack the line numbers in nested code objects.
- """
-
- # Create a new lnotab table. Each opcode is claimed to be at
- # 1000*lineno + (opcode number within line), so for example, the opcodes on
- # source line 12 will be given new line numbers 12000, 12001, 12002, etc.
- old_num = list(lnotab_numbers(code.co_lnotab, code.co_firstlineno))
- n_bytes = len(code.co_code)
- new_num = []
- line = 0
- opnum_in_line = 0
- i_byte = 0
- while i_byte < n_bytes:
- if old_num and i_byte == old_num[0][0]:
- line = old_num.pop(0)[1]
- opnum_in_line = 0
- new_num.append((i_byte, 100000000 + 1000*line + opnum_in_line))
- if ord(code.co_code[i_byte]) >= opcode.HAVE_ARGUMENT:
- i_byte += 3
- else:
- i_byte += 1
- opnum_in_line += 1
-
- # new_num is a list of pairs, (byteoff, lineoff). Turn it into an lnotab.
- new_firstlineno = new_num[0][1]-1
- new_lnotab = lnotab_string(new_num, new_firstlineno)
-
- # Recurse into code constants in this code object.
- new_consts = []
- for const in code.co_consts:
- if type(const) == types.CodeType:
- new_consts.append(hack_line_numbers(const))
- else:
- new_consts.append(const)
-
- # Create a new code object, just like the old one, except with new
- # line numbers.
- new_code = new.code(
- code.co_argcount, code.co_nlocals, code.co_stacksize, code.co_flags,
- code.co_code, tuple(new_consts), code.co_names, code.co_varnames,
- code.co_filename, code.co_name, new_firstlineno, new_lnotab
- )
-
- return new_code
-
-def hack_file(f):
- pyc = PycFile()
- pyc.read(f)
- pyc.hack_line_numbers()
- pyc.write(f)
-
-if __name__ == '__main__':
- hack_file(sys.argv[1])
+""" Wicked hack to get .pyc files to do bytecode tracing instead of + line tracing. +""" + +import marshal, new, opcode, sys, types + +from lnotab import lnotab_numbers, lnotab_string + +class PycFile: + def read(self, f): + if isinstance(f, basestring): + f = open(f, "rb") + self.magic = f.read(4) + self.modtime = f.read(4) + self.code = marshal.load(f) + + def write(self, f): + if isinstance(f, basestring): + f = open(f, "wb") + f.write(self.magic) + f.write(self.modtime) + marshal.dump(self.code, f) + + def hack_line_numbers(self): + self.code = hack_line_numbers(self.code) + +def hack_line_numbers(code): + """ Replace a code object's line number information to claim that every + byte of the bytecode is a new source line. Returns a new code + object. Also recurses to hack the line numbers in nested code objects. + """ + + # Create a new lnotab table. Each opcode is claimed to be at + # 1000*lineno + (opcode number within line), so for example, the opcodes on + # source line 12 will be given new line numbers 12000, 12001, 12002, etc. + old_num = list(lnotab_numbers(code.co_lnotab, code.co_firstlineno)) + n_bytes = len(code.co_code) + new_num = [] + line = 0 + opnum_in_line = 0 + i_byte = 0 + while i_byte < n_bytes: + if old_num and i_byte == old_num[0][0]: + line = old_num.pop(0)[1] + opnum_in_line = 0 + new_num.append((i_byte, 100000000 + 1000*line + opnum_in_line)) + if ord(code.co_code[i_byte]) >= opcode.HAVE_ARGUMENT: + i_byte += 3 + else: + i_byte += 1 + opnum_in_line += 1 + + # new_num is a list of pairs, (byteoff, lineoff). Turn it into an lnotab. + new_firstlineno = new_num[0][1]-1 + new_lnotab = lnotab_string(new_num, new_firstlineno) + + # Recurse into code constants in this code object. + new_consts = [] + for const in code.co_consts: + if type(const) == types.CodeType: + new_consts.append(hack_line_numbers(const)) + else: + new_consts.append(const) + + # Create a new code object, just like the old one, except with new + # line numbers. + new_code = new.code( + code.co_argcount, code.co_nlocals, code.co_stacksize, code.co_flags, + code.co_code, tuple(new_consts), code.co_names, code.co_varnames, + code.co_filename, code.co_name, new_firstlineno, new_lnotab + ) + + return new_code + +def hack_file(f): + pyc = PycFile() + pyc.read(f) + pyc.hack_line_numbers() + pyc.write(f) + +if __name__ == '__main__': + hack_file(sys.argv[1]) diff --git a/lab/sample.py b/lab/sample.py index cf4f6dcf..bb628484 100644 --- a/lab/sample.py +++ b/lab/sample.py @@ -1,5 +1,5 @@ -a, b = 1, 0
-if a or b or fn():
- # Hey
- a = 3
-d = 4
\ No newline at end of file +a, b = 1, 0 +if a or b or fn(): + # Hey + a = 3 +d = 4 diff --git a/lab/show_pyc.py b/lab/show_pyc.py index 7dacc2b0..b2cbb342 100644 --- a/lab/show_pyc.py +++ b/lab/show_pyc.py @@ -1,70 +1,70 @@ -import dis, marshal, struct, sys, time, types
-
-def show_pyc_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_py_file(fname):
- text = open(fname).read().replace('\r\n', '\n')
- show_py_text(text, fname=fname)
-
-def show_py_text(text, fname="<string>"):
- code = compile(text, fname, "exec")
- 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])
-
-def show_file(fname):
- if fname.endswith('pyc'):
- show_pyc_file(fname)
- elif fname.endswith('py'):
- show_py_file(fname)
- else:
- print "Odd file:", fname
-
-def main(args):
- if args[0] == '-c':
- show_py_text(" ".join(args[1:]).replace(";", "\n"))
- else:
- for a in args:
- show_file(a)
-
-if __name__ == '__main__':
- main(sys.argv[1:])
+import dis, marshal, struct, sys, time, types + +def show_pyc_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_py_file(fname): + text = open(fname).read().replace('\r\n', '\n') + show_py_text(text, fname=fname) + +def show_py_text(text, fname="<string>"): + code = compile(text, fname, "exec") + 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]) + +def show_file(fname): + if fname.endswith('pyc'): + show_pyc_file(fname) + elif fname.endswith('py'): + show_py_file(fname) + else: + print "Odd file:", fname + +def main(args): + if args[0] == '-c': + show_py_text(" ".join(args[1:]).replace(";", "\n")) + else: + for a in args: + show_file(a) + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/lab/trace_sample.py b/lab/trace_sample.py index 9fa37249..3f819199 100644 --- a/lab/trace_sample.py +++ b/lab/trace_sample.py @@ -1,57 +1,57 @@ -import os, sys
-
-global nest
-nest = 0
-
-def trace(frame, event, arg):
- #if event == 'line':
- global nest
-
- print "%s%s %s %d" % (
- " " * nest,
- event,
- os.path.basename(frame.f_code.co_filename),
- frame.f_lineno,
- )
-
- if event == 'call':
- nest += 1
- if event == 'return':
- nest -= 1
-
- return trace
-
-def trace2(frame, event, arg):
- #if event == 'line':
- global nest
-
- print "2: %s%s %s %d" % (
- " " * nest,
- event,
- os.path.basename(frame.f_code.co_filename),
- frame.f_lineno,
- )
-
- if event == 'call':
- nest += 1
- if event == 'return':
- nest -= 1
-
- return trace2
-
-sys.settrace(trace)
-
-def bar():
- print "nar"
-
-a = 26
-def foo(n):
- a = 28
- sys.settrace(sys.gettrace())
- bar()
- a = 30
- return 2*n
-
-print foo(a)
-#import sample
-#import littleclass
+import os, sys + +global nest +nest = 0 + +def trace(frame, event, arg): + #if event == 'line': + global nest + + print "%s%s %s %d" % ( + " " * nest, + event, + os.path.basename(frame.f_code.co_filename), + frame.f_lineno, + ) + + if event == 'call': + nest += 1 + if event == 'return': + nest -= 1 + + return trace + +def trace2(frame, event, arg): + #if event == 'line': + global nest + + print "2: %s%s %s %d" % ( + " " * nest, + event, + os.path.basename(frame.f_code.co_filename), + frame.f_lineno, + ) + + if event == 'call': + nest += 1 + if event == 'return': + nest -= 1 + + return trace2 + +sys.settrace(trace) + +def bar(): + print "nar" + +a = 26 +def foo(n): + a = 28 + sys.settrace(sys.gettrace()) + bar() + a = 30 + return 2*n + +print foo(a) +#import sample +#import littleclass |