Repository: python-telegram-bot/python-telegram-bot

Issue:
[REFACTOR] Drop checking of `sys.maxunicode` in parsing of entities
### What kind of feature are you missing? Where do you notice a shortcoming of PTB?

We currently check for `sys.maxunicode` whenever we parse entities:
https://github.com/python-telegram-bot/python-telegram-bot/blob/33b677aeb4a3e601668103914a70e633908e90b2/telegram/_message.py#L3128-L3130

However, [`sys.maxunicode`](https://docs.python.org/3/library/sys.html#sys.maxunicode) is guaranteed to be 0x10FFFF since Python 3.3. 

We needed a check for this back when we still supported older python versions (git blame tells that this line was last touched in 2016).

So, we do not need to check for this anymore, and it can thus be dropped.

### Describe the solution you'd like

Remove logic checking for `sys.maxunicode`. 

### Describe alternatives you've considered

_No response_

### Additional context

Originally discovered in https://github.com/python-telegram-bot/python-telegram-bot/pull/3594#discussion_r1141416231.

Added benefit of improving coverage, since this wasn't covered in our tests anyway.


Discussion:

Resolving patch:
diff --git a/telegram/_games/game.py b/telegram/_games/game.py
--- 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 @@ def parse_text_entity(self, entity: MessageEntity) -> str:
         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
--- 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 @@ def parse_entity(self, entity: MessageEntity) -> str:
         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 @@ def parse_caption_entity(self, entity: MessageEntity) -> str:
         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 @@ def _parse_html(
         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 @@ def _parse_html(
                     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
-                                    last_offset * 2 : (entity.offset - offset) * 2
-                                ].decode("utf-16-le")
-                            )
-                            + insert
+                    html_text += (
+                        escape(
+                            message_text[  # type: ignore
+                                last_offset * 2 : (entity.offset - offset) * 2
+                            ].decode("utf-16-le")
                         )
-                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 @@ def _parse_html(
                 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 @@ def _parse_markdown(
         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 @@ def _parse_markdown(
                     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
-                                    last_offset * 2 : (entity.offset - offset) * 2
-                                ].decode("utf-16-le"),
-                                version=version,
-                            )
-                            + insert
+                    markdown_text += (
+                        escape_markdown(
+                            message_text[  # type: ignore
+                                last_offset * 2 : (entity.offset - offset) * 2
+                            ].decode("utf-16-le"),
+                            version=version,
                         )
-                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 @@ def _parse_markdown(
                 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
--- 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 @@ def parse_explanation_entity(self, entity: MessageEntity) -> str:
         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]
 


Test patch: