Is this a bug or a feature?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
martincho
Posts: 96
Joined: Mon May 16, 2022 9:59 pm

Is this a bug or a feature?

Post by martincho » Fri Jun 03, 2022 2:05 am

Quite a painful day debugging some uasyncio code...only to run into this difference between MicroPython and Python 3.9:

Code: Select all

def test():
    x = b'\x02abcdefg\x03'

    print("--------------------------------------------")
    print(f"x:                  {x}")
    print("--------------------------------------------")
    print(f"type(x):            {type(x)}")
    print(f"len(x:              {len(x)}")

    print(f"x[0]:               {x[0]}")

    print(f"x[0] == 0x02:       {x[0] == 0x02}")
    print(f"type(x[0]):         {type(x[0])}")
    print(f"x[0] == 2:          {x[0] == 2}")

    n1 = b'a'
    print(f"x.index('a'):       {x.index(n1)}")

    n2 = b'\x02'
    print(f"x.index(b'\\0x02'):  {x.index(n2)}")

    # These four lines will throw exceptions on MicroPython
    # They run without problems on Python 3.9
    print(f"x.index(ord('a')):  {x.index(ord('a'))}")
    print(f"x.index(97):        {x.index(97)}")
    print(f"x.index(0x02):      {x.index(0x02)}")
    print(f"x.index(2):         {x.index(2)}")
test()
Python 3.9 output:

Code: Select all

--------------------------------------------
x:                  b'\x02abcdefg\x03'
--------------------------------------------
type(x):            <class 'bytes'>
len(x:              9
x[0]:               2
x[0] == 0x02:       True
type(x[0]):         <class 'int'>
x[0] == 2:          True
x.index('a'):       1
x.index(b'\0x02'):  0
x.index(ord('a')):  1
x.index(97):        1
x.index(0x02):      0
x.index(2):         0

Process finished with exit code 0
MicroPython output:

Code: Select all

--------------------------------------------
x:                  b'\x02abcdefg\x03'
--------------------------------------------
type(x):            <class 'bytes'>
len(x:              9
x[0]:               2
x[0] == 0x02:       True
type(x[0]):         <class 'int'>
x[0] == 2:          True
x.index('a'):       1
x.index(b'\0x02'):  0
Traceback (most recent call last):
  File "<stdin>", line 644, in <module>
  File "<stdin>", line 639, in test
TypeError: can't convert 'int' object to str implicitly

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Is this a bug or a feature?

Post by mattyt » Fri Jun 03, 2022 12:04 pm

I think it's a bug. From PEP 358, Specification:

Code: Select all

.index(bytes | int) -> int
So my understanding is that index should accept an int. (PEP 3137 doesn't seem to contradict this behaviour.)

For the record I've also tested against 3.10; same result as your test against 3.9.

PR's welcome! ;)

Actually, even your (currently failing) test would be helpful in the test suite - I don't see any tests covering bytes.index there.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Is this a bug or a feature?

Post by scruss » Fri Jun 03, 2022 12:05 pm

The rather improbable construct

Code: Select all

x.index(ord('a').to_bytes(1,'big'))
is compatible with both.

martincho
Posts: 96
Joined: Mon May 16, 2022 9:59 pm

Re: Is this a bug or a feature?

Post by martincho » Fri Jun 03, 2022 1:54 pm

mattyt wrote:
Fri Jun 03, 2022 12:04 pm
I think it's a bug. From PEP 358, Specification:

Code: Select all

.index(bytes | int) -> int
So my understanding is that index should accept an int. (PEP 3137 doesn't seem to contradict this behaviour.)

For the record I've also tested against 3.10; same result as your test against 3.9.

PR's welcome! ;)

Actually, even your (currently failing) test would be helpful in the test suite - I don't see any tests covering bytes.index there.
What's the best way to report a potential bug?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Is this a bug or a feature?

Post by jimmo » Mon Jun 20, 2022 6:47 am

martincho wrote:
Fri Jun 03, 2022 1:54 pm
What's the best way to report a potential bug?
I didn't see anything raised about this on GitHub, but just to chime in from the implementation side...

The line between "bug" and "feature that isn't implemented yet" and "feature that won't be implemented in MicroPython" is quite blurry!

The CPython docs (https://docs.python.org/3/library/stdty ... ytes.index) are very clear about the index form of index() and my inclination is that we should support it.

MicroPython currently explicitly requires that the types match -- see implementation here https://github.com/micropython/micropyt ... str.c#L701 It would be simple to address this, either using memchr or by creating a temporary buffer for the needle.

martincho
Posts: 96
Joined: Mon May 16, 2022 9:59 pm

Re: Is this a bug or a feature?

Post by martincho » Mon Jun 20, 2022 8:35 am

I thought I reported it on github. The last few weeks have been a blur. I'll put it back on my list.

Thanks.

Post Reply