diff --git a/xdis/cross_dis.py b/xdis/cross_dis.py index b8420bda..b4312cb4 100644 --- a/xdis/cross_dis.py +++ b/xdis/cross_dis.py @@ -15,7 +15,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Here we are more closely modeling Python's Lib/dis.py organization. -# However it appears that Python names and code has copied a bit heavily from +# However, it appears that Python names and code has copied a bit heavily from # earlier versions of xdis (and without attribution). from xdis.version_info import PYTHON_VERSION_TRIPLE @@ -74,11 +74,11 @@ def findlinestarts(code, dup_lines=False): for byte_incr, line_incr in zip(byte_increments, line_increments): if byte_incr: if lineno != lastlineno or dup_lines and 0 < byte_incr < 255: - yield (offset, lineno) + yield offset, lineno lastlineno = lineno pass if offset >= bytecode_len: - # The rest of the lnotab byte offsets are past the end of + # The rest of the ``lnotab byte offsets are past the end of # the bytecode, so the lines were optimized away. return offset += byte_incr @@ -88,7 +88,7 @@ def findlinestarts(code, dup_lines=False): line_incr -= 0x100 lineno += line_incr if lineno != lastlineno or (dup_lines and 0 < byte_incr < 255): - yield (offset, lineno) + yield offset, lineno def code_info(x, version_tuple, is_pypy=False): diff --git a/xdis/disasm.py b/xdis/disasm.py index b9c0da9d..fc20738c 100644 --- a/xdis/disasm.py +++ b/xdis/disasm.py @@ -21,19 +21,22 @@ # imports so this can run on older Pythons. This is # intended to be a more cross-version Python program -import datetime, os, re, sys, types +import datetime +import os +import re +import sys +import types from collections import deque import xdis - from xdis.bytecode import Bytecode from xdis.codetype import iscode, codeType2Portable +from xdis.cross_dis import format_code_info from xdis.load import check_object_path, load_module from xdis.magics import PYTHON_MAGIC_INT -from xdis.cross_dis import format_code_info +from xdis.op_imports import op_imports, remap_opcodes from xdis.version import __version__ from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE -from xdis.op_imports import op_imports, remap_opcodes def get_opcode(version_tuple, is_pypy, alternate_opmap=None): @@ -96,7 +99,7 @@ def show_module_header( "\n# ".join(sys.version.split("\n")), ) ) - if PYTHON_VERSION_TRIPLE < (3, 0) and bytecode_version >= 3.0: + if PYTHON_VERSION_TRIPLE < (3, 0) and bytecode_version >= "3.0": real_out.write( "\n## **Warning** bytecode strings will be converted to strings.\n" ) @@ -124,8 +127,6 @@ def disco( source_size=None, sip_hash=None, asm_format="classic", - show_bytes=False, - dup_lines=False, alternate_opmap=None, ): """ @@ -174,7 +175,7 @@ def disco_loop( is in the order of first encountered which is not amenable for the format used by a disassembler where code objects should be defined before using them in other functions. - However this is not recursive and will overall lead to less + However, this is not recursive and will overall lead to less memory consumption at run time. """ @@ -201,7 +202,7 @@ def code_uniquify(basename, co_code): def disco_loop_asm_format(opc, version_tuple, co, real_out, fn_name_map, all_fns): """Produces disassembly in a format more conducive to automatic assembly by producing inner modules before they are - used by outer ones. Since this is recusive, we'll + used by outer ones. Since this is recursive, we'll use more stack space at runtime. """ @@ -268,7 +269,6 @@ def disassemble_file( outstream=sys.stdout, asm_format="classic", header=False, - show_bytes=False, alternate_opmap=None, ): """ @@ -292,7 +292,7 @@ def disassemble_file( source_size, sip_hash, ) = load_module(pyc_filename) - except: + except Exception: # Hack alert: we're using pyc_filename set as a proxy for whether the filename exists. # check_object_path() will succeed if the file exists. @@ -320,6 +320,7 @@ def disassemble_file( magic_int, source_size, sip_hash, + header=header, show_filename=True, ) else: @@ -333,7 +334,6 @@ def disassemble_file( source_size=source_size, sip_hash=sip_hash, asm_format=asm_format, - show_bytes=show_bytes, alternate_opmap=alternate_opmap, ) # print co.co_filename diff --git a/xdis/unmarshal.py b/xdis/unmarshal.py index 2ff17d58..a40c02c4 100644 --- a/xdis/unmarshal.py +++ b/xdis/unmarshal.py @@ -64,7 +64,7 @@ def long(n): "0": "C_NULL", "N": "None", "S": "stopIteration", - ".": "Elipsis", + ".": "Ellipsis", "F": "False", "T": "True", "i": "int32", @@ -86,7 +86,6 @@ def long(n): "[": "list", "<": "frozenset", ">": "set", - "i": "int32", "{": "dict", "R": "python2_string_reference", "c": "code", @@ -104,11 +103,9 @@ def compat_str(s: str) -> str: try: return s.decode("utf-8") except UnicodeDecodeError: - # If not Unicode, return bytes - # and it will get converted to str when needed + # If not Unicode, return bytes, + # and it will get converted to str when needed. return s - - return s.decode() else: return str(s) @@ -151,9 +148,9 @@ def __init__(self, fp, magic_int, bytes_for_s, code_objects={}): self.marshal_version = 3 else: self.marshal_version = 4 - elif version < (3, 4) and version >= (2, 5): + elif (3, 4) > version >= (2, 5): self.marshal_version = 2 - elif version < (2, 5) and version >= (2, 4): + elif (2, 5) > version >= (2, 4): self.marshal_version = 1 else: self.marshal_version = 0 @@ -167,7 +164,7 @@ def load(self): same magic for the running Python interpreter, we can simply use the Python-supplied marshal.load(). - However we need to use this when versions are different since the internal + However, we need to use this when versions are different since the internal code structures are different. Sigh. """ @@ -213,22 +210,22 @@ def r_object(self, bytes_for_s=False): # Since 3.4, "flag" is the marshal.c name save_ref = True byte1 = byte1 & (FLAG_REF - 1) - marshalType = chr(byte1) + marshal_type = chr(byte1) # print(marshalType) # debug - if marshalType in UNMARSHAL_DISPATCH_TABLE: - func_suffix = UNMARSHAL_DISPATCH_TABLE[marshalType] + if marshal_type in UNMARSHAL_DISPATCH_TABLE: + func_suffix = UNMARSHAL_DISPATCH_TABLE[marshal_type] unmarshal_func = getattr(self, "t_" + func_suffix) return unmarshal_func(save_ref, bytes_for_s) else: try: sys.stderr.write( "Unknown type %i (hex %x) %c\n" - % (ord(marshalType), hex(ord(marshalType)), marshalType) + % (ord(marshal_type), hex(ord(marshal_type)), marshal_type) ) except TypeError: sys.stderr.write( - "Unknown type %i %c\n" % (ord(marshalType), marshalType) + "Unknown type %i %c\n" % (ord(marshal_type), marshal_type) ) return @@ -244,7 +241,7 @@ def t_None(self, save_ref, bytes_for_s=False): def t_stopIteration(self, save_ref, bytes_for_s=False): return StopIteration - def t_Elipsis(self, save_ref, bytes_for_s=False): + def t_Ellipsis(self, save_ref, bytes_for_s=False): return Ellipsis def t_False(self, save_ref, bytes_for_s=False):