Skip to content

Commit

Permalink
Pycharm linting
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Sep 12, 2022
1 parent 7f5440c commit 1c9981a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 41 deletions.
8 changes: 4 additions & 4 deletions xdis/cross_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
24 changes: 12 additions & 12 deletions xdis/disasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -124,8 +127,6 @@ def disco(
source_size=None,
sip_hash=None,
asm_format="classic",
show_bytes=False,
dup_lines=False,
alternate_opmap=None,
):
"""
Expand Down Expand Up @@ -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.
"""

Expand All @@ -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.
"""

Expand Down Expand Up @@ -268,7 +269,6 @@ def disassemble_file(
outstream=sys.stdout,
asm_format="classic",
header=False,
show_bytes=False,
alternate_opmap=None,
):
"""
Expand All @@ -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.
Expand Down Expand Up @@ -320,6 +320,7 @@ def disassemble_file(
magic_int,
source_size,
sip_hash,
header=header,
show_filename=True,
)
else:
Expand All @@ -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
Expand Down
27 changes: 12 additions & 15 deletions xdis/unmarshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def long(n):
"0": "C_NULL",
"N": "None",
"S": "stopIteration",
".": "Elipsis",
".": "Ellipsis",
"F": "False",
"T": "True",
"i": "int32",
Expand All @@ -86,7 +86,6 @@ def long(n):
"[": "list",
"<": "frozenset",
">": "set",
"i": "int32",
"{": "dict",
"R": "python2_string_reference",
"c": "code",
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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.
"""

Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions xdis/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def code2num(code, i):


def num2code(num):
return (num & 0xFF, num >> 8)
return num & 0xFF, num >> 8


# The inspect module interrogates this dictionary to build its
# The `inspect` module interrogates this dictionary to build its
# list of CO_* constants. It is also used by pretty_flags to
# turn the co_flags field into a human readable list.
# turn the co_flags field into a human-readable list.
COMPILER_FLAG_NAMES = {
0x00000001: "OPTIMIZED",
0x00000002: "NEWLOCALS",
Expand Down Expand Up @@ -50,7 +50,7 @@ def num2code(num):
0x10000000: "PYPY_ACCEPT_NULL_BYTES",
}

# Invert above dictionary so we can look up a bit value
# Invert above dictionary, so we can look up a bit value
# from the compile flag name
COMPILER_FLAG_BIT = dict([v, k] for (k, v) in COMPILER_FLAG_NAMES.items())

Expand Down Expand Up @@ -88,7 +88,7 @@ def is_negative_zero(n):


def better_repr(v):
"""Work around Python's unorthogonal and unhelpful repr() for primitive float
"""Work around Python's not orthogonal and unhelpful repr() for primitive float
and complex."""
if isinstance(v, float):
# float values 'nan' and 'inf' are not directly
Expand All @@ -106,7 +106,7 @@ def better_repr(v):
imag = better_repr(v.imag)
# FIXME: we could probably use repr() in most cases
# sort out when that's possible.
# The below is however round-tripable, and Python's repr() isn't.
# The below round trips, and Python's repr() isn't.
return "complex(%s, %s)" % (real, imag)
elif isinstance(v, tuple):
if len(v) == 1:
Expand Down
5 changes: 1 addition & 4 deletions xdis/version_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Copyright (c) 2020-2021 by Rocky Bernstein
Copyright (c) 2020-2022 by Rocky Bernstein
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
Expand All @@ -14,9 +14,6 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
NB. This is not a masterpiece of software, but became more like a hack.
Probably a complete rewrite would be sensefull. hG/2000-12-27
"""

import sys
Expand Down

0 comments on commit 1c9981a

Please sign in to comment.