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 # You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/]. # along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Game.""" """This module contains an object that represents a Telegram Game."""
import sys
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple
from telegram._files.animation import Animation from telegram._files.animation import Animation
@ -158,9 +157,6 @@ class Game(TelegramObject):
if not self.text: if not self.text:
raise RuntimeError("This Game has no '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 = self.text.encode("utf-16-le")
entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2] 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/]. # along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Message.""" """This module contains an object that represents a Telegram Message."""
import datetime import datetime
import sys
from html import escape from html import escape
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union
@ -3134,10 +3133,6 @@ class Message(TelegramObject):
if not self.text: if not self.text:
raise RuntimeError("This Message has no '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 = self.text.encode("utf-16-le")
entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2] entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2]
return entity_text.decode("utf-16-le") return entity_text.decode("utf-16-le")
@ -3164,10 +3159,6 @@ class Message(TelegramObject):
if not self.caption: if not self.caption:
raise RuntimeError("This Message has no '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 = self.caption.encode("utf-16-le")
entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2] entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2]
return entity_text.decode("utf-16-le") return entity_text.decode("utf-16-le")
@ -3244,8 +3235,7 @@ class Message(TelegramObject):
if message_text is None: if message_text is None:
return 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 = "" html_text = ""
last_offset = 0 last_offset = 0
@ -3301,21 +3291,14 @@ class Message(TelegramObject):
insert = escaped_text insert = escaped_text
if offset == 0: if offset == 0:
if sys.maxunicode == 0xFFFF: html_text += (
html_text += ( escape(
escape(message_text[last_offset : entity.offset - offset]) + insert message_text[ # type: ignore
last_offset * 2 : (entity.offset - offset) * 2
].decode("utf-16-le")
) )
else: + insert
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
else: else:
html_text += ( html_text += (
message_text[ # type: ignore message_text[ # type: ignore
@ -3327,14 +3310,9 @@ class Message(TelegramObject):
last_offset = entity.offset - offset + entity.length last_offset = entity.offset - offset + entity.length
if offset == 0: if offset == 0:
if sys.maxunicode == 0xFFFF: html_text += escape(
html_text += escape(message_text[last_offset:]) message_text[last_offset * 2 :].decode("utf-16-le") # type: ignore
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: else:
html_text += message_text[last_offset * 2 :].decode("utf-16-le") # type: ignore 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: if message_text is None:
return 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 = "" markdown_text = ""
last_offset = 0 last_offset = 0
@ -3519,25 +3496,15 @@ class Message(TelegramObject):
insert = escaped_text insert = escaped_text
if offset == 0: if offset == 0:
if sys.maxunicode == 0xFFFF: markdown_text += (
markdown_text += ( escape_markdown(
escape_markdown( message_text[ # type: ignore
message_text[last_offset : entity.offset - offset], version=version last_offset * 2 : (entity.offset - offset) * 2
) ].decode("utf-16-le"),
+ insert version=version,
) )
else: + insert
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
else: else:
markdown_text += ( markdown_text += (
message_text[ # type: ignore message_text[ # type: ignore
@ -3549,15 +3516,10 @@ class Message(TelegramObject):
last_offset = entity.offset - offset + entity.length last_offset = entity.offset - offset + entity.length
if offset == 0: if offset == 0:
if sys.maxunicode == 0xFFFF: markdown_text += escape_markdown(
markdown_text += escape_markdown(message_text[last_offset:], version=version) message_text[last_offset * 2 :].decode("utf-16-le"), # type: ignore
else: version=version,
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: else:
markdown_text += message_text[last_offset * 2 :].decode("utf-16-le") # type: ignore 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/]. # along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Poll.""" """This module contains an object that represents a Telegram Poll."""
import datetime import datetime
import sys
from typing import TYPE_CHECKING, ClassVar, Dict, List, Optional, Sequence, Tuple from typing import TYPE_CHECKING, ClassVar, Dict, List, Optional, Sequence, Tuple
from telegram import constants from telegram import constants
@ -300,9 +299,6 @@ class Poll(TelegramObject):
if not self.explanation: if not self.explanation:
raise RuntimeError("This Poll has no '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 = self.explanation.encode("utf-16-le")
entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2] entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2]