[Python-3000-checkins] r53892 - in python/branches/p3yk: Include/Python-ast.h Lib/test/test_bytes.py Objects/bytesobject.c Python/symtable.c

Brett Cannon brett at python.org
Sun Feb 25 16:07:19 CET 2007


My misunderstanding.  I thought I had heard you say that you really
like repr of an object be able to disambiguate between instances.
Sorry about the mix-up.

-Brett

On 2/24/07, Guido van Rossum <guido at python.org> wrote:
> I don't understand this. repr() isn't meant to let you tell individual
> objects apart, just disambiguate the type. repr(1) should !=
> repr("1"). But repr([]) == repr([]) even if the two lists are not the
> same object.
>
> --Guido
>
> On 2/24/07, Brett Cannon <brett at python.org> wrote:
> > Is this the type of repr we want for a mutable object?  I know Guido
> > has said he like repr representations to make it possible to tell
> > inidividual objects apart and that the __str__ representation can mask
> > that to make it easier to read.
> >
> > With the object being mutable it would be very easy to have two
> > objects that look the same under repr.
> >
> > -Brett
> >
> > On 2/24/07, georg.brandl <python-3000-checkins at python.org> wrote:
> > > Author: georg.brandl
> > > Date: Sat Feb 24 20:41:35 2007
> > > New Revision: 53892
> > >
> > > Modified:
> > >    python/branches/p3yk/Include/Python-ast.h
> > >    python/branches/p3yk/Lib/test/test_bytes.py
> > >    python/branches/p3yk/Objects/bytesobject.c
> > >    python/branches/p3yk/Python/symtable.c
> > > Log:
> > > Make bytes_repr return a string containing a b"" literal.
> > >
> > >
> > >
> > > Modified: python/branches/p3yk/Include/Python-ast.h
> > > ==============================================================================
> > > --- python/branches/p3yk/Include/Python-ast.h   (original)
> > > +++ python/branches/p3yk/Include/Python-ast.h   Sat Feb 24 20:41:35 2007
> > > @@ -516,4 +516,3 @@
> > >  alias_ty _Py_alias(identifier name, identifier asname, PyArena *arena);
> > >
> > >  PyObject* PyAST_mod2obj(mod_ty t);
> > > -
> > >
> > > Modified: python/branches/p3yk/Lib/test/test_bytes.py
> > > ==============================================================================
> > > --- python/branches/p3yk/Lib/test/test_bytes.py (original)
> > > +++ python/branches/p3yk/Lib/test/test_bytes.py Sat Feb 24 20:41:35 2007
> > > @@ -69,9 +69,11 @@
> > >          self.assertRaises(ValueError, bytes, [10**100])
> > >
> > >      def test_repr(self):
> > > -        self.assertEqual(repr(bytes()), "bytes()")
> > > -        self.assertEqual(repr(bytes([0])), "bytes([0x00])")
> > > -        self.assertEqual(repr(bytes([0, 1, 254, 255])), "bytes([0x00, 0x01, 0xfe, 0xff])")
> > > +        self.assertEqual(repr(bytes()), "b''")
> > > +        self.assertEqual(repr(bytes([0])), "b'\\0'")
> > > +        self.assertEqual(repr(bytes([0, 1, 254, 255])), "b'\\0\\x01\\xfe\\xff'")
> > > +        self.assertEqual(repr(bytes('abc')), "b'abc'")
> > > +        self.assertEqual(repr(bytes("'")), "b'\\''")
> > >
> > >      def test_compare(self):
> > >          b1 = bytes([1, 2, 3])
> > >
> > > Modified: python/branches/p3yk/Objects/bytesobject.c
> > > ==============================================================================
> > > --- python/branches/p3yk/Objects/bytesobject.c  (original)
> > > +++ python/branches/p3yk/Objects/bytesobject.c  Sat Feb 24 20:41:35 2007
> > > @@ -733,65 +733,63 @@
> > >      return -1;
> > >  }
> > >
> > > +/* Mostly copied from string_repr, but without the
> > > +   "smart quote" functionality. */
> > >  static PyObject *
> > >  bytes_repr(PyBytesObject *self)
> > >  {
> > > -    PyObject *list;
> > > -    PyObject *str;
> > > -    PyObject *result;
> > > -    int err;
> > > -    int i;
> > > -
> > > -    if (self->ob_size == 0)
> > > -        return PyString_FromString("bytes()");
> > > -
> > > -    list = PyList_New(0);
> > > -    if (list == NULL)
> > > +    size_t newsize = 3 + 4 * self->ob_size;
> > > +    PyObject *v;
> > > +    if (newsize > PY_SSIZE_T_MAX || newsize / 4 != self->ob_size) {
> > > +        PyErr_SetString(PyExc_OverflowError,
> > > +            "bytes object is too large to make repr");
> > >          return NULL;
> > > -
> > > -    str = PyString_FromString("bytes([");
> > > -    if (str == NULL)
> > > -        goto error;
> > > -
> > > -    err = PyList_Append(list, str);
> > > -    Py_DECREF(str);
> > > -    if (err < 0)
> > > -        goto error;
> > > -
> > > -    for (i = 0; i < self->ob_size; i++) {
> > > -        char buffer[20];
> > > -        sprintf(buffer, ", 0x%02x", (unsigned char) (self->ob_bytes[i]));
> > > -        str = PyString_FromString((i == 0) ? buffer+2 : buffer);
> > > -        if (str == NULL)
> > > -            goto error;
> > > -        err = PyList_Append(list, str);
> > > -        Py_DECREF(str);
> > > -        if (err < 0)
> > > -            goto error;
> > >      }
> > > -
> > > -    str = PyString_FromString("])");
> > > -    if (str == NULL)
> > > -        goto error;
> > > -
> > > -    err = PyList_Append(list, str);
> > > -    Py_DECREF(str);
> > > -    if (err < 0)
> > > -        goto error;
> > > -
> > > -    str = PyString_FromString("");
> > > -    if (str == NULL)
> > > -        goto error;
> > > -
> > > -    result = _PyString_Join(str, list);
> > > -    Py_DECREF(str);
> > > -    Py_DECREF(list);
> > > -    return result;
> > > -
> > > - error:
> > > -    /* Error handling when list != NULL  */
> > > -    Py_DECREF(list);
> > > -    return NULL;
> > > +    v = PyString_FromStringAndSize((char *)NULL, newsize);
> > > +    if (v == NULL) {
> > > +        return NULL;
> > > +    }
> > > +    else {
> > > +        register Py_ssize_t i;
> > > +        register char c;
> > > +        register char *p;
> > > +        int quote = '\'';
> > > +
> > > +        p = PyString_AS_STRING(v);
> > > +        *p++ = 'b';
> > > +        *p++ = quote;
> > > +        for (i = 0; i < self->ob_size; i++) {
> > > +            /* There's at least enough room for a hex escape
> > > +               and a closing quote. */
> > > +            assert(newsize - (p - PyString_AS_STRING(v)) >= 5);
> > > +            c = self->ob_bytes[i];
> > > +            if (c == quote || c == '\\')
> > > +                *p++ = '\\', *p++ = c;
> > > +            else if (c == '\t')
> > > +                *p++ = '\\', *p++ = 't';
> > > +            else if (c == '\n')
> > > +                *p++ = '\\', *p++ = 'n';
> > > +            else if (c == '\r')
> > > +                *p++ = '\\', *p++ = 'r';
> > > +            else if (c == 0)
> > > +                *p++ = '\\', *p++ = '0';
> > > +            else if (c < ' ' || c >= 0x7f) {
> > > +                /* For performance, we don't want to call
> > > +                   PyOS_snprintf here (extra layers of
> > > +                   function call). */
> > > +                sprintf(p, "\\x%02x", c & 0xff);
> > > +                                p += 4;
> > > +            }
> > > +            else
> > > +                *p++ = c;
> > > +        }
> > > +        assert(newsize - (p - PyString_AS_STRING(v)) >= 1);
> > > +        *p++ = quote;
> > > +        *p = '\0';
> > > +        _PyString_Resize(
> > > +            &v, (p - PyString_AS_STRING(v)));
> > > +        return v;
> > > +    }
> > >  }
> > >
> > >  static PyObject *
> > >
> > > Modified: python/branches/p3yk/Python/symtable.c
> > > ==============================================================================
> > > --- python/branches/p3yk/Python/symtable.c      (original)
> > > +++ python/branches/p3yk/Python/symtable.c      Sat Feb 24 20:41:35 2007
> > > @@ -1177,7 +1177,8 @@
> > >                 break;
> > >          case Num_kind:
> > >          case Str_kind:
> > > -       case Ellipsis_kind:
> > > +        case Bytes_kind:
> > > +        case Ellipsis_kind:
> > >                 /* Nothing to do here. */
> > >                 break;
> > >         /* The following exprs can be assignment targets. */
> > > _______________________________________________
> > > Python-3000-checkins mailing list
> > > Python-3000-checkins at python.org
> > > http://mail.python.org/mailman/listinfo/python-3000-checkins
> > >
> > _______________________________________________
> > Python-3000-checkins mailing list
> > Python-3000-checkins at python.org
> > http://mail.python.org/mailman/listinfo/python-3000-checkins
> >
>
>
> --
> --Guido van Rossum (home page: http://www.python.org/~guido/)
>


More information about the Python-3000-checkins mailing list