[Checkins] SVN: Acquisition/trunk/ Merged zagy-unicode-should-be-called branch incl. some more tests
Hanno Schlichting
hannosch at hannosch.eu
Sat Feb 19 10:53:45 EST 2011
Log message for revision 120446:
Merged zagy-unicode-should-be-called branch incl. some more tests
Changed:
U Acquisition/trunk/CHANGES.txt
U Acquisition/trunk/src/Acquisition/_Acquisition.c
U Acquisition/trunk/src/Acquisition/tests.py
-=-
Modified: Acquisition/trunk/CHANGES.txt
===================================================================
--- Acquisition/trunk/CHANGES.txt 2011-02-19 15:53:21 UTC (rev 120445)
+++ Acquisition/trunk/CHANGES.txt 2011-02-19 15:53:44 UTC (rev 120446)
@@ -6,6 +6,9 @@
- Add ``aq_explicit`` to ``IAcquisitionWrapper``.
+- Fixed bug: ``unicode(wrapped)`` was not calling a ``__unicode__``
+ method on wrapped objects.
+
2.13.5 (2010-09-29)
-------------------
Modified: Acquisition/trunk/src/Acquisition/_Acquisition.c
===================================================================
--- Acquisition/trunk/src/Acquisition/_Acquisition.c 2011-02-19 15:53:21 UTC (rev 120445)
+++ Acquisition/trunk/src/Acquisition/_Acquisition.c 2011-02-19 15:53:44 UTC (rev 120446)
@@ -51,8 +51,8 @@
*py__long__, *py__float__, *py__oct__, *py__hex__,
*py__getitem__, *py__setitem__, *py__delitem__,
*py__getslice__, *py__setslice__, *py__delslice__, *py__contains__,
- *py__len__, *py__of__, *py__call__, *py__repr__, *py__str__, *py__cmp__,
- *py__parent__, *py__iter__;
+ *py__len__, *py__of__, *py__call__, *py__repr__, *py__str__, *py__unicode__,
+ *py__cmp__, *py__parent__, *py__iter__;
static PyObject *Acquired=0;
@@ -95,6 +95,7 @@
INIT_PY_NAME(__call__);
INIT_PY_NAME(__repr__);
INIT_PY_NAME(__str__);
+ INIT_PY_NAME(__unicode__);
INIT_PY_NAME(__cmp__);
INIT_PY_NAME(__parent__);
INIT_PY_NAME(__iter__);
@@ -839,6 +840,23 @@
}
}
+static PyObject *
+Wrapper_unicode(Wrapper *self)
+{
+ PyObject *r;
+
+ if ((r=PyObject_GetAttr(OBJECT(self),py__unicode__)))
+ {
+ ASSIGN(r,PyObject_CallFunction(r,NULL,NULL));
+ return r;
+ }
+ else
+ {
+ PyErr_Clear();
+ return PyObject_Unicode(self->obj);
+ }
+}
+
static long
Wrapper_hash(Wrapper *self)
{
@@ -1313,6 +1331,8 @@
"Wrappers are not picklable"},
{"__reduce_ex__", (PyCFunction)Wrappers_are_not_picklable, METH_VARARGS,
"Wrappers are not picklable"},
+ {"__unicode__", (PyCFunction)Wrapper_unicode, METH_NOARGS,
+ "Unicode"},
{NULL, NULL} /* sentinel */
};
Modified: Acquisition/trunk/src/Acquisition/tests.py
===================================================================
--- Acquisition/trunk/src/Acquisition/tests.py 2011-02-19 15:53:21 UTC (rev 120445)
+++ Acquisition/trunk/src/Acquisition/tests.py 2011-02-19 15:53:44 UTC (rev 120446)
@@ -2435,13 +2435,51 @@
AttributeError: non_existant_attr
"""
+
import unittest
from doctest import DocTestSuite, DocFileSuite
+
+class TestUnicode(unittest.TestCase):
+
+ def test_implicit_aq_unicode_should_be_called(self):
+ class A(Acquisition.Implicit):
+ def __unicode__(self):
+ return u'unicode was called'
+ wrapped = A().__of__(A())
+ self.assertEqual(u'unicode was called', unicode(wrapped))
+ self.assertEqual(str(wrapped), repr(wrapped))
+
+ def test_explicit_aq_unicode_should_be_called(self):
+ class A(Acquisition.Explicit):
+ def __unicode__(self):
+ return u'unicode was called'
+ wrapped = A().__of__(A())
+ self.assertEqual(u'unicode was called', unicode(wrapped))
+ self.assertEqual(str(wrapped), repr(wrapped))
+
+ def test_implicit_should_fall_back_to_str(self):
+ class A(Acquisition.Implicit):
+ def __str__(self):
+ return 'str was called'
+ wrapped = A().__of__(A())
+ self.assertEqual(u'str was called', unicode(wrapped))
+ self.assertEqual('str was called', str(wrapped))
+
+ def test_explicit_should_fall_back_to_str(self):
+ class A(Acquisition.Explicit):
+ def __str__(self):
+ return 'str was called'
+ wrapped = A().__of__(A())
+ self.assertEqual(u'str was called', unicode(wrapped))
+ self.assertEqual('str was called', str(wrapped))
+
+
def test_suite():
return unittest.TestSuite((
DocTestSuite(),
DocFileSuite('README.txt', package='Acquisition'),
+ unittest.makeSuite(TestUnicode),
))
if __name__ == '__main__':
More information about the checkins
mailing list