[Python-checkins] r76396 - in python/branches/py3k: Lib/test/test_weakref.py Misc/NEWS Objects/weakrefobject.c

benjamin.peterson python-checkins at python.org
Thu Nov 19 04:08:32 CET 2009


Author: benjamin.peterson
Date: Thu Nov 19 04:08:32 2009
New Revision: 76396

Log:
fix __bytes__ handling here in py3x
Merged revisions 76395 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r76395 | benjamin.peterson | 2009-11-18 21:00:02 -0600 (Wed, 18 Nov 2009) | 1 line
  
  #5037 proxy __unicode__ correctly
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Lib/test/test_weakref.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Objects/weakrefobject.c

Modified: python/branches/py3k/Lib/test/test_weakref.py
==============================================================================
--- python/branches/py3k/Lib/test/test_weakref.py	(original)
+++ python/branches/py3k/Lib/test/test_weakref.py	Thu Nov 19 04:08:32 2009
@@ -183,6 +183,17 @@
         self.assertEqual(L3[:5], p3[:5])
         self.assertEqual(L3[2:5], p3[2:5])
 
+    def test_proxy_unicode(self):
+        # See bug 5037
+        class C(object):
+            def __str__(self):
+                return "string"
+            def __bytes__(self):
+                return b"bytes"
+        instance = C()
+        self.assertTrue("__bytes__" in dir(weakref.proxy(instance)))
+        self.assertEqual(bytes(weakref.proxy(instance)), b"bytes")
+
     def test_proxy_index(self):
         class C:
             def __index__(self):

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Thu Nov 19 04:08:32 2009
@@ -137,6 +137,9 @@
 Library
 -------
 
+- Issue #5037: Proxy the __unicode__ special method instead to __unicode__
+  instead of __str__.
+
 - Issue #7341: Close the internal file object in the TarFile constructor in
   case of an error.
 

Modified: python/branches/py3k/Objects/weakrefobject.c
==============================================================================
--- python/branches/py3k/Objects/weakrefobject.c	(original)
+++ python/branches/py3k/Objects/weakrefobject.c	Thu Nov 19 04:08:32 2009
@@ -435,6 +435,13 @@
         return generic(proxy, v, w); \
     }
 
+#define WRAP_METHOD(method, special) \
+    static PyObject * \
+    method(PyObject *proxy) { \
+	    UNWRAP(proxy); \
+		return PyObject_CallMethod(proxy, special, ""); \
+	}
+
 
 /* direct slots */
 
@@ -576,6 +583,15 @@
 }
 
 
+WRAP_METHOD(proxy_bytes, "__bytes__");
+
+
+static PyMethodDef proxy_methods[] = {
+	{"__bytes__", (PyCFunction)proxy_bytes, METH_NOARGS},
+	{NULL, NULL}
+};
+
+
 static PyNumberMethods proxy_as_number = {
     proxy_add,              /*nb_add*/
     proxy_sub,              /*nb_subtract*/
@@ -661,6 +677,7 @@
     0,                                  /* tp_weaklistoffset */
     (getiterfunc)proxy_iter,            /* tp_iter */
     (iternextfunc)proxy_iternext,       /* tp_iternext */
+	proxy_methods,                      /* tp_methods */
 };
 
 


More information about the Python-checkins mailing list