diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2015-01-24 04:05:18 (GMT) |
---|---|---|
committer | Ethan Furman <ethan@stoneleaf.us> | 2015-01-24 04:05:18 (GMT) |
commit | b95b56150fc3e7834783b54acdddeaed4fe44e27 (patch) | |
tree | c1994946e84b457841024402b50f8a9640211cb4 /Objects/bytearrayobject.c | |
parent | 8861502e0746465c4124548681f05969c08f4cae (diff) | |
download | cpython-b95b56150fc3e7834783b54acdddeaed4fe44e27.zip cpython-b95b56150fc3e7834783b54acdddeaed4fe44e27.tar.gz cpython-b95b56150fc3e7834783b54acdddeaed4fe44e27.tar.bz2 |
Issue20284: Implement PEP461
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r-- | Objects/bytearrayobject.c | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 47d480f..b9a87d6 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -4,6 +4,7 @@ #include "Python.h" #include "structmember.h" #include "bytes_methods.h" +#include "bytesobject.h" /*[clinic input] class bytearray "PyByteArrayObject *" "&PyByteArray_Type" @@ -294,6 +295,31 @@ PyByteArray_Concat(PyObject *a, PyObject *b) return (PyObject *)result; } +static PyObject * +bytearray_format(PyByteArrayObject *self, PyObject *args) +{ + PyObject *bytes_in, *bytes_out, *res; + char *bytestring; + + if (self == NULL || !PyByteArray_Check(self) || args == NULL) { + PyErr_BadInternalCall(); + return NULL; + } + bytestring = PyByteArray_AS_STRING(self); + bytes_in = PyBytes_FromString(bytestring); + if (bytes_in == NULL) + return NULL; + bytes_out = _PyBytes_Format(bytes_in, args); + Py_DECREF(bytes_in); + if (bytes_out == NULL) + return NULL; + res = PyByteArray_FromObject(bytes_out); + Py_DECREF(bytes_out); + if (res == NULL) + return NULL; + return res; +} + /* Functions stuffed into the type object */ static Py_ssize_t @@ -3723,6 +3749,21 @@ bytearray_methods[] = { {NULL} }; +static PyObject * +bytearray_mod(PyObject *v, PyObject *w) +{ + if (!PyByteArray_Check(v)) + Py_RETURN_NOTIMPLEMENTED; + return bytearray_format((PyByteArrayObject *)v, w); +} + +static PyNumberMethods bytearray_as_number = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + bytearray_mod, /*nb_remainder*/ +}; + PyDoc_STRVAR(bytearray_doc, "bytearray(iterable_of_ints) -> bytearray\n\ bytearray(string, encoding[, errors]) -> bytearray\n\ @@ -3751,7 +3792,7 @@ PyTypeObject PyByteArray_Type = { 0, /* tp_setattr */ 0, /* tp_reserved */ (reprfunc)bytearray_repr, /* tp_repr */ - 0, /* tp_as_number */ + &bytearray_as_number, /* tp_as_number */ &bytearray_as_sequence, /* tp_as_sequence */ &bytearray_as_mapping, /* tp_as_mapping */ 0, /* tp_hash */ |