Skip to content

Commit 37c5b44

Browse files
committed
construct: Convert to f-string (manual)
Manually fix the remaining code which `ruff check --select UP031,UP032,UP031 --unsafe-fixes --fix` did not update. Convert `repr(…)` to `{…!r}`. Signed-off-by: Philipp Hahn <phahn-oss@avm.de>
1 parent 9802ce3 commit 37c5b44

3 files changed

Lines changed: 9 additions & 12 deletions

File tree

elftools/construct/core.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,14 @@ def _read_stream(stream: IO[bytes], length: int) -> bytes:
313313
raise ValueError("length must be >= 0", length)
314314
data = stream.read(length)
315315
if len(data) != length:
316-
raise FieldError("expected %d, found %d" % (length, len(data)))
316+
raise FieldError(f"expected {length}, found {len(data)}")
317317
return data
318318

319319
def _write_stream(stream: IO[bytes], length: int, data: bytes) -> None:
320320
if length < 0:
321321
raise ValueError("length must be >= 0", length)
322322
if len(data) != length:
323-
raise FieldError("expected %d, found %d" % (length, len(data)))
323+
raise FieldError(f"expected {length}, found {len(data)}")
324324
stream.write(data)
325325

326326
class StaticField(Construct):
@@ -452,12 +452,12 @@ def _parse(self, stream: IO[bytes], context: Container) -> ListContainer:
452452
obj.append(self.subcon._parse(stream, context))
453453
c += 1
454454
except ConstructError as ex:
455-
raise ArrayError("expected %d, found %d" % (count, c), ex)
455+
raise ArrayError(f"expected {count}, found {c}", ex)
456456
return obj
457457
def _build(self, obj: Any, stream: IO[bytes], context: Container) -> None:
458458
count = self.countfunc(context)
459459
if len(obj) != count:
460-
raise ArrayError("expected %d, found %d" % (count, len(obj)))
460+
raise ArrayError(f"expected {count}, found {len(obj)}")
461461
if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
462462
for subobj in obj:
463463
self.subcon._build(subobj, stream, context.__copy__())
@@ -534,14 +534,12 @@ def _parse(self, stream: IO[bytes], context: Container) -> ListContainer:
534534
c += 1
535535
except ConstructError as ex:
536536
if c < self.mincount:
537-
raise RangeError("expected %d to %d, found %d" %
538-
(self.mincount, self.maxcout, c), ex)
537+
raise RangeError(f"expected {self.mincount} to {self.maxcout}, found {c}", ex)
539538
stream.seek(pos)
540539
return obj
541540
def _build(self, obj: Any, stream: IO[bytes], context: Container) -> None:
542541
if len(obj) < self.mincount or len(obj) > self.maxcout:
543-
raise RangeError("expected %d to %d, found %d" %
544-
(self.mincount, self.maxcout, len(obj)))
542+
raise RangeError(f"expected {self.mincount} to {self.maxcout}, found {len(obj)}")
545543
cnt = 0
546544
try:
547545
if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
@@ -558,8 +556,7 @@ def _build(self, obj: Any, stream: IO[bytes], context: Container) -> None:
558556
cnt += 1
559557
except ConstructError as ex:
560558
if cnt < self.mincount:
561-
raise RangeError("expected %d to %d, found %d" %
562-
(self.mincount, self.maxcout, len(obj)), ex)
559+
raise RangeError(f"expected {self.mincount} to {self.maxcout}, found {len(obj)}", ex)
563560
def _sizeof(self, context: Container) -> int:
564561
raise SizeofError("can't calculate size")
565562

elftools/construct/debug.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(self, name: str | None = None, show_stream: bool = True,
4848
Construct.__init__(self, None)
4949
if name is None:
5050
Probe.counter += 1
51-
name = "<unnamed %d>" % (Probe.counter,)
51+
name = f"<unnamed {Probe.counter}>"
5252
self.printname = name
5353
self.show_stream = show_stream
5454
self.show_context = show_context

elftools/construct/lib/container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def copy(self) -> Self:
103103
__copy__ = copy
104104

105105
def __repr__(self) -> str:
106-
return f"{self.__class__.__name__}({repr(self.__dict__)})"
106+
return f"{self.__class__.__name__}({self.__dict__!r})"
107107

108108
def __str__(self) -> str:
109109
return f"{self.__class__.__name__}({str(self.__dict__)})"

0 commit comments

Comments
 (0)