adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.07k stars 1.2k forks source link

Unexpected difference between cPython and Circuitpython when using count method on binary strings #9675

Open kamocat opened 13 hours ago

kamocat commented 13 hours ago

CircuitPython version

Adafruit CircuitPython 9.1.4 on 2024-09-17; Raspberry Pi Pico with rp2040

Code/REPL

a = b'A' * 32
print(a.count(b'A'))
print(a.count(65)) #A is ascii character 65

Behavior

The count function works with the byte string but not the integer. See:

32
Traceback (most recent call last):
  File "code.py", line 3, in <module>
TypeError: can't convert 'int' object to str implicitly

Description

In contrast, with cPython 3.12.6 in win32, the count works with both the integer and the byte string:

 >>> a = b'A' * 32
 >>> a.count(b'A')
 32
 >>> a.count(65)
 32

Additional information

https://docs.python.org/3/library/stdtypes.html#bytes.count states

The subsequence to search for may be any bytes-like object or an integer in the range 0 to 255.

dhalbert commented 12 hours ago

This behavior also exists in MicroPython, from which we take a lot of this kind of core functionality.