Skip to content

fix(util): drop the empty part smart_split appends, accept None in extract_arguments - #2648

Open
beduldul wants to merge 1 commit into
eternnoir:masterfrom
beduldul:fix/util-split-and-none
Open

beduldul wants to merge 1 commit into
eternnoir:masterfrom
beduldul:fix/util-split-and-none

Conversation

@beduldul

Copy link
Copy Markdown
Contributor

Description

Two input-handling bugs in telebot/util.py.

smart_split appends an empty part. It compared with <, so any text whose length is an exact multiple of chars_per_string went through the split path one last time on the remainder and appended '':

>>> smart_split("abc", 3)
['abc', '']          # expected ['abc']

>>> smart_split("a" * 4096)
['a'*4096, '']       # a full-length message

Sending that empty part to Telegram fails with Bad Request: message text is empty. Changed to <=, which covers every multiple, not just the 4096 case.

extract_arguments raises on None. It compiled the regexp and matched before checking the argument, so a non-text message (message.text is None) raised TypeError. is_command and extract_command already tolerate None, so this now matches them.

>>> extract_arguments(None)
TypeError: expected string or bytes-like object, got 'NoneType'

Describe your tests

Added tests/test_util.py — smart_split had no coverage at all. It walks exact multiples across several chunk sizes, checks the newline/space preference, the 4096 cap, and the None handling plus the normal extract_arguments cases.

Full suite: 314 passed, 73 skipped.

Python version: 3.12.3

OS: macOS 27.2

Checklist:

  • I added/edited example on new feature/change (if exists)
  • My changes won't break backward compatibility
  • I made changes both for sync and async

…tract_arguments

smart_split compared with `<`, so text whose length is an exact multiple of
chars_per_string went through the split path one last time on the remainder and
appended ''. smart_split("abc", 3) returned ["abc", ""], and sending that empty
part to Telegram fails with "message text is empty".

extract_arguments compiled its regexp and matched before looking at None, so a
non-text message raised TypeError on message.text. is_command and
extract_command already tolerate None, so this now matches them.
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.

1 participant