From bea294ba294d6479b14d5aa2b14153af58abae0d Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Mon, 7 Mar 2022 16:58:55 +0300 Subject: [PATCH 1/4] Port EncodingMap from the core to Argument Clinic --- Objects/clinic/unicodeobject.c.h | 20 ++++++++- Objects/unicodeobject.c | 69 ++++++++++---------------------- 2 files changed, 41 insertions(+), 48 deletions(-) diff --git a/Objects/clinic/unicodeobject.c.h b/Objects/clinic/unicodeobject.c.h index 9ef8ce2e35364c..037333d0c1d9f0 100644 --- a/Objects/clinic/unicodeobject.c.h +++ b/Objects/clinic/unicodeobject.c.h @@ -2,6 +2,24 @@ preserve [clinic start generated code]*/ +PyDoc_STRVAR(EncodingMap_size__doc__, +"size($self, /)\n" +"--\n" +"\n" +"Return the size (in bytes) of this object."); + +#define ENCODINGMAP_SIZE_METHODDEF \ + {"size", (PyCFunction)EncodingMap_size, METH_NOARGS, EncodingMap_size__doc__}, + +static PyObject * +EncodingMap_size_impl(struct encoding_map *self); + +static PyObject * +EncodingMap_size(struct encoding_map *self, PyObject *Py_UNUSED(ignored)) +{ + return EncodingMap_size_impl(self); +} + PyDoc_STRVAR(unicode_title__doc__, "title($self, /)\n" "--\n" @@ -1327,4 +1345,4 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=f10cf85d3935b3b7 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=313c4434bc1bd488 input=a9049054013a1b77]*/ diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 908ad514925999..38f70b876e72cf 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -8367,6 +8367,11 @@ PyUnicode_DecodeCharmap(const char *s, /* Charmap encoding: the lookup table */ +/*[clinic input] +class EncodingMap "struct encoding_map *" "&EncodingMapType" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=14e46bbb6c522d22]*/ + struct encoding_map { PyObject_HEAD unsigned char level1[32]; @@ -8374,62 +8379,32 @@ struct encoding_map { unsigned char level23[1]; }; -static PyObject* -encoding_map_size(PyObject *obj, PyObject* args) +/*[clinic input] +EncodingMap.size + +Return the size (in bytes) of this object. +[clinic start generated code]*/ + +static PyObject * +EncodingMap_size_impl(struct encoding_map *self) +/*[clinic end generated code: output=c4c969e4c99342a4 input=004ff13f26bb5366]*/ { - struct encoding_map *map = (struct encoding_map*)obj; - return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + - 128*map->count3); + return PyLong_FromLong((sizeof(*self) - 1) + 16 * self->count2 + + 128 * self->count3); } static PyMethodDef encoding_map_methods[] = { - {"size", encoding_map_size, METH_NOARGS, - PyDoc_STR("Return the size (in bytes) of this object") }, - { 0 } + ENCODINGMAP_SIZE_METHODDEF + {NULL, NULL} }; static PyTypeObject EncodingMapType = { PyVarObject_HEAD_INIT(NULL, 0) - "EncodingMap", /*tp_name*/ - sizeof(struct encoding_map), /*tp_basicsize*/ - 0, /*tp_itemsize*/ + .tp_name = "EncodingMap", + .tp_basicsize = sizeof(struct encoding_map), /* methods */ - 0, /*tp_dealloc*/ - 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT, /*tp_flags*/ - 0, /*tp_doc*/ - 0, /*tp_traverse*/ - 0, /*tp_clear*/ - 0, /*tp_richcompare*/ - 0, /*tp_weaklistoffset*/ - 0, /*tp_iter*/ - 0, /*tp_iternext*/ - encoding_map_methods, /*tp_methods*/ - 0, /*tp_members*/ - 0, /*tp_getset*/ - 0, /*tp_base*/ - 0, /*tp_dict*/ - 0, /*tp_descr_get*/ - 0, /*tp_descr_set*/ - 0, /*tp_dictoffset*/ - 0, /*tp_init*/ - 0, /*tp_alloc*/ - 0, /*tp_new*/ - 0, /*tp_free*/ - 0, /*tp_is_gc*/ + .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_methods = encoding_map_methods, }; PyObject* From ed58f80950c8d8db95509d0025b6b018b648f8bd Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Mon, 7 Mar 2022 17:58:17 +0300 Subject: [PATCH 2/4] Provide instance type encoding_map to AC --- Objects/unicodeobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 38f70b876e72cf..ac263804cd41bb 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -399,6 +399,7 @@ static const unsigned char ascii_linebreak[] = { static int convert_uc(PyObject *obj, void *addr); +struct encoding_map; #include "clinic/unicodeobject.c.h" _Py_error_handler From b270cb13994ed5416487911ad7dbdafad6b4c949 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Tue, 12 Apr 2022 17:26:40 +0300 Subject: [PATCH 3/4] Restore original spacing of PyLong_FromLong --- Objects/unicodeobject.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index f4116a7e87631f..295016765cd78e 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -8354,8 +8354,8 @@ static PyObject * EncodingMap_size_impl(struct encoding_map *self) /*[clinic end generated code: output=c4c969e4c99342a4 input=004ff13f26bb5366]*/ { - return PyLong_FromLong((sizeof(*self) - 1) + 16 * self->count2 + - 128 * self->count3); + return PyLong_FromLong((sizeof(*self) - 1) + 16*self->count2 + + 128*self->count3); } static PyMethodDef encoding_map_methods[] = { From 8c62cca9f1f66a086bf5349c52e21a4f24ec073c Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Mon, 18 Apr 2022 07:24:08 +0300 Subject: [PATCH 4/4] Add a NEWS entry --- .../2022-04-18-07-23-48.gh-issue-91102.vm-6g1.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-04-18-07-23-48.gh-issue-91102.vm-6g1.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-04-18-07-23-48.gh-issue-91102.vm-6g1.rst b/Misc/NEWS.d/next/Core and Builtins/2022-04-18-07-23-48.gh-issue-91102.vm-6g1.rst new file mode 100644 index 00000000000000..5f3897e6dcd030 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-04-18-07-23-48.gh-issue-91102.vm-6g1.rst @@ -0,0 +1 @@ +Use Argument Clinic for :class:`EncodingMap`. Patch by Oleg Iarygin.