1.1 --- a/lib/__builtins__/unicode.py Tue Dec 13 22:46:52 2016 +0100
1.2 +++ b/lib/__builtins__/unicode.py Tue Dec 13 22:48:28 2016 +0100
1.3 @@ -77,6 +77,29 @@
1.4 else:
1.5 return op(other.__data__, self.__data__)
1.6
1.7 + def _convert(self, result, other):
1.8 +
1.9 + "Convert 'result' to a Unicode object if 'other' already is."
1.10 +
1.11 + if _isinstance(other, utf8string):
1.12 + return utf8string(result, self.encoding)
1.13 + else:
1.14 + return result
1.15 +
1.16 + def __iadd__(self, other):
1.17 +
1.18 + "Return a string combining this string with 'other'."
1.19 +
1.20 + return self._convert(self._binary_op(str_add, other), other)
1.21 +
1.22 + __add__ = __iadd__
1.23 +
1.24 + def __radd__(self, other):
1.25 +
1.26 + "Return a string combining this string with 'other'."
1.27 +
1.28 + return self._convert(self._binary_op_rev(str_add, other), other)
1.29 +
1.30 def encode(self, encoding=None):
1.31
1.32 """
2.1 --- a/tests/unicode.py Tue Dec 13 22:46:52 2016 +0100
2.2 +++ b/tests/unicode.py Tue Dec 13 22:48:28 2016 +0100
2.3 @@ -14,35 +14,56 @@
2.4 u = unicode("æøå", "ISO-8859-1")
2.5 print u # æøå
2.6 print u.encode("ISO-8859-1") # æøå
2.7 +print u.encoding # ISO-8859-1
2.8
2.9 # Explicitly from Unicode literals.
2.10
2.11 u2 = u"æøå"
2.12 print u2 # æøå
2.13 print u2.encode("ISO-8859-1") # æøå
2.14 +print u2.encoding # ISO-8859-1
2.15
2.16 # Implicitly from string literals.
2.17
2.18 #u3 = "æøå"
2.19 #print u3 # æøå
2.20 #print u3.encode("ISO-8859-1") # æøå
2.21 +#print u3.encoding # ISO-8859-1
2.22
2.23 # Combine bytes and text.
2.24 # The text should be decoded.
2.25
2.26 su = s + u
2.27 print su # ÆØÅæøå
2.28 +print su.__class__ # __builtins__.str.string
2.29
2.30 # Combine text and bytes.
2.31 # The text should be decoded.
2.32
2.33 us = u + s
2.34 print us # æøåÆØÅ
2.35 +print us.__class__ # __builtins__.str.string
2.36 +
2.37 +# Combine text and text.
2.38 +
2.39 +uu2 = u + u2
2.40 +print uu2 # æøå
2.41 +print uu2.__class__ # __builtins__.unicode.utf8string
2.42 +print uu2.encoding # ISO-8859-1
2.43
2.44 # Inspect and update the encoding of stdout.
2.45 +# Note that su and us are byte strings and are not recoded.
2.46
2.47 print sys.stdout.encoding # None
2.48 +
2.49 sys.stdout.encoding = "ISO-8859-1"
2.50 +print sys.stdout.encoding # ISO-8859-1
2.51 print u # æøå
2.52 print su # ÆØÅæøå
2.53 print us # æøåÆØÅ
2.54 +
2.55 +sys.stdout.encoding = "UTF-8"
2.56 +print sys.stdout.encoding # UTF-8
2.57 +print u # æøå
2.58 +print su # ÆØÅæøå
2.59 +print us # æøåÆØÅ