summaryrefslogtreecommitdiffstats
path: root/Objects/bytesobject.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-10-14 11:50:40 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-10-14 11:50:40 (GMT)
commit3c50ce39bfe0b5b905432b195c5bb1cf939f4273 (patch)
tree910857066590c920f5ef1009ec74f6d4ba632e17 /Objects/bytesobject.c
parentf2eafa323bc16e11dd4564ec68f27e8ea8b9c254 (diff)
downloadcpython-3c50ce39bfe0b5b905432b195c5bb1cf939f4273.zip
cpython-3c50ce39bfe0b5b905432b195c5bb1cf939f4273.tar.gz
cpython-3c50ce39bfe0b5b905432b195c5bb1cf939f4273.tar.bz2
Factorize _PyBytes_FromList() and _PyBytes_FromTuple() code using a C macro
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r--Objects/bytesobject.c89
1 files changed, 35 insertions, 54 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 1aae9e3..e54f299 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -3408,68 +3408,49 @@ fail:
return NULL;
}
+#define _PyBytes_FROM_LIST_BODY(x, GET_ITEM) \
+ do { \
+ PyObject *bytes; \
+ Py_ssize_t i; \
+ Py_ssize_t value; \
+ char *str; \
+ PyObject *item; \
+ \
+ bytes = PyBytes_FromStringAndSize(NULL, Py_SIZE(x)); \
+ if (bytes == NULL) \
+ return NULL; \
+ str = ((PyBytesObject *)bytes)->ob_sval; \
+ \
+ for (i = 0; i < Py_SIZE(x); i++) { \
+ item = GET_ITEM((x), i); \
+ value = PyNumber_AsSsize_t(item, PyExc_ValueError); \
+ if (value == -1 && PyErr_Occurred()) \
+ goto error; \
+ \
+ if (value < 0 || value >= 256) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "bytes must be in range(0, 256)"); \
+ goto error; \
+ } \
+ *str++ = (char) value; \
+ } \
+ return bytes; \
+ \
+ error: \
+ Py_DECREF(bytes); \
+ return NULL; \
+ } while (0)
+
static PyObject*
_PyBytes_FromList(PyObject *x)
{
- PyObject *new;
- Py_ssize_t i;
- Py_ssize_t value;
- char *str;
-
- new = PyBytes_FromStringAndSize(NULL, Py_SIZE(x));
- if (new == NULL)
- return NULL;
- str = ((PyBytesObject *)new)->ob_sval;
-
- for (i = 0; i < Py_SIZE(x); i++) {
- value = PyNumber_AsSsize_t(PyList_GET_ITEM(x, i), PyExc_ValueError);
- if (value == -1 && PyErr_Occurred())
- goto error;
-
- if (value < 0 || value >= 256) {
- PyErr_SetString(PyExc_ValueError,
- "bytes must be in range(0, 256)");
- goto error;
- }
- *str++ = (char) value;
- }
- return new;
-
-error:
- Py_DECREF(new);
- return NULL;
+ _PyBytes_FROM_LIST_BODY(x, PyList_GET_ITEM);
}
static PyObject*
_PyBytes_FromTuple(PyObject *x)
{
- PyObject *new;
- Py_ssize_t i;
- Py_ssize_t value;
- char *str;
-
- new = PyBytes_FromStringAndSize(NULL, Py_SIZE(x));
- if (new == NULL)
- return NULL;
- str = ((PyBytesObject *)new)->ob_sval;
-
- for (i = 0; i < Py_SIZE(x); i++) {
- value = PyNumber_AsSsize_t(PyTuple_GET_ITEM(x, i), PyExc_ValueError);
- if (value == -1 && PyErr_Occurred())
- goto error;
-
- if (value < 0 || value >= 256) {
- PyErr_SetString(PyExc_ValueError,
- "bytes must be in range(0, 256)");
- goto error;
- }
- *str++ = (char) value;
- }
- return new;
-
-error:
- Py_DECREF(new);
- return NULL;
+ _PyBytes_FROM_LIST_BODY(x, PyTuple_GET_ITEM);
}
static PyObject *