2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Fix style parsers randomly returning "unsorted" dicts.

This is due to Python <3.6 having "unsorted" dicts. Dicts are inherently
unsorted, but starting from Python 3.6 they keep the order in which the
keys are inserted (useful for unpacking)
This commit is contained in:
Dan 2018-12-31 17:13:50 +01:00
parent e5be7fcc90
commit 21dbbc3f0b
2 changed files with 12 additions and 8 deletions

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import re import re
from collections import OrderedDict
from pyrogram.api.types import ( from pyrogram.api.types import (
MessageEntityBold as Bold, MessageEntityBold as Bold,
@ -75,10 +76,11 @@ class HTML:
text = text.replace(match.group(), body) text = text.replace(match.group(), body)
offset += len(style) * 2 + 5 + (len(url) + 8 if url else 0) offset += len(style) * 2 + 5 + (len(url) + 8 if url else 0)
return dict( # TODO: OrderedDict to be removed in Python3.6
message=utils.remove_surrogates(text), return OrderedDict([
entities=entities ("message", utils.remove_surrogates(text)),
) ("entities", entities)
])
def unparse(self, message: str, entities: list): def unparse(self, message: str, entities: list):
message = utils.add_surrogates(message).strip() message = utils.add_surrogates(message).strip()

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import re import re
from collections import OrderedDict
from pyrogram.api.types import ( from pyrogram.api.types import (
MessageEntityBold as Bold, MessageEntityBold as Bold,
@ -97,10 +98,11 @@ class Markdown:
entities.append(entity) entities.append(entity)
message = message.replace(match.group(), body) message = message.replace(match.group(), body)
return dict( # TODO: OrderedDict to be removed in Python3.6
message=utils.remove_surrogates(message), return OrderedDict([
entities=entities ("message", utils.remove_surrogates(message)),
) ("entities", entities)
])
def unparse(self, message: str, entities: list): def unparse(self, message: str, entities: list):
message = utils.add_surrogates(message).strip() message = utils.add_surrogates(message).strip()