Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3201977

Browse files
committed
fix __bytes__ handling here in py3x
Merged revisions 76395 via svnmerge from svn+ssh://[email protected]/python/trunk ........ r76395 | benjamin.peterson | 2009-11-18 21:00:02 -0600 (Wed, 18 Nov 2009) | 1 line #5037 proxy __unicode__ correctly ........
1 parent 36c3c02 commit 3201977

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lib/test/test_weakref.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,17 @@ def test_basic_proxy(self):
183183
self.assertEqual(L3[:5], p3[:5])
184184
self.assertEqual(L3[2:5], p3[2:5])
185185

186+
def test_proxy_unicode(self):
187+
# See bug 5037
188+
class C(object):
189+
def __str__(self):
190+
return "string"
191+
def __bytes__(self):
192+
return b"bytes"
193+
instance = C()
194+
self.assertTrue("__bytes__" in dir(weakref.proxy(instance)))
195+
self.assertEqual(bytes(weakref.proxy(instance)), b"bytes")
196+
186197
def test_proxy_index(self):
187198
class C:
188199
def __index__(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ C-API
137137
Library
138138
-------
139139

140+
- Issue #5037: Proxy the __unicode__ special method instead to __unicode__
141+
instead of __str__.
142+
140143
- Issue #7341: Close the internal file object in the TarFile constructor in
141144
case of an error.
142145

Objects/weakrefobject.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ proxy_checkref(PyWeakReference *proxy)
435435
return generic(proxy, v, w); \
436436
}
437437

438+
#define WRAP_METHOD(method, special) \
439+
static PyObject * \
440+
method(PyObject *proxy) { \
441+
UNWRAP(proxy); \
442+
return PyObject_CallMethod(proxy, special, ""); \
443+
}
444+
438445

439446
/* direct slots */
440447

@@ -576,6 +583,15 @@ proxy_iternext(PyWeakReference *proxy)
576583
}
577584

578585

586+
WRAP_METHOD(proxy_bytes, "__bytes__");
587+
588+
589+
static PyMethodDef proxy_methods[] = {
590+
{"__bytes__", (PyCFunction)proxy_bytes, METH_NOARGS},
591+
{NULL, NULL}
592+
};
593+
594+
579595
static PyNumberMethods proxy_as_number = {
580596
proxy_add, /*nb_add*/
581597
proxy_sub, /*nb_subtract*/
@@ -661,6 +677,7 @@ _PyWeakref_ProxyType = {
661677
0, /* tp_weaklistoffset */
662678
(getiterfunc)proxy_iter, /* tp_iter */
663679
(iternextfunc)proxy_iternext, /* tp_iternext */
680+
proxy_methods, /* tp_methods */
664681
};
665682

666683

0 commit comments

Comments
 (0)