Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape demonstration bytes in BleakClient docstring. #1663

Merged
merged 1 commit into from
Oct 10, 2024

Conversation

MrSmoer
Copy link
Contributor

@MrSmoer MrSmoer commented Oct 10, 2024

The bytes inside the example weren't properly escaped. Some IDE's threw an error, readthedocs showed this as empty.

@dlech
Copy link
Collaborator

dlech commented Oct 10, 2024

I think the bug is in the documentation renderer.

b"\x00\x01\x02\x03" is perfectly valid Python code. The intention it to have a byte array with 4 bytes, namely 0, 1, 2, 3. With your suggested change, it would be a byte array with 16 bytes.

>>> b"\x00\x01\x02\x03" == bytes([0, 1, 2, 3])
True
>>> b"\\x00\\x01\\x02\\x03" == bytes([0, 1, 2, 3])
False
>>> b"\\x00\\x01\\x02\\x03" == bytes([92, 120, 48, 48, 92, 120, 48, 49, 92, 120, 48, 50, 92, 120, 48, 51])
True

@MrSmoer
Copy link
Contributor Author

MrSmoer commented Oct 10, 2024

This is not supposed to be python code, this is supposed to be a string containing python code (it is wrapped with """ which creates a multiline string).
In the context of the docstring you want it to be python code when it is displayed, eg. for displaying as \x01 you want it to be made of: a backslash character, an x character, a zero character, a one character,
If you want to create a backslash character to display as a backslash inside a string in python, you need to escape it by putting another backslash.
Before, the \x inside the string tells python to make the next character be the the next two hexdigits as ascii code.
Ascii code of 0-3 is in order: Null character, Start of Heading, Start of Text, End of Text. So this creates a string containing those four control characters, which don't properly display as anything - not on the readthedocs, not in my IDE's type-hint and not in my Terminal.

>>> print("""await client.write_gatt_char(MY_CHAR_UUID, b"\\x00\\x01\\x02\\x03", response=True)""")
await client.write_gatt_char(MY_CHAR_UUID, b"\x00\x01\x02\x03", response=True)
>>> print("""await client.write_gatt_char(MY_CHAR_UUID, b"\x00\x01\x02\x03", response=True)""")
await client.write_gatt_char(MY_CHAR_UUID, b"", response=True)
>>> "\x61" # 0x61 is 97 in decimal which decodes to 'a' in ascii 
'a'
>>> 

@MrSmoer
Copy link
Contributor Author

MrSmoer commented Oct 10, 2024

Besides, thank you for creating this library!
It just saved me after fighting with another python BLE library for hours. Someone told me about bleak and it just worked.

@dlech
Copy link
Collaborator

dlech commented Oct 10, 2024

This is not supposed to be python code, this is supposed to be a string containing python code

Ah, of course! My mistake.

@dlech
Copy link
Collaborator

dlech commented Oct 10, 2024

Instead of adding escapes, could we change it to a raw string instead (r""")? Then people could still copy-paste the example from the source code.

The bytes inside the example weren't properly escaped using a raw string helps. Some IDE's threw an error, readthedocs showed this as empty.
@MrSmoer MrSmoer force-pushed the escape-examples-in-docs branch from d008c76 to 7bc484e Compare October 10, 2024 22:19
@MrSmoer
Copy link
Contributor Author

MrSmoer commented Oct 10, 2024

Sure, an r-string is definitely better

@dlech dlech merged commit 8073b41 into hbldh:develop Oct 10, 2024
19 checks passed
@dlech
Copy link
Collaborator

dlech commented Oct 10, 2024

Thanks!

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