Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code style enhancements and less sphinx warnings #1570

Merged
merged 7 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ jobs:
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Cache for pip
uses: actions/cache@v1
id: cache-pip
Expand All @@ -26,8 +23,13 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Lint
- name: Critical lint
run: |
pip install flake8
flake8 . --count --select=E9,F63,F7 --show-source --statistics --exclude=android-?dk # TODO: Add F82
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude=pwnlib/constants,android-?dk,.git,__pycache__
flake8 . --count --select=E9,F63,F7,E71 --show-source --statistics --exclude=android-?dk # TODO: Add F82

- name: Style lint
run: |
flake8 pwnlib setup.py docs travis --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
flake8 examples --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --ignore='F403,F405'
flake8 pwn --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --ignore='F401,F403,F405'
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def linkcode_resolve(domain, info):
val = mod
for k in info['fullname'].split('.'):
val = getattr(val, k, None)
if val == None:
if val is None:
break

# Special case for shellcraft
Expand Down
32 changes: 16 additions & 16 deletions docs/source/util/iters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
count ,
cycle ,
dropwhile ,
filter ,
filterfalse ,
groupby ,
ifilter ,
ifilterfalse ,
imap ,
islice ,
izip ,
izip_longest ,
map ,
permutations ,
product ,
repeat ,
starmap ,
takewhile ,
tee
tee ,
zip ,
zip_longest

.. function:: chain

Expand Down Expand Up @@ -63,29 +63,29 @@

Alias for :func:`itertools.groupby`

.. function:: ifilter
.. function:: filter

Alias for :func:`itertools.ifilter`
Alias for python3-style :func:`filter`

.. function:: ifilterfalse
.. function:: filterfalse

Alias for :func:`itertools.ifilterfalse`
Alias for :func:`itertools.filterfalse`

.. function:: imap
.. function:: map

Alias for :func:`itertools.imap`
Alias for python3-style :func:`map`

.. function:: islice

Alias for :func:`itertools.islice`

.. function:: izip
.. function:: zip

Alias for :func:`itertools.izip`
Alias for python3-style :func:`zip`

.. function:: izip_longest
.. function:: zip_longest

Alias for :func:`itertools.izip_longest`
Alias for :func:`itertools.zip_longest`

.. function:: permutations

Expand Down
2 changes: 1 addition & 1 deletion pwn/toplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@
debug = log.debug
success = log.success

__all__ = [x for x in globals().keys() if x != '__name__']
__all__ = [x for x in tuple(globals()) if x != '__name__']
6 changes: 3 additions & 3 deletions pwnlib/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ def _assembler():
stderr=subprocess.STDOUT, universal_newlines=True)
version = re.search(r' (\d\.\d+)', result).group(1)
if version < '2.19':
log.warn_once('Your binutils version is too old and may not work!\n' + \
'Try updating with: https://docs.pwntools.com/en/stable/install/binutils.html\n' + \
'Reported Version: %r' % result.strip())
log.warn_once('Your binutils version is too old and may not work!\n'
'Try updating with: https://docs.pwntools.com/en/stable/install/binutils.html\n'
'Reported Version: %r', result.strip())


return assembler
Expand Down
4 changes: 2 additions & 2 deletions pwnlib/commandline/constgrep.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def main(args):
continue

# Check the constant
if constant != None:
if constant is not None:
val = getattr(mod, k)
if args.mask_mode:
if constant & val != val:
Expand All @@ -124,7 +124,7 @@ def main(args):
# If we are in match_mode, then try to find a combination of
# constants that yield the exact given value
# We do not want to find combinations using the value 0.
if not (constant == None or constant == 0) and args.mask_mode:
if constant and args.mask_mode:
mask = constant
good = []
out = [(v, k) for v, k in out if v != 0]
Expand Down
2 changes: 1 addition & 1 deletion pwnlib/constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __getattr__(self, key):
# Special case for __all__, we want to return the contextually
# relevant module.
if key == '__all__':
return self.guess().__dict__.keys()
return list(self.guess().__dict__.keys())

# Special case for all other special properties which aren't defined
if key.endswith('__'):
Expand Down
18 changes: 9 additions & 9 deletions pwnlib/elf/corefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ def __getitem__(self, item):
stop += self.stop

if not (self.start <= start <= stop <= self.stop):
log.error("Byte range [%#x:%#x] not within range [%#x:%#x]" \
% (start, stop, self.start, self.stop))
log.error("Byte range [%#x:%#x] not within range [%#x:%#x]",
start, stop, self.start, self.stop)

data = self._core.read(start, stop-start)

Expand Down Expand Up @@ -551,15 +551,15 @@ def __init__(self, *a, **kw):
self.load_addr = 0
self._address = 0

if not self.elftype == 'CORE':
if self.elftype != 'CORE':
log.error("%s is not a valid corefile" % self.file.name)

if not self.arch in prstatus_types.keys():
if self.arch not in prstatus_types:
log.warn_once("%s does not use a supported corefile architecture, registers are unavailable" % self.file.name)

prstatus_type = prstatus_types.get(self.arch, None)
heapcrash marked this conversation as resolved.
Show resolved Hide resolved
prpsinfo_type = prpsinfo_types.get(self.bits, None)
siginfo_type = siginfo_types.get(self.bits, None)
prstatus_type = prstatus_types.get(self.arch)
prpsinfo_type = prpsinfo_types.get(self.bits)
siginfo_type = siginfo_types.get(self.bits)

with log.waitfor("Parsing corefile...") as w:
self._load_mappings()
Expand Down Expand Up @@ -669,8 +669,8 @@ def _parse_nt_file(self, note):
continue

if mapping.start == self.at_sysinfo_ehdr \
or (not vdso and mapping.size in [0x1000, 0x2000] \
and mapping.flags == 5 \
or (not vdso and mapping.size in [0x1000, 0x2000]
and mapping.flags == 5
and self.read(mapping.start, 4) == b'\x7fELF'):
mapping.name = '[vdso]'
vdso = True
Expand Down
5 changes: 1 addition & 4 deletions pwnlib/elf/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,6 @@ def _describe(self, *a, **kw):
self.checksec(*a, **kw)
)

def __repr__(self):
heapcrash marked this conversation as resolved.
Show resolved Hide resolved
return "ELF(%r)" % self.path

def get_machine_arch(self):
return {
'EM_X86_64': 'amd64',
Expand Down Expand Up @@ -1845,7 +1842,7 @@ def checksec(self, banner=True, color=True):
# Check for Linux configuration, it must contain more than
# just the version.
if len(self.config) > 1:
config_opts = collections.defaultdict(lambda: [])
config_opts = collections.defaultdict(list)
for checker in kernel_configuration:
result, message = checker(self.config)

Expand Down
2 changes: 1 addition & 1 deletion pwnlib/encoders/arm/alphanumeric/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __call__(self, input, avoid, pcreg=None):
enc_dec_loop = b.encDecoderLoopBuilder(dec_loop)
dec = b.DecoderBuilder(dec_loop, self.icache_flush)

output,dec = b.buildInit(dec);
output, dec = b.buildInit(dec)

output += dec
output += enc_dec_loop
Expand Down
6 changes: 3 additions & 3 deletions pwnlib/encoders/arm/alphanumeric/alphanum_byte.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def off_gen(c):
return x
return 0

# return an alphanumeric value ret such that c XOR ret is also alphanumeric
# =========================================================================
# return an alphanumeric value ret such that c XOR ret is also alphanumeric
# =========================================================================
def alphanumeric_get_complement(c):
c &= 0xff;
c &= 0xff
while True:
ret = alphanumeric_get_byte()
if alphanumeric_check(c ^ ret):
Expand Down
5 changes: 2 additions & 3 deletions pwnlib/fmtstr.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,14 +834,13 @@ class FmtStr(object):

"""

def __init__(self, execute_fmt, offset = None, padlen = 0, numbwritten = 0):
def __init__(self, execute_fmt, offset=None, padlen=0, numbwritten=0):
self.execute_fmt = execute_fmt
self.offset = offset
self.padlen = padlen
self.numbwritten = numbwritten


if self.offset == None:
if self.offset is None:
self.offset, self.padlen = self.find_offset()
log.info("Found format string offset: %d", self.offset)

Expand Down
2 changes: 1 addition & 1 deletion pwnlib/gdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def binary():

if multiarch:
return multiarch
log.warn_once('Cross-architecture debugging usually requires gdb-multiarch\n' \
log.warn_once('Cross-architecture debugging usually requires gdb-multiarch\n'
'$ apt-get install gdb-multiarch')

if not gdb:
Expand Down
2 changes: 1 addition & 1 deletion pwnlib/memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def some_leaker(addr):
... if addr & 0xff == 0:
... return None
... return binsh[addr:addr+4]
>>> leaker_nonulls.d(0) == None
>>> leaker_nonulls.d(0) is None
leaking 0x0
True
>>> leaker_nonulls[0x100:0x104] == binsh[0x100:0x104]
Expand Down
6 changes: 3 additions & 3 deletions pwnlib/protocols/adb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def c(self):
process(context.adb + ['start-server']).recvall()
time.sleep(0.3)
else:
log.exception('Could not connect to ADB server (%s:%s)' % \
(self.host, self.port))
log.exception('Could not connect to ADB server (%s:%s)',
self.host, self.port)

# Final attempt...
if not self._c:
Expand Down Expand Up @@ -462,7 +462,7 @@ def stat(self, path):
>>> expected = {'mode': 16749, 'size': 0, 'time': 0}
>>> pwnlib.protocols.adb.AdbClient().stat('/proc') == expected
True
>>> pwnlib.protocols.adb.AdbClient().stat('/does/not/exist') == None
>>> pwnlib.protocols.adb.AdbClient().stat('/does/not/exist') is None
True
"""
if isinstance(path, six.text_type):
Expand Down
2 changes: 1 addition & 1 deletion pwnlib/regsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def regsort(in_out, all_regs, tmp = None, xchg = True, randomize = None):
# Collapse constant values
#
# For eaxmple, {'eax': 0, 'ebx': 0} => {'eax': 0, 'ebx': 'eax'}
v_k = defaultdict(lambda: [])
v_k = defaultdict(list)
for k,v in sorted(in_out.items()):
if v not in all_regs and v != 0:
v_k[v].append(k)
Expand Down
4 changes: 2 additions & 2 deletions pwnlib/rop/rop.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class DescriptiveStack(list):
descriptions = {}

def __init__(self, address):
self.descriptions = collections.defaultdict(lambda: [])
self.descriptions = collections.defaultdict(list)
self.address = address or 0
self._next_next = 0
self._next_last = 0
Expand Down Expand Up @@ -1317,7 +1317,7 @@ def __getattr__(self, attr):
True
>>> rop.ret_8 == rop.search(move=8)
True
>>> rop.ret != None
>>> rop.ret is not None
True
"""
gadget = collections.namedtuple('gadget', ['address', 'details'])
Expand Down
2 changes: 1 addition & 1 deletion pwnlib/shellcraft/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def init_mako():
from mako import ast
import threading

if lookup != None:
if lookup is not None:
return

class IsInsideManager(object):
Expand Down
4 changes: 2 additions & 2 deletions pwnlib/shellcraft/templates/aarch64/freebsd/syscall.asm
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ Example:
args = []
else:
syscall_repr = 'syscall(%s)'
if syscall == None:
if syscall is None:
args = ['?']
else:
args = [repr(syscall)]

for arg in [arg0, arg1, arg2, arg3, arg4, arg5]:
if arg == None:
if arg is None:
args.append('?')
else:
args.append(pretty(arg, False))
Expand Down
4 changes: 2 additions & 2 deletions pwnlib/shellcraft/templates/aarch64/linux/syscall.asm
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ Example:
args = []
else:
syscall_repr = 'syscall(%s)'
if syscall == None:
if syscall is None:
args = ['?']
else:
args = [repr(syscall)]

for arg in [arg0, arg1, arg2, arg3, arg4, arg5]:
if arg == None:
if arg is None:
args.append('?')
else:
args.append(repr(arg))
Expand Down
4 changes: 2 additions & 2 deletions pwnlib/shellcraft/templates/amd64/freebsd/syscall.asm
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ Example:
args = []
else:
syscall_repr = 'syscall(%s)'
if syscall == None:
if syscall is None:
args = ['?']
else:
args = [repr(syscall)]

for arg in [arg0, arg1, arg2, arg3, arg4, arg5]:
if arg == None:
if arg is None:
args.append('?')
else:
args.append(pretty(arg, False))
Expand Down
4 changes: 2 additions & 2 deletions pwnlib/shellcraft/templates/amd64/linux/syscall.asm
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ Example:
args = []
else:
syscall_repr = 'syscall(%s)'
if syscall == None:
if syscall is None:
args = ['?']
else:
args = [repr(syscall)]

for arg in [arg0, arg1, arg2, arg3, arg4, arg5]:
if arg == None:
if arg is None:
args.append('?')
else:
args.append(repr(arg))
Expand Down
2 changes: 1 addition & 1 deletion pwnlib/shellcraft/templates/amd64/ret.asm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Args:
</%docstring>
<%page args="return_value = None"/>

% if return_value != None:
% if return_value is not None:
${amd64.mov('rax', return_value)}
% endif

Expand Down
Loading