aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2019-03-27 13:18:11 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2019-04-08 15:11:57 +0200
commit2bdbd719a796777e72627281c163bf51b6d03114 (patch)
tree1782862a61993b27d1efec7d08e44b38406a6df8
parente40cfb66a19c2bf3b8c01f3e056ea56914aea18c (diff)
downloadwee-slack-2bdbd719a796777e72627281c163bf51b6d03114.tar.gz
Make encode_to/decode_from_utf8 noop in python 3
Python 3 receives unicode strings as arguments in weechat callbacks, websocet receive etc. and we can send unicode strings to weechat functions, websocket etc., so we don't need to do any of this converting.
-rw-r--r--_pytest/test_utf8_helpers.py119
-rw-r--r--wee_slack.py8
2 files changed, 76 insertions, 51 deletions
diff --git a/_pytest/test_utf8_helpers.py b/_pytest/test_utf8_helpers.py
index 15340a5..a2dea42 100644
--- a/_pytest/test_utf8_helpers.py
+++ b/_pytest/test_utf8_helpers.py
@@ -2,73 +2,94 @@
from __future__ import print_function, unicode_literals
+import sys
from collections import OrderedDict
from wee_slack import decode_from_utf8, encode_to_utf8, utf8_decode
-def test_decode_preserves_string_without_utf8():
- assert u'test' == decode_from_utf8(b'test')
+b_ae = 'æ'.encode('utf-8')
+b_oe = 'ø'.encode('utf-8')
+b_aa = 'å'.encode('utf-8')
+b_word = b_ae + b_oe + b_aa
-def test_decode_preserves_unicode_strings():
- assert u'æøå' == decode_from_utf8(u'æøå')
-def test_decode_preserves_mapping_type():
- value_dict = {'a': 'x', 'b': 'y', 'c': 'z'}
- value_ord_dict = OrderedDict(value_dict)
- assert type(value_dict) == type(decode_from_utf8(value_dict))
- assert type(value_ord_dict) == type(decode_from_utf8(value_ord_dict))
+if sys.version_info.major > 2:
+ def test_decode_should_not_transform_str():
+ assert 'æøå' == decode_from_utf8('æøå')
-def test_decode_preserves_iterable_type():
- value_set = {'a', 'b', 'c'}
- value_tuple = ('a', 'b', 'c')
- assert type(value_set) == type(decode_from_utf8(value_set))
- assert type(value_tuple) == type(decode_from_utf8(value_tuple))
+ def test_decode_should_not_transform_bytes():
+ assert b_word == decode_from_utf8(b_word)
-def test_decodes_utf8_string_to_unicode():
- assert u'æøå' == decode_from_utf8(b'æøå')
+ def test_encode_should_not_transform_str():
+ assert 'æøå' == encode_to_utf8('æøå')
-def test_decodes_utf8_dict_to_unicode():
- assert {u'æ': u'å', u'ø': u'å'} == decode_from_utf8({b'æ': b'å', b'ø': b'å'})
+ def test_encode_should_not_transform_bytes():
+ assert b_word == encode_to_utf8(b_word)
-def test_decodes_utf8_list_to_unicode():
- assert [u'æ', u'ø', u'å'] == decode_from_utf8([b'æ', b'ø', b'å'])
+else:
+ def test_decode_preserves_string_without_utf8():
+ assert 'test' == decode_from_utf8(b'test')
-def test_encode_preserves_string_without_utf8():
- assert b'test' == encode_to_utf8(u'test')
+ def test_decode_preserves_unicode_strings():
+ assert 'æøå' == decode_from_utf8('æøå')
-def test_encode_preserves_byte_strings():
- assert b'æøå' == encode_to_utf8(b'æøå')
+ def test_decode_preserves_mapping_type():
+ value_dict = {'a': 'x', 'b': 'y', 'c': 'z'}
+ value_ord_dict = OrderedDict(value_dict)
+ assert type(value_dict) == type(decode_from_utf8(value_dict))
+ assert type(value_ord_dict) == type(decode_from_utf8(value_ord_dict))
-def test_encode_preserves_mapping_type():
- value_dict = {'a': 'x', 'b': 'y', 'c': 'z'}
- value_ord_dict = OrderedDict(value_dict)
- assert type(value_dict) == type(encode_to_utf8(value_dict))
- assert type(value_ord_dict) == type(encode_to_utf8(value_ord_dict))
+ def test_decode_preserves_iterable_type():
+ value_set = {'a', 'b', 'c'}
+ value_tuple = ('a', 'b', 'c')
+ assert type(value_set) == type(decode_from_utf8(value_set))
+ assert type(value_tuple) == type(decode_from_utf8(value_tuple))
-def test_encode_preserves_iterable_type():
- value_set = {'a', 'b', 'c'}
- value_tuple = ('a', 'b', 'c')
- assert type(value_set) == type(encode_to_utf8(value_set))
- assert type(value_tuple) == type(encode_to_utf8(value_tuple))
+ def test_decodes_utf8_string_to_unicode():
+ assert 'æøå' == decode_from_utf8(b_word)
-def test_encodes_utf8_string_to_unicode():
- assert b'æøå' == encode_to_utf8(u'æøå')
+ def test_decodes_utf8_dict_to_unicode():
+ assert {'æ': 'å', 'ø': 'å'} == decode_from_utf8({b_ae: b_aa, b_oe: b_aa})
-def test_encodes_utf8_dict_to_unicode():
- assert {b'æ': b'å', b'ø': b'å'} == encode_to_utf8({u'æ': u'å', u'ø': u'å'})
+ def test_decodes_utf8_list_to_unicode():
+ assert ['æ', 'ø', 'å'] == decode_from_utf8([b_ae, b_oe, b_aa])
-def test_encodes_utf8_list_to_unicode():
- assert [b'æ', b'ø', b'å'] == encode_to_utf8([u'æ', u'ø', u'å'])
+ def test_encode_preserves_string_without_utf8():
+ assert b'test' == encode_to_utf8('test')
-@utf8_decode
-def method_with_utf8_decode(*args, **kwargs):
- return (args, kwargs)
+ def test_encode_preserves_byte_strings():
+ assert b_word == encode_to_utf8(b_word)
-def test_utf8_decode():
- args = (b'æ', b'ø', b'å')
- kwargs = {b'æ': b'å', b'ø': b'å'}
+ def test_encode_preserves_mapping_type():
+ value_dict = {'a': 'x', 'b': 'y', 'c': 'z'}
+ value_ord_dict = OrderedDict(value_dict)
+ assert type(value_dict) == type(encode_to_utf8(value_dict))
+ assert type(value_ord_dict) == type(encode_to_utf8(value_ord_dict))
- result_args, result_kwargs = method_with_utf8_decode(*args, **kwargs)
+ def test_encode_preserves_iterable_type():
+ value_set = {'a', 'b', 'c'}
+ value_tuple = ('a', 'b', 'c')
+ assert type(value_set) == type(encode_to_utf8(value_set))
+ assert type(value_tuple) == type(encode_to_utf8(value_tuple))
- assert result_args == decode_from_utf8(args)
- assert result_kwargs == decode_from_utf8(kwargs)
+ def test_encodes_utf8_string_to_unicode():
+ assert b_word == encode_to_utf8('æøå')
+
+ def test_encodes_utf8_dict_to_unicode():
+ assert {b_ae: b_aa, b_oe: b_aa} == encode_to_utf8({'æ': 'å', 'ø': 'å'})
+
+ def test_encodes_utf8_list_to_unicode():
+ assert [b_ae, b_oe, b_aa] == encode_to_utf8(['æ', 'ø', 'å'])
+
+ @utf8_decode
+ def method_with_utf8_decode(*args, **kwargs):
+ return (args, kwargs)
+
+ def test_utf8_decode():
+ args = (b_ae, b_oe, b_aa)
+ kwargs = {b_ae: b_aa, b_oe: b_aa}
+
+ result_args, result_kwargs = method_with_utf8_decode(*args, **kwargs)
+
+ assert result_args == decode_from_utf8(args)
+ assert result_kwargs == decode_from_utf8(kwargs)
diff --git a/wee_slack.py b/wee_slack.py
index 8b659f6..758c522 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -143,7 +143,9 @@ EMOJI = []
def encode_to_utf8(data):
- if isinstance(data, unicode):
+ if sys.version_info.major > 2:
+ return data
+ elif isinstance(data, unicode):
return data.encode('utf-8')
if isinstance(data, bytes):
return data
@@ -156,7 +158,9 @@ def encode_to_utf8(data):
def decode_from_utf8(data):
- if isinstance(data, bytes):
+ if sys.version_info.major > 2:
+ return data
+ elif isinstance(data, bytes):
return data.decode('utf-8')
if isinstance(data, unicode):
return data