From 11f86b88133d5a3b99767d063f9d78e57131f48a Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Mon, 27 Mar 2023 01:28:23 +0530 Subject: [PATCH] Drop Usage of `sys.maxunicode` (#3630) --- telegram/_games/game.py | 4 -- telegram/_message.py | 86 ++++++++++++----------------------------- telegram/_poll.py | 4 -- 3 files changed, 24 insertions(+), 70 deletions(-) diff --git a/telegram/_games/game.py b/telegram/_games/game.py index d5a6b3875..9efd4871b 100644 --- a/telegram/_games/game.py +++ b/telegram/_games/game.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains an object that represents a Telegram Game.""" -import sys from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple from telegram._files.animation import Animation @@ -158,9 +157,6 @@ class Game(TelegramObject): if not self.text: raise RuntimeError("This Game has no 'text'.") - # Is it a narrow build, if so we don't need to convert - if sys.maxunicode == 0xFFFF: - return self.text[entity.offset : entity.offset + entity.length] entity_text = self.text.encode("utf-16-le") entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2] diff --git a/telegram/_message.py b/telegram/_message.py index 7d1a01420..31425731e 100644 --- a/telegram/_message.py +++ b/telegram/_message.py @@ -19,7 +19,6 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains an object that represents a Telegram Message.""" import datetime -import sys from html import escape from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union @@ -3134,10 +3133,6 @@ class Message(TelegramObject): if not self.text: raise RuntimeError("This Message has no 'text'.") - # Is it a narrow build, if so we don't need to convert - if sys.maxunicode == 0xFFFF: - return self.text[entity.offset : entity.offset + entity.length] - entity_text = self.text.encode("utf-16-le") entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2] return entity_text.decode("utf-16-le") @@ -3164,10 +3159,6 @@ class Message(TelegramObject): if not self.caption: raise RuntimeError("This Message has no 'caption'.") - # Is it a narrow build, if so we don't need to convert - if sys.maxunicode == 0xFFFF: - return self.caption[entity.offset : entity.offset + entity.length] - entity_text = self.caption.encode("utf-16-le") entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2] return entity_text.decode("utf-16-le") @@ -3244,8 +3235,7 @@ class Message(TelegramObject): if message_text is None: return None - if sys.maxunicode != 0xFFFF: - message_text = message_text.encode("utf-16-le") # type: ignore + message_text = message_text.encode("utf-16-le") # type: ignore html_text = "" last_offset = 0 @@ -3301,21 +3291,14 @@ class Message(TelegramObject): insert = escaped_text if offset == 0: - if sys.maxunicode == 0xFFFF: - html_text += ( - escape(message_text[last_offset : entity.offset - offset]) + insert + html_text += ( + escape( + message_text[ # type: ignore + last_offset * 2 : (entity.offset - offset) * 2 + ].decode("utf-16-le") ) - else: - html_text += ( - escape( - message_text[ # type: ignore - last_offset * 2 : (entity.offset - offset) * 2 - ].decode("utf-16-le") - ) - + insert - ) - elif sys.maxunicode == 0xFFFF: - html_text += message_text[last_offset : entity.offset - offset] + insert + + insert + ) else: html_text += ( message_text[ # type: ignore @@ -3327,14 +3310,9 @@ class Message(TelegramObject): last_offset = entity.offset - offset + entity.length if offset == 0: - if sys.maxunicode == 0xFFFF: - html_text += escape(message_text[last_offset:]) - else: - html_text += escape( - message_text[last_offset * 2 :].decode("utf-16-le") # type: ignore - ) - elif sys.maxunicode == 0xFFFF: - html_text += message_text[last_offset:] + html_text += escape( + message_text[last_offset * 2 :].decode("utf-16-le") # type: ignore + ) else: html_text += message_text[last_offset * 2 :].decode("utf-16-le") # type: ignore @@ -3429,8 +3407,7 @@ class Message(TelegramObject): if message_text is None: return None - if sys.maxunicode != 0xFFFF: - message_text = message_text.encode("utf-16-le") # type: ignore + message_text = message_text.encode("utf-16-le") # type: ignore markdown_text = "" last_offset = 0 @@ -3519,25 +3496,15 @@ class Message(TelegramObject): insert = escaped_text if offset == 0: - if sys.maxunicode == 0xFFFF: - markdown_text += ( - escape_markdown( - message_text[last_offset : entity.offset - offset], version=version - ) - + insert + markdown_text += ( + escape_markdown( + message_text[ # type: ignore + last_offset * 2 : (entity.offset - offset) * 2 + ].decode("utf-16-le"), + version=version, ) - else: - markdown_text += ( - escape_markdown( - message_text[ # type: ignore - last_offset * 2 : (entity.offset - offset) * 2 - ].decode("utf-16-le"), - version=version, - ) - + insert - ) - elif sys.maxunicode == 0xFFFF: - markdown_text += message_text[last_offset : entity.offset - offset] + insert + + insert + ) else: markdown_text += ( message_text[ # type: ignore @@ -3549,15 +3516,10 @@ class Message(TelegramObject): last_offset = entity.offset - offset + entity.length if offset == 0: - if sys.maxunicode == 0xFFFF: - markdown_text += escape_markdown(message_text[last_offset:], version=version) - else: - markdown_text += escape_markdown( - message_text[last_offset * 2 :].decode("utf-16-le"), # type: ignore - version=version, - ) - elif sys.maxunicode == 0xFFFF: - markdown_text += message_text[last_offset:] + markdown_text += escape_markdown( + message_text[last_offset * 2 :].decode("utf-16-le"), # type: ignore + version=version, + ) else: markdown_text += message_text[last_offset * 2 :].decode("utf-16-le") # type: ignore diff --git a/telegram/_poll.py b/telegram/_poll.py index 2b1b8a787..2182d3146 100644 --- a/telegram/_poll.py +++ b/telegram/_poll.py @@ -18,7 +18,6 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains an object that represents a Telegram Poll.""" import datetime -import sys from typing import TYPE_CHECKING, ClassVar, Dict, List, Optional, Sequence, Tuple from telegram import constants @@ -300,9 +299,6 @@ class Poll(TelegramObject): if not self.explanation: raise RuntimeError("This Poll has no 'explanation'.") - # Is it a narrow build, if so we don't need to convert - if sys.maxunicode == 0xFFFF: - return self.explanation[entity.offset : entity.offset + entity.length] entity_text = self.explanation.encode("utf-16-le") entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2]