diff options
| author | Dong-hee Na <donghee.na@python.org> | 2021-08-23 10:01:51 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-23 19:01:51 +0900 |
| commit | 24b63c695ae0a95b06379eaadace66735abac1e2 (patch) | |
| tree | 8bc8bbc38511c504860b494e125c232f51457638 /Objects | |
| parent | 6082bb5addab93755ab6e2bd2ed6021b391e10d1 (diff) | |
| download | cpython-git-24b63c695ae0a95b06379eaadace66735abac1e2.tar.gz | |
bpo-24234: Implement bytes.__bytes__ (GH-27901)
Diffstat (limited to 'Objects')
| -rw-r--r-- | Objects/bytesobject.c | 20 | ||||
| -rw-r--r-- | Objects/clinic/bytesobject.c.h | 20 |
2 files changed, 39 insertions, 1 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index eaedb0b568..13f94b4c82 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -1687,6 +1687,25 @@ static PyBufferProcs bytes_as_buffer = { }; +/*[clinic input] +bytes.__bytes__ +Convert this value to exact type bytes. +[clinic start generated code]*/ + +static PyObject * +bytes___bytes___impl(PyBytesObject *self) +/*[clinic end generated code: output=63a306a9bc0caac5 input=34ec5ddba98bd6bb]*/ +{ + if (PyBytes_CheckExact(self)) { + Py_INCREF(self); + return (PyObject *)self; + } + else { + return PyBytes_FromString(self->ob_sval); + } +} + + #define LEFTSTRIP 0 #define RIGHTSTRIP 1 #define BOTHSTRIP 2 @@ -2474,6 +2493,7 @@ bytes_getnewargs(PyBytesObject *v, PyObject *Py_UNUSED(ignored)) static PyMethodDef bytes_methods[] = { {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS}, + BYTES___BYTES___METHODDEF {"capitalize", stringlib_capitalize, METH_NOARGS, _Py_capitalize__doc__}, STRINGLIB_CENTER_METHODDEF diff --git a/Objects/clinic/bytesobject.c.h b/Objects/clinic/bytesobject.c.h index 9e365ce1a0..103a364281 100644 --- a/Objects/clinic/bytesobject.c.h +++ b/Objects/clinic/bytesobject.c.h @@ -2,6 +2,24 @@ preserve [clinic start generated code]*/ +PyDoc_STRVAR(bytes___bytes____doc__, +"__bytes__($self, /)\n" +"--\n" +"\n" +"Convert this value to exact type bytes."); + +#define BYTES___BYTES___METHODDEF \ + {"__bytes__", (PyCFunction)bytes___bytes__, METH_NOARGS, bytes___bytes____doc__}, + +static PyObject * +bytes___bytes___impl(PyBytesObject *self); + +static PyObject * +bytes___bytes__(PyBytesObject *self, PyObject *Py_UNUSED(ignored)) +{ + return bytes___bytes___impl(self); +} + PyDoc_STRVAR(bytes_split__doc__, "split($self, /, sep=None, maxsplit=-1)\n" "--\n" @@ -878,4 +896,4 @@ skip_optional_pos: exit: return return_value; } -/*[clinic end generated code: output=b3f0ec2753246b9c input=a9049054013a1b77]*/ +/*[clinic end generated code: output=d706344859f40122 input=a9049054013a1b77]*/ |
