summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMarc-André Lemburg <mal@egenix.com>2001-10-19 12:02:29 (GMT)
committerMarc-André Lemburg <mal@egenix.com>2001-10-19 12:02:29 (GMT)
commitb5507ecd3cfce17bab26311298f527572611af0b (patch)
tree81382c5f5faa45adb2c93ad5b6b7f1b040cb954a /Lib
parentf6fb171c9d6c0232937518dc00d3d31baeaf84c8 (diff)
downloadcpython-b5507ecd3cfce17bab26311298f527572611af0b.zip
cpython-b5507ecd3cfce17bab26311298f527572611af0b.tar.gz
cpython-b5507ecd3cfce17bab26311298f527572611af0b.tar.bz2
Additional test and documentation for the unicode() changes.
This patch should also be applied to the 2.2b1 trunk.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/output/test_unicode1
-rw-r--r--Lib/test/test_unicode.py82
2 files changed, 62 insertions, 21 deletions
diff --git a/Lib/test/output/test_unicode b/Lib/test/output/test_unicode
index 783a486..82ed240 100644
--- a/Lib/test/output/test_unicode
+++ b/Lib/test/output/test_unicode
@@ -2,6 +2,7 @@ test_unicode
Testing Unicode comparisons... done.
Testing Unicode contains method... done.
Testing Unicode formatting strings... done.
+Testing builtin unicode()... done.
Testing builtin codecs... done.
Testing standard mapping codecs... 0-127... 128-255... done.
Testing Unicode string concatenation... done.
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 68eae13..eff11cf 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -389,6 +389,67 @@ verify('%i %*.*s' % (10, 5,3,u'abc',) == u'10 abc')
verify('%i%s %*.*s' % (10, 3, 5,3,u'abc',) == u'103 abc')
print 'done.'
+print 'Testing builtin unicode()...',
+
+# unicode(obj) tests (this maps to PyObject_Unicode() at C level)
+
+verify(unicode(u'unicode remains unicode') == u'unicode remains unicode')
+
+class UnicodeSubclass(unicode):
+ pass
+
+verify(unicode(UnicodeSubclass('unicode subclass becomes unicode'))
+ == u'unicode subclass becomes unicode')
+
+verify(unicode('strings are converted to unicode')
+ == u'strings are converted to unicode')
+
+class UnicodeCompat:
+ def __init__(self, x):
+ self.x = x
+ def __unicode__(self):
+ return self.x
+
+verify(unicode(UnicodeCompat('__unicode__ compatible objects are recognized'))
+ == u'__unicode__ compatible objects are recognized')
+
+class StringCompat:
+ def __init__(self, x):
+ self.x = x
+ def __str__(self):
+ return self.x
+
+verify(unicode(StringCompat('__str__ compatible objects are recognized'))
+ == u'__str__ compatible objects are recognized')
+
+# unicode(obj) is compatible to str():
+
+o = StringCompat('unicode(obj) is compatible to str()')
+verify(unicode(o) == u'unicode(obj) is compatible to str()')
+verify(str(o) == 'unicode(obj) is compatible to str()')
+
+for obj in (123, 123.45, 123L):
+ verify(unicode(obj) == unicode(str(obj)))
+
+# unicode(obj, encoding, error) tests (this maps to
+# PyUnicode_FromEncodedObject() at C level)
+
+try:
+ unicode(u'decoding unicode is not supported', 'utf-8', 'strict')
+except TypeError:
+ pass
+else:
+ raise TestFailed, "decoding unicode should NOT be supported"
+
+verify(unicode('strings are decoded to unicode', 'utf-8', 'strict')
+ == u'strings are decoded to unicode')
+
+verify(unicode(buffer('character buffers are decoded to unicode'),
+ 'utf-8', 'strict')
+ == u'character buffers are decoded to unicode')
+
+print 'done.'
+
# Test builtin codecs
print 'Testing builtin codecs...',
@@ -437,32 +498,11 @@ verify(unicode(''.join((chr(0xe2), chr(0x82), chr(0xac))),
# * strict decoding testing for all of the
# UTF8_ERROR cases in PyUnicode_DecodeUTF8
-
-
verify(unicode('hello','ascii') == u'hello')
verify(unicode('hello','utf-8') == u'hello')
verify(unicode('hello','utf8') == u'hello')
verify(unicode('hello','latin-1') == u'hello')
-# Compatibility to str():
-class String:
- x = ''
- def __str__(self):
- return self.x
-
-o = String()
-
-o.x = 'abc'
-verify(unicode(o) == u'abc')
-verify(str(o) == 'abc')
-
-o.x = u'abc'
-verify(unicode(o) == u'abc')
-verify(str(o) == 'abc')
-
-for obj in (123, 123.45, 123L):
- verify(unicode(obj) == unicode(str(obj)))
-
# Error handling
try:
u'Andr\202 x'.encode('ascii')