summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_unicode.py
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2005-04-26 03:45:26 (GMT)
committerBrett Cannon <bcannon@gmail.com>2005-04-26 03:45:26 (GMT)
commitc3647ac93e2a38762de8a23b1d94a6380e9ad468 (patch)
treea7e00a7e8f70ee226fdeb3d229b9734d5b17a344 /Lib/test/test_unicode.py
parentd7c795e72966f7c72b94b919f3539be66495e6c3 (diff)
downloadcpython-c3647ac93e2a38762de8a23b1d94a6380e9ad468.zip
cpython-c3647ac93e2a38762de8a23b1d94a6380e9ad468.tar.gz
cpython-c3647ac93e2a38762de8a23b1d94a6380e9ad468.tar.bz2
Make subclasses of int, long, complex, float, and unicode perform type
conversion using the proper magic slot (e.g., __int__()). Also move conversion code out of PyNumber_*() functions in the C API into the nb_* function. Applied patch #1109424. Thanks Walter Doewald.
Diffstat (limited to 'Lib/test/test_unicode.py')
-rw-r--r--Lib/test/test_unicode.py64
1 files changed, 63 insertions, 1 deletions
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 69244f0..80242d5 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -389,7 +389,6 @@ class UnicodeTest(
self.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, u'abc',), u'103 abc')
self.assertEqual('%c' % u'a', u'a')
-
def test_constructor(self):
# unicode(obj) tests (this maps to PyObject_Unicode() at C level)
@@ -725,6 +724,69 @@ class UnicodeTest(
y = x.encode("raw-unicode-escape").decode("raw-unicode-escape")
self.assertEqual(x, y)
+ def test_conversion(self):
+ # Make sure __unicode__() works properly
+ class Foo0:
+ def __str__(self):
+ return "foo"
+
+ class Foo1:
+ def __unicode__(self):
+ return u"foo"
+
+ class Foo2(object):
+ def __unicode__(self):
+ return u"foo"
+
+ class Foo3(object):
+ def __unicode__(self):
+ return "foo"
+
+ class Foo4(str):
+ def __unicode__(self):
+ return "foo"
+
+ class Foo5(unicode):
+ def __unicode__(self):
+ return "foo"
+
+ class Foo6(str):
+ def __str__(self):
+ return "foos"
+
+ def __unicode__(self):
+ return u"foou"
+
+ class Foo7(unicode):
+ def __str__(self):
+ return "foos"
+ def __unicode__(self):
+ return u"foou"
+
+ class Foo8(unicode):
+ def __new__(cls, content=""):
+ return unicode.__new__(cls, 2*content)
+ def __unicode__(self):
+ return self
+
+ class Foo9(unicode):
+ def __str__(self):
+ return "string"
+ def __unicode__(self):
+ return "not unicode"
+
+ self.assertEqual(unicode(Foo0()), u"foo")
+ self.assertEqual(unicode(Foo1()), u"foo")
+ self.assertEqual(unicode(Foo2()), u"foo")
+ self.assertEqual(unicode(Foo3()), u"foo")
+ self.assertEqual(unicode(Foo4("bar")), u"foo")
+ self.assertEqual(unicode(Foo5("bar")), u"foo")
+ self.assertEqual(unicode(Foo6("bar")), u"foou")
+ self.assertEqual(unicode(Foo7("bar")), u"foou")
+ self.assertEqual(unicode(Foo8("foo")), u"foofoo")
+ self.assertEqual(str(Foo9("foo")), "string")
+ self.assertEqual(unicode(Foo9("foo")), u"not unicode")
+
def test_main():
test_support.run_unittest(UnicodeTest)