This patch allows classes to define __bytes__ to customize what bytes(obj) returns. I added two new C functions PyObject_Bytes, and PyBytes_FromObject.
http://codereview.appspot.com/3245/diff/1/4 File Objects/object.c (right): http://codereview.appspot.com/3245/diff/1/4#newcode469 Line 469: return PyBytes_FromString("<NULL>"); Should this raise an exception instead? ...
16 years, 8 months ago
(2008-08-26 09:05:15 UTC)
#2
16 years, 8 months ago
(2008-08-26 13:30:04 UTC)
#3
On 2008/08/26 09:05:15, warsaw wrote:
> http://codereview.appspot.com/3245/diff/1/4
> File Objects/object.c (right):
>
> http://codereview.appspot.com/3245/diff/1/4#newcode469
> Line 469: return PyBytes_FromString("<NULL>");
> Should this raise an exception instead? What's the use case for allowing v ==
> NULL?
I did this because this is what PyObject_Str does if the value is NULL.
>
> http://codereview.appspot.com/3245/diff/1/4#newcode491
> Line 491: PyErr_Clear();
> Should this swallow all lookup errors or just more specific ones indicating
> __bytes__ doesn't exist?
This is also the behavior of 2.6's PyObject_Unicode if the lookup returns NULL.
I personally think that it could check for an attribute error, but I guess that
might be expensive.
Issue 3245: #2415 bytes() should respect __bytes__
(Closed)
Created 16 years, 8 months ago by Benjamin
Modified 15 years, 9 months ago
Reviewers: barry_python.org
Base URL: http://svn.python.org/view/*checkout*/python/branches/py3k/
Comments: 2