[Python-3000] PEP 3137 plan of attack

Adam Olsen rhamph at gmail.com
Mon Oct 15 20:40:29 CEST 2007


On 10/15/07, Guido van Rossum <guido at python.org> wrote:
> On 10/15/07, Jim Jewett <jimjjewett at gmail.com> wrote:
> > On 10/12/07, Guido van Rossum <guido at python.org> wrote:
> > > On 10/12/07, Gregory P. Smith <greg at krypto.org> wrote:
> > > > > > - add missing methods to PyBytes (for list, see the PEP and compare to
> > > > > > what's already there)
> >
> > > > As I work on these..  Should the mutable PyBytes_ (buffer) objects implement
> > > > the following methods inplace and return an additional reference to self?
> >
> > > > .capitalize(), .center(), .expandtabs(), .rjust(), .swapcase(), .title(),
> > > > .upper(), .zfill()
> >
> > > No... That would be a huge trap to fall in at all sorts of occasions.
> >
> > So would returning a different object.  I expect a mutation operation
> > on an explicitly mutable object to mutate the object, instead of
> > creating something new.
> >
> > If it returns a new one, I can imagine doing something like:
> >
> >     obj.inqueue=bytesbuffer(100)
> >     obj.inqueue.lower()   # oh, wait, that didn't really do anything
> > after all...
> >     if obj.inqueue[:4] == b"http":   # works on my *regular* input...
> >
> > Maybe the answer is "don't do that", and to only do this sort of
> > processing before it goes in the buffer or after it comes out, but ...
> > it still looks like a major gotcha.
>
> Since these methods with these very names already exist for strings
> and return new values there, I don't see the gotcha unless you never
> use strings.

Maybe .lower() should return immutable bytes, rather than mutable
buffer?  For the use cases I can imagine this'd still work correctly,
and fits better with why it makes a copy.  buffer is all about
operating in-place, so any copy immediately doesn't fit the buffer
concept.

    obj.inqueue = bytesbuffer(100)
    # replaces existing buffer contents.  Temp copy need not be mutable
    obj.inqueue[:] = obj.inqueue.lower()
    if obj.inqueue[:4] == b"http":


    obj.inqueue = bytesbuffer(100)
    if obj.inqueue[:4].lower() == b"http":  # compares bytes with bytes

-- 
Adam Olsen, aka Rhamphoryncus


More information about the Python-3000 mailing list