0

I have the below line which I assume works in python 2

m = re.search((
            # [Nr] Name              Type             Address           Offset
            r'^\s*(?P<number>\d+)'
            r'\s+(?P<name>[^\s]*)'
            r'\s+(?P<size>{hex_re})'
            r'\s+(?P<address>{hex_re})'
            r'\s+(?P<lma>{hex_re})'
            r'\s+(?P<offset>{hex_re})'
            r'\s+(?P<align>[^\s]+)'
            ).format(hex_re=hex_re), line)

On compilation I had the following error - TypeError: cannot use a string pattern on a bytes-like object

So I tried the following 2 solutions.

  • adding b like br'\s+(?P<name>[^\s]*)' but then it says byte object does not have format function
  • adding b and .decode('utf-8') like br'\s+(?P<name>[^\s]*)'.decode('utf-8'). Reiterates the original error.

I am trying to get this to run using python3.

Link to exact file: https://github.com/minimalchaos/SM-G970F_a12_kernel/blob/main/scripts/rkp_cfp/instrument.py

line object :

proc = subprocess.Popen([OBJDUMP, '--section-headers', vmlinux], stdout=subprocess.PIPE)
f = each_procline(proc)
it = iter(f)
line = next(it)
  • 1
    `line` is a bytes-like object. How is it created? – Axe319 Jan 11 '22 at 19:10
  • 1
    [`Popen.stdout`](https://docs.python.org/3/library/subprocess.html#subprocess.Popen.stdout) is a byte stream with the arguments given. It is read [here](https://github.com/minimalchaos/SM-G970F_a12_kernel/blob/ad09d19eb259ddcf473c8d1aba4cca1909aa57ee/scripts/rkp_cfp/instrument.py#L950). You need to use `line.decode('utf-8')` or whatever encoding it is giving you as your second arg to `re.search`. – Axe319 Jan 11 '22 at 19:23
  • Updated description with the relevant lines. I tried decode, updated the the exact line I used – minima1cha0s Jan 11 '22 at 19:23
  • Don't use `.decode` on the pattern. That is already a string. Use it on the `line`. – Axe319 Jan 11 '22 at 19:31
  • 1
    Thats works. my understanding was the pattern was being treated as bytes which is wrong. Thank you – minima1cha0s Jan 11 '22 at 21:26

0 Answers0