aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/strings
diff options
context:
space:
mode:
authordvshkurko <dvshkurko@yandex-team.ru>2022-02-10 16:45:51 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:45:51 +0300
commit321ee9bce31ec6e238be26dbcbe539cffa2c3309 (patch)
tree14407a2757cbf29eb97e266b7f07e851f971000c /library/python/strings
parent2f6ca198245aeffd5e2d82b65927c2465b68b4f5 (diff)
downloadydb-321ee9bce31ec6e238be26dbcbe539cffa2c3309.tar.gz
Restoring authorship annotation for <dvshkurko@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/python/strings')
-rw-r--r--library/python/strings/strings.py54
-rw-r--r--library/python/strings/ut/test_strings.py58
-rw-r--r--library/python/strings/ut/ya.make2
-rw-r--r--library/python/strings/ya.make2
4 files changed, 58 insertions, 58 deletions
diff --git a/library/python/strings/strings.py b/library/python/strings/strings.py
index 5bfddfe78a..2e018ae038 100644
--- a/library/python/strings/strings.py
+++ b/library/python/strings/strings.py
@@ -1,6 +1,6 @@
import locale
import logging
-import six
+import six
import sys
import codecs
@@ -23,15 +23,15 @@ def left_strip(el, prefix):
# Explicit to-text conversion
-# Chooses between str/unicode, i.e. six.binary_type/six.text_type
+# Chooses between str/unicode, i.e. six.binary_type/six.text_type
def to_basestring(value):
- if isinstance(value, (six.binary_type, six.text_type)):
+ if isinstance(value, (six.binary_type, six.text_type)):
return value
try:
- if six.PY2:
- return unicode(value)
- else:
- return str(value)
+ if six.PY2:
+ return unicode(value)
+ else:
+ return str(value)
except UnicodeDecodeError:
try:
return str(value)
@@ -41,35 +41,35 @@ to_text = to_basestring
def to_unicode(value, from_enc=DEFAULT_ENCODING):
- if isinstance(value, six.text_type):
+ if isinstance(value, six.text_type):
return value
- if isinstance(value, six.binary_type):
- if six.PY2:
- return unicode(value, from_enc, ENCODING_ERRORS_POLICY)
- else:
- return value.decode(from_enc, errors=ENCODING_ERRORS_POLICY)
- return six.text_type(value)
+ if isinstance(value, six.binary_type):
+ if six.PY2:
+ return unicode(value, from_enc, ENCODING_ERRORS_POLICY)
+ else:
+ return value.decode(from_enc, errors=ENCODING_ERRORS_POLICY)
+ return six.text_type(value)
# Optional from_enc enables transcoding
def to_str(value, to_enc=DEFAULT_ENCODING, from_enc=None):
- if isinstance(value, six.binary_type):
+ if isinstance(value, six.binary_type):
if from_enc is None or to_enc == from_enc:
# Unknown input encoding or input and output encoding are the same
return value
value = to_unicode(value, from_enc=from_enc)
- if isinstance(value, six.text_type):
+ if isinstance(value, six.text_type):
return value.encode(to_enc, ENCODING_ERRORS_POLICY)
- return six.binary_type(value)
+ return six.binary_type(value)
def _convert_deep(x, enc, convert, relaxed=True):
if x is None:
return None
- if isinstance(x, (six.text_type, six.binary_type)):
+ if isinstance(x, (six.text_type, six.binary_type)):
return convert(x, enc)
if isinstance(x, dict):
- return {convert(k, enc): _convert_deep(v, enc, convert, relaxed) for k, v in six.iteritems(x)}
+ return {convert(k, enc): _convert_deep(v, enc, convert, relaxed) for k, v in six.iteritems(x)}
if isinstance(x, list):
return [_convert_deep(e, enc, convert, relaxed) for e in x]
if isinstance(x, tuple):
@@ -91,13 +91,13 @@ def stringize_deep(x, enc=DEFAULT_ENCODING, relaxed=True):
@library.python.func.memoize()
def locale_encoding():
try:
- loc = locale.getdefaultlocale()[1]
- if loc:
- codecs.lookup(loc)
- return loc
- except LookupError as e:
- logger.debug('Cannot get system locale: %s', e)
- return None
+ loc = locale.getdefaultlocale()[1]
+ if loc:
+ codecs.lookup(loc)
+ return loc
+ except LookupError as e:
+ logger.debug('Cannot get system locale: %s', e)
+ return None
except ValueError as e:
logger.warn('Cannot get system locale: %s', e)
return None
@@ -124,6 +124,6 @@ def get_stream_encoding(stream):
def encode(value, encoding=DEFAULT_ENCODING):
- if isinstance(value, six.binary_type):
+ if isinstance(value, six.binary_type):
value = value.decode(encoding, errors='ignore')
return value.encode(encoding)
diff --git a/library/python/strings/ut/test_strings.py b/library/python/strings/ut/test_strings.py
index dd0c694ee1..9a862ed060 100644
--- a/library/python/strings/ut/test_strings.py
+++ b/library/python/strings/ut/test_strings.py
@@ -1,7 +1,7 @@
# coding=utf-8
import pytest
-import six
+import six
import library.python.strings
@@ -34,11 +34,11 @@ class NonConvertible(ConvertibleToUnicodeOnly, ConvertibleToStrOnly):
def test_to_basestring():
assert library.python.strings.to_basestring('str') == 'str'
assert library.python.strings.to_basestring(u'юникод') == u'юникод'
- if six.PY2: # __str__ should return str not bytes in Python3
- assert library.python.strings.to_basestring(Convertible()) == Convertible.text
- assert library.python.strings.to_basestring(ConvertibleToUnicodeOnly()) == Convertible.text
- assert library.python.strings.to_basestring(ConvertibleToStrOnly()) == Convertible.text_utf8
- assert library.python.strings.to_basestring(NonConvertible())
+ if six.PY2: # __str__ should return str not bytes in Python3
+ assert library.python.strings.to_basestring(Convertible()) == Convertible.text
+ assert library.python.strings.to_basestring(ConvertibleToUnicodeOnly()) == Convertible.text
+ assert library.python.strings.to_basestring(ConvertibleToStrOnly()) == Convertible.text_utf8
+ assert library.python.strings.to_basestring(NonConvertible())
def test_to_unicode():
@@ -46,13 +46,13 @@ def test_to_unicode():
assert library.python.strings.to_unicode('str') == u'str'
assert library.python.strings.to_unicode(u'строка'.encode('utf-8')) == u'строка'
assert library.python.strings.to_unicode(u'строка'.encode('cp1251'), 'cp1251') == u'строка'
- if six.PY2: # __str__ should return str not bytes in Python3
- assert library.python.strings.to_unicode(Convertible()) == Convertible.text
- assert library.python.strings.to_unicode(ConvertibleToUnicodeOnly()) == Convertible.text
- with pytest.raises(UnicodeDecodeError):
- library.python.strings.to_unicode(ConvertibleToStrOnly())
- with pytest.raises(UnicodeDecodeError):
- library.python.strings.to_unicode(NonConvertible())
+ if six.PY2: # __str__ should return str not bytes in Python3
+ assert library.python.strings.to_unicode(Convertible()) == Convertible.text
+ assert library.python.strings.to_unicode(ConvertibleToUnicodeOnly()) == Convertible.text
+ with pytest.raises(UnicodeDecodeError):
+ library.python.strings.to_unicode(ConvertibleToStrOnly())
+ with pytest.raises(UnicodeDecodeError):
+ library.python.strings.to_unicode(NonConvertible())
def test_to_unicode_errors_replace():
@@ -61,17 +61,17 @@ def test_to_unicode_errors_replace():
def test_to_str():
- assert library.python.strings.to_str('str') == 'str' if six.PY2 else b'str'
- assert library.python.strings.to_str(u'unicode') == 'unicode' if six.PY2 else b'unicode'
+ assert library.python.strings.to_str('str') == 'str' if six.PY2 else b'str'
+ assert library.python.strings.to_str(u'unicode') == 'unicode' if six.PY2 else b'unicode'
assert library.python.strings.to_str(u'юникод') == u'юникод'.encode('utf-8')
assert library.python.strings.to_str(u'юникод', 'cp1251') == u'юникод'.encode('cp1251')
- if six.PY2:
- assert library.python.strings.to_str(Convertible()) == Convertible.text_utf8
- with pytest.raises(UnicodeEncodeError):
- library.python.strings.to_str(ConvertibleToUnicodeOnly())
- assert library.python.strings.to_str(ConvertibleToStrOnly()) == Convertible.text_utf8
- with pytest.raises(UnicodeEncodeError):
- library.python.strings.to_str(NonConvertible())
+ if six.PY2:
+ assert library.python.strings.to_str(Convertible()) == Convertible.text_utf8
+ with pytest.raises(UnicodeEncodeError):
+ library.python.strings.to_str(ConvertibleToUnicodeOnly())
+ assert library.python.strings.to_str(ConvertibleToStrOnly()) == Convertible.text_utf8
+ with pytest.raises(UnicodeEncodeError):
+ library.python.strings.to_str(NonConvertible())
def test_to_str_errors_replace():
@@ -80,8 +80,8 @@ def test_to_str_errors_replace():
def test_to_str_transcode():
- assert library.python.strings.to_str('str', from_enc='ascii') == 'str' if six.PY2 else b'str'
- assert library.python.strings.to_str('str', from_enc='utf-8') == 'str' if six.PY2 else b'str'
+ assert library.python.strings.to_str('str', from_enc='ascii') == 'str' if six.PY2 else b'str'
+ assert library.python.strings.to_str('str', from_enc='utf-8') == 'str' if six.PY2 else b'str'
assert library.python.strings.to_str(u'юникод'.encode('utf-8'), from_enc='utf-8') == u'юникод'.encode('utf-8')
assert library.python.strings.to_str(u'юникод'.encode('utf-8'), to_enc='utf-8', from_enc='utf-8') == u'юникод'.encode('utf-8')
@@ -117,9 +117,9 @@ def test_stringize_deep():
u'ключ 2': u'значение 2',
'list': [u'ключ 2', 'key 1', (u'к', 2)]
}) == {
- 'key 1' if six.PY2 else b'key 1': 'value 1' if six.PY2 else b'value 1',
+ 'key 1' if six.PY2 else b'key 1': 'value 1' if six.PY2 else b'value 1',
u'ключ 2'.encode('utf-8'): u'значение 2'.encode('utf-8'),
- 'list' if six.PY2 else b'list': [u'ключ 2'.encode('utf-8'), 'key 1' if six.PY2 else b'key 1', (u'к'.encode('utf-8'), 2)]
+ 'list' if six.PY2 else b'list': [u'ключ 2'.encode('utf-8'), 'key 1' if six.PY2 else b'key 1', (u'к'.encode('utf-8'), 2)]
}
@@ -141,16 +141,16 @@ def test_stringize_deep_nested():
u'подключ 2': u'value 2',
},
}) == {
- 'key 1' if six.PY2 else b'key 1': 'value 1' if six.PY2 else b'value 1',
+ 'key 1' if six.PY2 else b'key 1': 'value 1' if six.PY2 else b'value 1',
u'ключ 2'.encode('utf-8'): {
- 'subkey 1' if six.PY2 else b'subkey 1': 'value 1' if six.PY2 else b'value 1',
+ 'subkey 1' if six.PY2 else b'subkey 1': 'value 1' if six.PY2 else b'value 1',
u'подключ 2'.encode('utf-8'): u'value 2'.encode('utf-8'),
},
}
def test_stringize_deep_plain():
- assert library.python.strings.stringize_deep('str') == 'str' if six.PY2 else b'str'
+ assert library.python.strings.stringize_deep('str') == 'str' if six.PY2 else b'str'
assert library.python.strings.stringize_deep(u'юникод') == u'юникод'.encode('utf-8')
assert library.python.strings.stringize_deep(u'юникод'.encode('utf-8')) == u'юникод'.encode('utf-8')
diff --git a/library/python/strings/ut/ya.make b/library/python/strings/ut/ya.make
index dfacb226c7..30800efd37 100644
--- a/library/python/strings/ut/ya.make
+++ b/library/python/strings/ut/ya.make
@@ -1,6 +1,6 @@
OWNER(g:yatool)
-PY23_TEST()
+PY23_TEST()
TEST_SRCS(test_strings.py)
diff --git a/library/python/strings/ya.make b/library/python/strings/ya.make
index 7e0b033717..49d4fa9b04 100644
--- a/library/python/strings/ya.make
+++ b/library/python/strings/ya.make
@@ -10,7 +10,7 @@ PY_SRCS(
PEERDIR(
library/python/func
- contrib/python/six
+ contrib/python/six
)
END()