[Python-checkins] cpython: Issue #17339: Improved TypeError message in bytes constructor.

serhiy.storchaka python-checkins at python.org
Sun Apr 10 07:45:43 EDT 2016


https://hg.python.org/cpython/rev/6b16eec56854
changeset:   100905:6b16eec56854
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Apr 10 14:44:59 2016 +0300
summary:
  Issue #17339: Improved TypeError message in bytes constructor.

files:
  Objects/bytesobject.c |  31 +++++++++++++++----------------
  1 files changed, 15 insertions(+), 16 deletions(-)


diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -3469,31 +3469,24 @@
 }
 
 static PyObject *
-_PyBytes_FromIterator(PyObject *x)
+_PyBytes_FromIterator(PyObject *it, PyObject *x)
 {
     char *str;
-    PyObject *it;
     Py_ssize_t i, size;
     _PyBytesWriter writer;
 
-    _PyBytesWriter_Init(&writer);
-
     /* For iterator version, create a string object and resize as needed */
     size = PyObject_LengthHint(x, 64);
     if (size == -1 && PyErr_Occurred())
         return NULL;
 
+    _PyBytesWriter_Init(&writer);
     str = _PyBytesWriter_Alloc(&writer, size);
     if (str == NULL)
         return NULL;
     writer.overallocate = 1;
     size = writer.allocated;
 
-    /* Get the iterator */
-    it = PyObject_GetIter(x);
-    if (it == NULL)
-        goto error;
-
     /* Run the iterator to exhaustion */
     for (i = 0; ; i++) {
         PyObject *item;
@@ -3529,19 +3522,19 @@
         }
         *str++ = (char) value;
     }
-    Py_DECREF(it);
 
     return _PyBytesWriter_Finish(&writer, str);
 
   error:
     _PyBytesWriter_Dealloc(&writer);
-    Py_XDECREF(it);
     return NULL;
 }
 
 PyObject *
 PyBytes_FromObject(PyObject *x)
 {
+    PyObject *it, *result;
+
     if (x == NULL) {
         PyErr_BadInternalCall();
         return NULL;
@@ -3562,13 +3555,19 @@
     if (PyTuple_CheckExact(x))
         return _PyBytes_FromTuple(x);
 
-    if (PyUnicode_Check(x)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "cannot convert unicode object to bytes");
-        return NULL;
+    if (!PyUnicode_Check(x)) {
+        it = PyObject_GetIter(x);
+        if (it != NULL) {
+            result = _PyBytes_FromIterator(it, x);
+            Py_DECREF(it);
+            return result;
+        }
     }
 
-    return _PyBytes_FromIterator(x);
+    PyErr_Format(PyExc_TypeError,
+                 "cannot convert '%.200s' object to bytes",
+                 x->ob_type->tp_name);
+    return NULL;
 }
 
 static PyObject *

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list