Fix PEP8 violations and add flake8 to CI (#1102)

* Add flake8 config to setup.cfg

* Fix W291 PEP8 warnings

* Fix W605 PEP8 warnings

* Fix E711 PEP8 error

* Fix E713 PEP8 errors

* Fix E262 PEP8 errors

* Fix E714 PEP8 error

* Fix E712 PEP8 errors

* Add flake8 to CI
pull/1104/head
Gulshan Singh 3 years ago committed by GitHub
parent b3a560eff0
commit 1df714d700
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,10 +22,12 @@ jobs:
run: | run: |
pip install isort pip install isort
pip install black pip install black
pip install flake8
- name: Run linters - name: Run linters
run: | run: |
git diff-index --quiet HEAD -- pwndbg tests git diff-index --quiet HEAD -- pwndbg tests
isort --check-only --diff pwndbg tests isort --check-only --diff pwndbg tests
black --diff --check pwndbg tests black --diff --check pwndbg tests
flake8 --show-source pwndbg/ tests
python3 -m py_compile $(git ls-files 'pwndbg/*.py') python3 -m py_compile $(git ls-files 'pwndbg/*.py')

@ -142,7 +142,7 @@ def generateColorFunction(config):
def strip(x): def strip(x):
return re.sub("\x1b\\[[\d;]+m", "", x) return re.sub("\x1b\\[[\\d;]+m", "", x)
def terminateWith(x, color): def terminateWith(x, color):

@ -26,7 +26,7 @@ class PwntoolsLexer(RegexLexer):
#: optional Comment or Whitespace #: optional Comment or Whitespace
string = r'"(\\"|[^"])*"' string = r'"(\\"|[^"])*"'
char = r"[\w$.@-]" char = r"[\w$.@-]"
identifier = r"(?:[a-zA-Z$_]" + char + "*|\." + char + "+|or)" identifier = r"(?:[a-zA-Z$_]" + char + r"*|\." + char + r"+|or)"
number = r"(?:0[xX][a-zA-Z0-9]+|\d+)" number = r"(?:0[xX][a-zA-Z0-9]+|\d+)"
memory = r"(?:[\]\[])" memory = r"(?:[\]\[])"

@ -13,7 +13,7 @@ parser = argparse.ArgumentParser(
description="""Attaches to a given pid, process name or device file. description="""Attaches to a given pid, process name or device file.
This command wraps the original GDB `attach` command to add the ability This command wraps the original GDB `attach` command to add the ability
to debug a process with given name. In such case the process identifier is to debug a process with given name. In such case the process identifier is
fetched via the `pidof <name>` command. fetched via the `pidof <name>` command.
Original GDB attach command help: Original GDB attach command help:

@ -27,7 +27,7 @@ def comm(addr=None, comment=None):
else: else:
f.write("file:%s=" % pwndbg.proc.exe) f.write("file:%s=" % pwndbg.proc.exe)
f.write("%#x:%s\n" % (target, comment)) f.write("%#x:%s\n" % (target, comment))
if not pwndbg.proc.exe in file_lists.keys(): if pwndbg.proc.exe not in file_lists.keys():
file_lists[pwndbg.proc.exe] = {} file_lists[pwndbg.proc.exe] = {}
file_lists[pwndbg.proc.exe][hex(target)] = comment file_lists[pwndbg.proc.exe][hex(target)] = comment
except Exception: except Exception:
@ -46,7 +46,7 @@ def init():
filename = text1.split(":")[1] filename = text1.split(":")[1]
addr_comm = text2.split(":") addr_comm = text2.split(":")
if not filename in file_lists: if filename not in file_lists:
file_lists[filename] = {} file_lists[filename] = {}
file_lists[filename][addr_comm[0]] = addr_comm[1] file_lists[filename][addr_comm[0]] = addr_comm[1]

@ -22,8 +22,8 @@ def get_rec_addr_string(addr, visited_map):
page = pwndbg.vmmap.find(addr) page = pwndbg.vmmap.find(addr)
arrow_right = C.arrow(" %s " % config_arrow_right) arrow_right = C.arrow(" %s " % config_arrow_right)
if not page is None: if page is not None:
if not addr in visited_map: if addr not in visited_map:
return "" return ""
parent_info = visited_map[addr] parent_info = visited_map[addr]

@ -101,7 +101,7 @@ def p2p_walk(addr, ranges, current_level):
maybe_addr = maybe_points_to_ranges(addr, ranges[current_level]) maybe_addr = maybe_points_to_ranges(addr, ranges[current_level])
if maybe_addr == None: if maybe_addr is None:
return None return None
if current_level == levels - 1: if current_level == levels - 1:
@ -124,5 +124,5 @@ def p2p(mapping_names: [] = None):
for addr in range(rng.begin, rng.end): for addr in range(rng.begin, rng.end):
maybe_pointer = p2p_walk(addr, mapping_names, current_level=1) maybe_pointer = p2p_walk(addr, mapping_names, current_level=1)
if maybe_pointer != None: if maybe_pointer is not None:
ts(address=addr, count=1) ts(address=addr, count=1)

@ -87,7 +87,7 @@ def probeleak(address=None, count=0x40, max_distance=0x0, point_to=None, max_ptr
ptrsize = pwndbg.arch.ptrsize ptrsize = pwndbg.arch.ptrsize
count = max(int(count), ptrsize) count = max(int(count), ptrsize)
off_zeros = int(math.ceil(math.log(count, 2) / 4)) off_zeros = int(math.ceil(math.log(count, 2) / 4))
if flags != None: if flags is not None:
require_flags = flags_str2int(flags) require_flags = flags_str2int(flags)
if count > address > 0x10000: # in case someone puts in an end address and not a count (smh) if count > address > 0x10000: # in case someone puts in an end address and not a count (smh)
@ -119,9 +119,9 @@ def probeleak(address=None, count=0x40, max_distance=0x0, point_to=None, max_ptr
p = pwndbg.arch.unpack(data[i : i + ptrsize]) p = pwndbg.arch.unpack(data[i : i + ptrsize])
page = find_module(p, max_distance) page = find_module(p, max_distance)
if page: if page:
if point_to != None and point_to not in page.objfile: if point_to is not None and point_to not in page.objfile:
continue continue
if flags != None and not satisfied_flags(require_flags, page.flags): if flags is not None and not satisfied_flags(require_flags, page.flags):
continue continue
if not found: if not found:
print(M.legend()) print(M.legend())

@ -108,7 +108,7 @@ Briefly describe the problem you are having in a few paragraphs.
<!-- <!--
What do we have to do to reproduce the problem? What do we have to do to reproduce the problem?
If this is connected to particular C/asm code, If this is connected to particular C/asm code,
please provide the smallest C code that reproduces the issue. please provide the smallest C code that reproduces the issue.
--> -->

@ -35,7 +35,7 @@ def get_field_by_name(obj, field):
# Dereference once # Dereference once
if obj.type.code == gdb.TYPE_CODE_PTR: if obj.type.code == gdb.TYPE_CODE_PTR:
obj = obj.dereference() obj = obj.dereference()
for f in re.split("(->|\.|\[\d+\])", field): for f in re.split(r"(->|\.|\[\d+\])", field):
if not f: if not f:
continue continue
if f == "->": if f == "->":

@ -67,7 +67,7 @@ def enhance(value, code=True, safe_linking=False):
# If it's not in a page we know about, try to dereference # If it's not in a page we know about, try to dereference
# it anyway just to test. # it anyway just to test.
can_read = True can_read = True
if not page or None == pwndbg.memory.peek(value): if not page or None is pwndbg.memory.peek(value):
can_read = False can_read = False
if not can_read: if not can_read:

@ -165,27 +165,27 @@ def unix():
NETLINK_TYPES = { NETLINK_TYPES = {
0: "NETLINK_ROUTE", # /* Routing/device hook */ 0: "NETLINK_ROUTE", # Routing/device hook
1: "NETLINK_UNUSED", # /* Unused number */ 1: "NETLINK_UNUSED", # Unused number
2: "NETLINK_USERSOCK", # /* Reserved for user mode socket protocols */ 2: "NETLINK_USERSOCK", # Reserved for user mode socket protocols
3: "NETLINK_FIREWALL", # /* Unused number", formerly ip_queue */ 3: "NETLINK_FIREWALL", # Unused number", formerly ip_queue
4: "NETLINK_SOCK_DIAG", # /* socket monitoring */ 4: "NETLINK_SOCK_DIAG", # socket monitoring
5: "NETLINK_NFLOG", # /* netfilter/iptables ULOG */ 5: "NETLINK_NFLOG", # netfilter/iptables ULOG
6: "NETLINK_XFRM", # /* ipsec */ 6: "NETLINK_XFRM", # ipsec
7: "NETLINK_SELINUX", # /* SELinux event notifications */ 7: "NETLINK_SELINUX", # SELinux event notifications
8: "NETLINK_ISCSI", # /* Open-iSCSI */ 8: "NETLINK_ISCSI", # Open-iSCSI
9: "NETLINK_AUDIT", # /* auditing */ 9: "NETLINK_AUDIT", # auditing
10: "NETLINK_FIB_LOOKUP", # 10: "NETLINK_FIB_LOOKUP", #
11: "NETLINK_CONNECTOR", # 11: "NETLINK_CONNECTOR", #
12: "NETLINK_NETFILTER", # /* netfilter subsystem */ 12: "NETLINK_NETFILTER", # netfilter subsystem
13: "NETLINK_IP6_FW", # 13: "NETLINK_IP6_FW", #
14: "NETLINK_DNRTMSG", # /* DECnet routing messages */ 14: "NETLINK_DNRTMSG", # DECnet routing messages
15: "NETLINK_KOBJECT_UEVENT", # /* Kernel messages to userspace */ 15: "NETLINK_KOBJECT_UEVENT", # Kernel messages to userspace
16: "NETLINK_GENERIC", # 16: "NETLINK_GENERIC", #
18: "NETLINK_SCSITRANSPORT", # /* SCSI Transports */ 18: "NETLINK_SCSITRANSPORT", # SCSI Transports
19: "NETLINK_ECRYPTFS", # 19: "NETLINK_ECRYPTFS", #
20: "NETLINK_RDMA", # 20: "NETLINK_RDMA", #
21: "NETLINK_CRYPTO", # /* Crypto layer */ 21: "NETLINK_CRYPTO", # Crypto layer
} }

@ -2,3 +2,66 @@
ignore = E203,E227,E251,E226,E501,E221,E241,E402,E261,E265,E704,W603,W503 ignore = E203,E227,E251,E226,E501,E221,E241,E402,E261,E265,E704,W603,W503
max-line-length=120 max-line-length=120
exclude = capstone,unicorn,pwndbg/constants exclude = capstone,unicorn,pwndbg/constants
[flake8]
ignore =
# continuation line under-indented for hanging indent (default)
E121,
# closing bracket does not match indentation of opening bracket's line (default)
E123,
# continuation line over-indented for hanging indent (default)
E126,
# continuation line under-indented for visual indent
E128,
# closing bracket is missing indentation (default)
E133,
# whitespace before :
E203,
# multiple spaces before operator
E221,
# missing whitespace around arithmetic operator (default)
E226,
# missing whitespace around bitwise or shift operator
E227,
# multiple spaces after , (default)
E241,
# tab after ',' (default)
E242,
# unexpected spaces around keyword / parameter equals
E251,
# missing whitespace around arithmetic operator
E261,
# too many leading '#' for block comment
E266,
# block comment should start with #
E265,
# module level import not at top of file
E402,
# line too long (82 > 79 characters)
E501,
# multiple statements on one line (def) (default)
E704,
# do not use bare except, specify exception instead
E722,
# do not assign a lambda expression, use a def
E731
# line break before binary operator (default)
W503,
# line break occurred after a binary operator (default)
W504,
# doc line too long (82 > 79 characters) (default)
W505,
# module imported but unused
F401,
# 'from module import *' used; unable to detect undefined names
F403,
# name may be undefined, or defined from star imports: module
F405,
# redefinition of unused name from line n
F811,
# undefined name name
F821,
# local variable name is assigned to but never used
F841
max-line-length=120
exclude = capstone,unicorn,pwndbg/constants

@ -40,7 +40,7 @@ def launched_bash_binary():
os.remove(path) # Cleanup os.remove(path) # Cleanup
@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) @pytest.mark.skipif(can_attach is False, reason=REASON_CANNOT_ATTACH)
def test_attachp_command_attaches_to_procname(launched_bash_binary): def test_attachp_command_attaches_to_procname(launched_bash_binary):
pid, binary_path = launched_bash_binary pid, binary_path = launched_bash_binary
@ -53,7 +53,7 @@ def test_attachp_command_attaches_to_procname(launched_bash_binary):
assert re.search(r"Detaching from program: %s, process %s" % (binary_path, pid), result) assert re.search(r"Detaching from program: %s, process %s" % (binary_path, pid), result)
@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) @pytest.mark.skipif(can_attach is False, reason=REASON_CANNOT_ATTACH)
def test_attachp_command_attaches_to_pid(launched_bash_binary): def test_attachp_command_attaches_to_pid(launched_bash_binary):
pid, binary_path = launched_bash_binary pid, binary_path = launched_bash_binary
@ -65,7 +65,7 @@ def test_attachp_command_attaches_to_pid(launched_bash_binary):
assert re.search(r"Detaching from program: %s, process %s" % (binary_path, pid), result) assert re.search(r"Detaching from program: %s, process %s" % (binary_path, pid), result)
@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) @pytest.mark.skipif(can_attach is False, reason=REASON_CANNOT_ATTACH)
def test_attachp_command_attaches_to_procname_too_many_pids(launched_bash_binary): def test_attachp_command_attaches_to_procname_too_many_pids(launched_bash_binary):
pid, binary_path = launched_bash_binary pid, binary_path = launched_bash_binary
@ -86,7 +86,7 @@ def test_attachp_command_attaches_to_procname_too_many_pids(launched_bash_binary
assert matches == expected_pids assert matches == expected_pids
@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) @pytest.mark.skipif(can_attach is False, reason=REASON_CANNOT_ATTACH)
def test_attachp_command_nonexistent_procname(): def test_attachp_command_nonexistent_procname():
result = run_gdb_with_script( result = run_gdb_with_script(
pyafter="attachp some-nonexistent-process-name" pyafter="attachp some-nonexistent-process-name"

@ -56,13 +56,13 @@ def test_loads_binary_with_core_without_crashing():
assert all(item in output for item in expected) assert all(item in output for item in expected)
lwp_line = re.compile("^\[New LWP \d+\]$") lwp_line = re.compile(r"^\[New LWP \d+\]$")
assert any([lwp_line.match(line) for line in output]) assert any([lwp_line.match(line) for line in output])
binary_line = re.compile("^Core was generated by .+$") binary_line = re.compile("^Core was generated by .+$")
assert any([binary_line.match(line) for line in output]) assert any([binary_line.match(line) for line in output])
crash_address_line = re.compile("^#0 0x[0-9a-fA-F]+ in main \(\)$") crash_address_line = re.compile(r"^#0 0x[0-9a-fA-F]+ in main \(\)$")
assert any([crash_address_line.match(line) for line in output]) assert any([crash_address_line.match(line) for line in output])
@ -82,13 +82,13 @@ def test_loads_core_without_crashing():
assert all(item in output for item in expected) assert all(item in output for item in expected)
lwp_line = re.compile("^\[New LWP \d+\]$") lwp_line = re.compile(r"^\[New LWP \d+\]$")
assert any([lwp_line.match(line) for line in output]) assert any([lwp_line.match(line) for line in output])
binary_line = re.compile("^Core was generated by .+$") binary_line = re.compile("^Core was generated by .+$")
assert any([binary_line.match(line) for line in output]) assert any([binary_line.match(line) for line in output])
crash_address_line = re.compile("^#0 0x[0-9a-fA-F]+ in \?\? \(\)$") crash_address_line = re.compile(r"^#0 0x[0-9a-fA-F]+ in \?\? \(\)$")
assert any([crash_address_line.match(line) for line in output]) assert any([crash_address_line.match(line) for line in output])

Loading…
Cancel
Save