Skip to content

Modernize python code#607

Merged
eliben merged 5 commits intoeliben:mainfrom
pmhahn:modernize
Mar 15, 2025
Merged

Modernize python code#607
eliben merged 5 commits intoeliben:mainfrom
pmhahn:modernize

Conversation

@pmhahn
Copy link
Contributor

@pmhahn pmhahn commented Mar 13, 2025

While working on #514 I took the opportunity to modernize some old code:

  • Use named arguments instead of kwargs to allow typing them
  • Simplify pyelftools.construct.core.Range: mypy complained about pos not being declared in all cases; rewrite the logic.
  • Some variables are const, except while declaring them.
    • Build _printable with a dict-comprehension: Basically this is
    • Build _OPCODE_NAME_MAP with a dict-comprehension
  • Some more ruff findings
    • Optimize check for empty containers PLC1802 et.al.
    • Simplify sequence concatenation RUF005
    • Convert to list comprehensions PERF401
    • Convert yield from for-loop UP028

ℹ️ There is a small change in the output of elftools.dwarf.lineprogram.LineState.__repr__(), where I removed the trailing \n. If you want it back append another"" to the end of the tuple.

@pmhahn pmhahn force-pushed the modernize branch 2 times, most recently from 747ef02 to 567fa92 Compare March 14, 2025 16:00
raise RangeError("expected %d to %d, found %d" %
(self.mincount, self.maxcout, c), ex)
stream.seek(pos)
ctx = context.__copy__ if self.subcon.conflags & self.FLAG_COPY_CONTEXT else (lambda: context)
Copy link
Owner

Choose a reason for hiding this comment

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

If at all possible, I would prefer to minimize changes inside elftools/construct

Is there a simpler way to make the type checker happy? Just predeclaring pos?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Minimum would be an additional pos = -1 before the try, e.g.

--- a/elftools/construct/core.py
+++ b/elftools/construct/core.py
@@ -496,1 +496,1 @@ class Range(Subconstruct):
-        c = 0
+        c = pos = -1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now also in #609

pmhahn added 5 commits March 15, 2025 06:21
`ruff check --select UP028 --fix --unsafe-fixes`

- [UP028](https://docs.astral.sh/ruff/rules/yield-in-for-loop/)

Signed-off-by: Philipp Hahn <[email protected]>
`ruff check --select PERF401`

- [PERF401](https://docs.astral.sh/ruff/rules/manual-list-comprehension/)

Signed-off-by: Philipp Hahn <[email protected]>
`ruff check --select RUF005 --unsafe-fixes --fix`

- [RUF005](https://docs.astral.sh/ruff/rules/collection-literal-concatenation/)

Signed-off-by: Philipp Hahn <[email protected]>
`len(…) ==/!=/> 0` is O(n), while `not …` is O(1)
as long as … is not a generator.

- PLC1802](https://docs.astral.sh/ruff/rules/len-test/)

Signed-off-by: Philipp Hahn <[email protected]>
@eliben eliben merged commit bc8fca4 into eliben:main Mar 15, 2025
5 checks passed
@pmhahn pmhahn deleted the modernize branch March 17, 2025 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants