Drop Usage of sys.maxunicode (#3630)

This commit is contained in:
Harshil 2023-03-27 01:28:23 +05:30 committed by GitHub
parent 9997a9f47e
commit 11f86b8813
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 70 deletions

View file

@ -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]

View file

@ -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,7 +3235,6 @@ class Message(TelegramObject):
if message_text is None:
return None
if sys.maxunicode != 0xFFFF:
message_text = message_text.encode("utf-16-le") # type: ignore
html_text = ""
@ -3301,11 +3291,6 @@ class Message(TelegramObject):
insert = escaped_text
if offset == 0:
if sys.maxunicode == 0xFFFF:
html_text += (
escape(message_text[last_offset : entity.offset - offset]) + insert
)
else:
html_text += (
escape(
message_text[ # type: ignore
@ -3314,8 +3299,6 @@ class Message(TelegramObject):
)
+ insert
)
elif sys.maxunicode == 0xFFFF:
html_text += message_text[last_offset : entity.offset - offset] + 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:]
else:
html_text += message_text[last_offset * 2 :].decode("utf-16-le") # type: ignore
@ -3429,7 +3407,6 @@ class Message(TelegramObject):
if message_text is None:
return None
if sys.maxunicode != 0xFFFF:
message_text = message_text.encode("utf-16-le") # type: ignore
markdown_text = ""
@ -3519,14 +3496,6 @@ 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
)
else:
markdown_text += (
escape_markdown(
message_text[ # type: ignore
@ -3536,8 +3505,6 @@ class Message(TelegramObject):
)
+ insert
)
elif sys.maxunicode == 0xFFFF:
markdown_text += message_text[last_offset : entity.offset - offset] + 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:]
else:
markdown_text += message_text[last_offset * 2 :].decode("utf-16-le") # type: ignore

View file

@ -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]