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

refactor: use f-strings across codebase #768

Merged

Conversation

theguy147
Copy link
Collaborator

refactor: use f-strings across codebase

Description/Motivation/Screenshots

This PR replaces all .format(...) and % strings with f-strings.

How Has This Been Tested?

Architecture Yes/No Comments
x86-32 ✔️
x86-64 ✔️
ARM ✖️
AARCH64 ✖️
MIPS ✖️
POWERPC ✖️
SPARC ✖️
RISC-V ✖️
make test ✖️ test_cmd_elf_info already failed on this branch before this PR

Checklist

  • My PR was done against the gdb_8_py36_code_refactor branch, not master.
  • My code follows the code style of this project.
  • My change includes a change to the documentation, if required.
  • My change adds tests as appropriate.
  • I have read and agree to the CONTRIBUTING document.

@daniellimws
Copy link
Collaborator

1000+ lines of changes 😰

I looked at maybe 100+ of them, all looks ok 😄

Copy link
Collaborator

@Grazfather Grazfather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, but looks like you missed some. Also we can probably use this PR to clean up a lot of unneeded :s or :c or 0xf"{...:x}"

gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@Grazfather Grazfather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd grep for that str() pattern.

gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
@Grazfather
Copy link
Collaborator

A few comments to address, let's get this merged!

@theguy147
Copy link
Collaborator Author

A few comments to address, let's get this merged!

I actually tried implementing your comments (not just the lines you mentioned but in regards to the pattern) and messed everything up because I got confused with the types. That's why I focused on the "Type Hints" PR first and wanted to fix this one afterwards.

@Grazfather
Copy link
Collaborator

OK fair

@hugsy
Copy link
Owner

hugsy commented Jan 9, 2022

@theguy147 I will stop developing on this branch (after 5f641bb) so you can now adjust the conflicts.

Some more commits might land, but only affect /docs so no worries for this PR.

Cheers

@hugsy hugsy added this to the Release: next milestone Jan 9, 2022
Copy link
Collaborator

@Grazfather Grazfather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly good, but a few things:

Most :s are probably unnecessary.
All str() should be unnecessary. Can use !s if we really have to. It does the same thing.

Also, the diff looks messed up. I suggest you squash and then rebase to clean it up.

gef.py Outdated Show resolved Hide resolved
gef.py Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
@theguy147 theguy147 marked this pull request as draft January 10, 2022 20:09
@theguy147 theguy147 force-pushed the gdb_8_py36_code_refactor branch 2 times, most recently from 076d565 to ef19244 Compare January 11, 2022 13:28
@theguy147 theguy147 force-pushed the gdb_8_py36_code_refactor branch 2 times, most recently from 4238d69 to 7b71465 Compare January 11, 2022 19:05
@theguy147 theguy147 marked this pull request as ready for review January 11, 2022 19:10
gef.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@Grazfather Grazfather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed lines 1-6500

gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Show resolved Hide resolved
Copy link
Collaborator

@Grazfather Grazfather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done reviewing. A few nits. Thanks for your patience.

gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
Comment on lines 8621 to 8624
# backward compat for gdb (gdb < 7.10)
if not hasattr(gdb, "FrameDecorator"):
gdb.execute("backtrace {:d}".format(nb_backtrace))
gdb.execute(f"backtrace {nb_backtrace:d}")
return
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has nothing to do with your PR but since we now baseline gdb to be 8, can you remove this block?

Suggested change
# backward compat for gdb (gdb < 7.10)
if not hasattr(gdb, "FrameDecorator"):
gdb.execute("backtrace {:d}".format(nb_backtrace))
gdb.execute(f"backtrace {nb_backtrace:d}")
return

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should I still commit the suggested fix or now that it was added to the project to track, we do that in a future code change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Future one

memalign = gef.arch.ptrsize

offset = idx * memalign
current_address = align_address(addr + offset)
addrs = dereference_from(current_address)
l = ""
addr_l = format_address(int(addrs[0], 16))
l += "{:s}{:s}{:+#07x}: {:{ma}s}".format(Color.colorify(addr_l, base_address_color),
l += "{}{}{:+#07x}: {:{ma}s}".format(Color.colorify(addr_l, base_address_color),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you didn't f-string this one?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eye, also the args on the next line aren't lined up.

Copy link
Collaborator Author

@theguy147 theguy147 Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few format-strings that I didn't convert to f-strings yet. When I didn't convert them it was mostly because I was concerned with worsening the readability. In this case the part I wasn't sure about is {:{ma}s}. This line would turn into:

l += f"{Color.colorify(addr_l, base_address_color)}{VERTICAL_LINE}{base_offset+offset:+#07x}: {sep.join(addrs[1:]):{memalign*2+2}s}"

I can include this and similar strings as well if you think that it improves the code quality overall (at least for the aspect of consistency)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah fair. We shouldn't be married to f strings if it makes things harder to read (but please fix the alignment of the args)

@Grazfather Grazfather merged commit ad51825 into hugsy:gdb_8_py36_code_refactor Jan 12, 2022
@theguy147 theguy147 deleted the gdb_8_py36_code_refactor branch January 12, 2022 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants