diff --git a/CHANGELOG.md b/CHANGELOG.md index d25791f..05b0044 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ #Changelog. +## Version 0.4.1c: ## +- Bug Fix: encoding.to_binary and to_unicode now transform other datatypes (like int) into that type as well. Fixes [issue #32](https://github.com/luckydonald/pytg/issues/32), and that closes [issue 31](https://github.com/luckydonald/pytg/issues/31) + ## Version 0.4.1b: ## - added ```Receiver.queued_messages()```, showing how many messages are waiting in the queue. diff --git a/pytg/__init__.py b/pytg/__init__.py index b96df62..2527655 100644 --- a/pytg/__init__.py +++ b/pytg/__init__.py @@ -12,7 +12,7 @@ __all__ = ["receiver", "sender", "Telegram"] -VERSION = "0.4.1b" +VERSION = "0.4.1c" class Telegram(object): """ diff --git a/pytg/encoding.py b/pytg/encoding.py index e798200..b49bf73 100644 --- a/pytg/encoding.py +++ b/pytg/encoding.py @@ -4,33 +4,37 @@ import sys if sys.version < '3': # python 2.7 - text_type = unicode - binary_type = str - native_type = binary_type - long_int = long - def to_native(x): - return to_binary(x) + text_type = unicode + binary_type = str + native_type = binary_type + long_int = long + def to_native(x): + return to_binary(x) else: # python 3 - text_type = str - binary_type = bytes - native_type = text_type - long_int = int - def to_native(x): - return to_unicode(x) + text_type = str + binary_type = bytes + native_type = text_type + long_int = int + def to_native(x): + return to_unicode(x) -def to_binary(x): - if isinstance(x, text_type): - return x.encode("utf-8") - else: - return x +def to_binary(x, force=False): + if isinstance(x, text_type): + return x.encode("utf-8") + elif isinstance(x, binary_type): + return x + else: + return to_binary(str(x)) def to_unicode(x): - if isinstance(x, binary_type): - if x == b'\\': - return u"\\" - return x.decode("utf-8") - else: - return x + if isinstance(x, binary_type): + if x == b'\\': + return u"\\" + return x.decode("utf-8") + elif isinstance(x, text_type): + return x + else: + return to_unicode(str(x))