Skip to content

Commit 6d59753

Browse files
committed
Fix normalizing string subclasses with incompatible __new__.
Fixes robotframework#2739.
1 parent 67def36 commit 6d59753

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/robot/utils/normalizing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from collections import MutableMapping
1818

1919
from .platform import PY3, IRONPYTHON
20-
from .robottypes import is_dict_like
20+
from .robottypes import is_dict_like, is_unicode
2121

2222

2323
def normalize(string, ignore=(), caseless=True, spaceless=True):
@@ -26,7 +26,7 @@ def normalize(string, ignore=(), caseless=True, spaceless=True):
2626
By default string is turned to lower case and all whitespace is removed.
2727
Additional characters can be removed by giving them in ``ignore`` list.
2828
"""
29-
empty = type(string)()
29+
empty = u'' if is_unicode(string) else b''
3030
if PY3 and isinstance(ignore, bytes):
3131
# Iterating bytes in Python3 yields integers.
3232
ignore = [bytes([i]) for i in ignore]

utest/utils/test_normalizing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ def test_bytes(self):
5656
self._verify(b'FOO & Bar', b'bar', ignore=b'F&o')
5757
self._verify(b'FOO & Bar', b'OOBar', ignore=b'F&o', caseless=False)
5858

59+
def test_string_subclass_without_compatible_init(self):
60+
class BrokenLikeSudsText(str):
61+
def __new__(cls, value):
62+
return str.__new__(cls, value)
63+
self._verify(BrokenLikeSudsText('suds.sax.text.Text is BROKEN'),
64+
'suds.sax.text.textisbroken')
65+
self._verify(BrokenLikeSudsText(''), '')
66+
5967

6068
class TestNormalizedDict(unittest.TestCase):
6169

0 commit comments

Comments
 (0)