summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2012-12-19 21:28:46 (GMT)
committerBenjamin Peterson <benjamin@python.org>2012-12-19 21:28:46 (GMT)
commit7643c92cdd0e8a3cfd83be9edae3964097b63f0e (patch)
tree2eccc052cc4b4d7e8f51f152c601a5dc48bb53b7
parentaf5e29a769c1db9c1dd24b6d7344b9c6e0f2713f (diff)
parent5ff3f73d94305900c0773e67ebdd9701d856a0fe (diff)
downloadcpython-7643c92cdd0e8a3cfd83be9edae3964097b63f0e.zip
cpython-7643c92cdd0e8a3cfd83be9edae3964097b63f0e.tar.gz
cpython-7643c92cdd0e8a3cfd83be9edae3964097b63f0e.tar.bz2
merge 3.3 (#16722)
-rw-r--r--Lib/test/test_bytes.py6
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/bytesobject.c33
3 files changed, 37 insertions, 5 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 8ae90e4..2b32767 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -715,6 +715,12 @@ class BytesTest(BaseBytesTest):
def __bytes__(self):
return None
self.assertRaises(TypeError, bytes, A())
+ class A:
+ def __bytes__(self):
+ return b'a'
+ def __index__(self):
+ return 42
+ self.assertEqual(bytes(A()), b'a')
# Test PyBytes_FromFormat()
def test_from_format(self):
diff --git a/Misc/NEWS b/Misc/NEWS
index 7890f64..77db4e1 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1?
Core and Builtins
-----------------
+- Issue #16722: In the bytes() constructor, try to call __bytes__ on the
+ argument before __index__.
+
- Issue #16421: loading multiple modules from one shared object is now
handled correctly (previously, the first module loaded from that file
was silently returned). Patch by Václav Šmilauer.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 8d8cb05..91f65f9 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2428,8 +2428,10 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
const char *encoding = NULL;
const char *errors = NULL;
PyObject *new = NULL;
+ PyObject *func;
Py_ssize_t size;
static char *kwlist[] = {"source", "encoding", "errors", 0};
+ _Py_IDENTIFIER(__bytes__);
if (type != &PyBytes_Type)
return str_subtype_new(type, args, kwds);
@@ -2459,6 +2461,28 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
assert(PyBytes_Check(new));
return new;
}
+
+ /* We'd like to call PyObject_Bytes here, but we need to check for an
+ integer argument before deferring to PyBytes_FromObject, something
+ PyObject_Bytes doesn't do. */
+ func = _PyObject_LookupSpecial(x, &PyId___bytes__);
+ if (func != NULL) {
+ new = PyObject_CallFunctionObjArgs(func, NULL);
+ Py_DECREF(func);
+ if (new == NULL)
+ return NULL;
+ if (!PyBytes_Check(new)) {
+ PyErr_Format(PyExc_TypeError,
+ "__bytes__ returned non-bytes (type %.200s)",
+ Py_TYPE(new)->tp_name);
+ Py_DECREF(new);
+ return NULL;
+ }
+ return new;
+ }
+ else if (PyErr_Occurred())
+ return NULL;
+
/* Is it an integer? */
size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
if (size == -1 && PyErr_Occurred()) {
@@ -2472,12 +2496,10 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
else {
new = PyBytes_FromStringAndSize(NULL, size);
- if (new == NULL) {
+ if (new == NULL)
return NULL;
- }
- if (size > 0) {
+ if (size > 0)
memset(((PyBytesObject*)new)->ob_sval, 0, size);
- }
return new;
}
@@ -2487,7 +2509,8 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
"encoding or errors without a string argument");
return NULL;
}
- return PyObject_Bytes(x);
+
+ return PyBytes_FromObject(x);
}
PyObject *