diff options
author | Dong-hee Na <donghee.na@python.org> | 2022-03-01 14:35:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-01 14:35:43 (GMT) |
commit | 0cc63641859b2f60ea65bb7c0b6d1cfcec1e2f1a (patch) | |
tree | 998a2163ebb3c585e70068c261ecb7596f9c014d /Modules | |
parent | c60e6b6ad7aaf9c72035ff9fb1575e2710aeb4b4 (diff) | |
download | cpython-0cc63641859b2f60ea65bb7c0b6d1cfcec1e2f1a.zip cpython-0cc63641859b2f60ea65bb7c0b6d1cfcec1e2f1a.tar.gz cpython-0cc63641859b2f60ea65bb7c0b6d1cfcec1e2f1a.tar.bz2 |
bpo-46541: Remove usage of _Py_IDENTIFIER from multibytecodec (GH-31475)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/cjkcodecs/clinic/multibytecodec.c.h | 72 | ||||
-rw-r--r-- | Modules/cjkcodecs/multibytecodec.c | 56 |
2 files changed, 103 insertions, 25 deletions
diff --git a/Modules/cjkcodecs/clinic/multibytecodec.c.h b/Modules/cjkcodecs/clinic/multibytecodec.c.h index 5638883..1dfb9a1 100644 --- a/Modules/cjkcodecs/clinic/multibytecodec.c.h +++ b/Modules/cjkcodecs/clinic/multibytecodec.c.h @@ -481,7 +481,30 @@ PyDoc_STRVAR(_multibytecodec_MultibyteStreamWriter_write__doc__, "\n"); #define _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_WRITE_METHODDEF \ - {"write", (PyCFunction)_multibytecodec_MultibyteStreamWriter_write, METH_O, _multibytecodec_MultibyteStreamWriter_write__doc__}, + {"write", (PyCFunction)(void(*)(void))_multibytecodec_MultibyteStreamWriter_write, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _multibytecodec_MultibyteStreamWriter_write__doc__}, + +static PyObject * +_multibytecodec_MultibyteStreamWriter_write_impl(MultibyteStreamWriterObject *self, + PyTypeObject *cls, + PyObject *strobj); + +static PyObject * +_multibytecodec_MultibyteStreamWriter_write(MultibyteStreamWriterObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"", NULL}; + static _PyArg_Parser _parser = {"O:write", _keywords, 0}; + PyObject *strobj; + + if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, + &strobj)) { + goto exit; + } + return_value = _multibytecodec_MultibyteStreamWriter_write_impl(self, cls, strobj); + +exit: + return return_value; +} PyDoc_STRVAR(_multibytecodec_MultibyteStreamWriter_writelines__doc__, "writelines($self, lines, /)\n" @@ -489,7 +512,30 @@ PyDoc_STRVAR(_multibytecodec_MultibyteStreamWriter_writelines__doc__, "\n"); #define _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_WRITELINES_METHODDEF \ - {"writelines", (PyCFunction)_multibytecodec_MultibyteStreamWriter_writelines, METH_O, _multibytecodec_MultibyteStreamWriter_writelines__doc__}, + {"writelines", (PyCFunction)(void(*)(void))_multibytecodec_MultibyteStreamWriter_writelines, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _multibytecodec_MultibyteStreamWriter_writelines__doc__}, + +static PyObject * +_multibytecodec_MultibyteStreamWriter_writelines_impl(MultibyteStreamWriterObject *self, + PyTypeObject *cls, + PyObject *lines); + +static PyObject * +_multibytecodec_MultibyteStreamWriter_writelines(MultibyteStreamWriterObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"", NULL}; + static _PyArg_Parser _parser = {"O:writelines", _keywords, 0}; + PyObject *lines; + + if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, + &lines)) { + goto exit; + } + return_value = _multibytecodec_MultibyteStreamWriter_writelines_impl(self, cls, lines); + +exit: + return return_value; +} PyDoc_STRVAR(_multibytecodec_MultibyteStreamWriter_reset__doc__, "reset($self, /)\n" @@ -497,15 +543,27 @@ PyDoc_STRVAR(_multibytecodec_MultibyteStreamWriter_reset__doc__, "\n"); #define _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_RESET_METHODDEF \ - {"reset", (PyCFunction)_multibytecodec_MultibyteStreamWriter_reset, METH_NOARGS, _multibytecodec_MultibyteStreamWriter_reset__doc__}, + {"reset", (PyCFunction)(void(*)(void))_multibytecodec_MultibyteStreamWriter_reset, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _multibytecodec_MultibyteStreamWriter_reset__doc__}, static PyObject * -_multibytecodec_MultibyteStreamWriter_reset_impl(MultibyteStreamWriterObject *self); +_multibytecodec_MultibyteStreamWriter_reset_impl(MultibyteStreamWriterObject *self, + PyTypeObject *cls); static PyObject * -_multibytecodec_MultibyteStreamWriter_reset(MultibyteStreamWriterObject *self, PyObject *Py_UNUSED(ignored)) +_multibytecodec_MultibyteStreamWriter_reset(MultibyteStreamWriterObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) { - return _multibytecodec_MultibyteStreamWriter_reset_impl(self); + PyObject *return_value = NULL; + static const char * const _keywords[] = { NULL}; + static _PyArg_Parser _parser = {":reset", _keywords, 0}; + + if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser + )) { + goto exit; + } + return_value = _multibytecodec_MultibyteStreamWriter_reset_impl(self, cls); + +exit: + return return_value; } PyDoc_STRVAR(_multibytecodec___create_codec__doc__, @@ -515,4 +573,4 @@ PyDoc_STRVAR(_multibytecodec___create_codec__doc__, #define _MULTIBYTECODEC___CREATE_CODEC_METHODDEF \ {"__create_codec", (PyCFunction)_multibytecodec___create_codec, METH_O, _multibytecodec___create_codec__doc__}, -/*[clinic end generated code: output=5c0f74129db07c87 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=8813c05077580bda input=a9049054013a1b77]*/ diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c index acfe966..4769ab2 100644 --- a/Modules/cjkcodecs/multibytecodec.c +++ b/Modules/cjkcodecs/multibytecodec.c @@ -5,7 +5,6 @@ */ #define PY_SSIZE_T_CLEAN -#define NEEDS_PY_IDENTIFIER #include "Python.h" #include "structmember.h" // PyMemberDef #include "multibytecodec.h" @@ -19,6 +18,7 @@ typedef struct { PyTypeObject *reader_type; PyTypeObject *writer_type; PyTypeObject *multibytecodec_type; + PyObject *str_write; } _multibytecodec_state; static _multibytecodec_state * @@ -72,8 +72,6 @@ static PyObject *multibytecodec_encode(MultibyteCodec *, #define MBENC_RESET MBENC_MAX<<1 /* reset after an encoding session */ -_Py_IDENTIFIER(write); - static PyObject * make_tuple(PyObject *object, Py_ssize_t len) { @@ -1715,7 +1713,7 @@ static PyType_Spec reader_spec = { static int mbstreamwriter_iwrite(MultibyteStreamWriterObject *self, - PyObject *unistr) + PyObject *unistr, PyObject *str_write) { PyObject *str, *wr; @@ -1723,7 +1721,7 @@ mbstreamwriter_iwrite(MultibyteStreamWriterObject *self, if (str == NULL) return -1; - wr = _PyObject_CallMethodIdOneArg(self->stream, &PyId_write, str); + wr = _PyObject_CallMethodOneArg(self->stream, str_write, str); Py_DECREF(str); if (wr == NULL) return -1; @@ -1735,32 +1733,38 @@ mbstreamwriter_iwrite(MultibyteStreamWriterObject *self, /*[clinic input] _multibytecodec.MultibyteStreamWriter.write + cls: defining_class strobj: object / [clinic start generated code]*/ static PyObject * -_multibytecodec_MultibyteStreamWriter_write(MultibyteStreamWriterObject *self, - PyObject *strobj) -/*[clinic end generated code: output=e13ae841c895251e input=551dc4c018c10a2b]*/ +_multibytecodec_MultibyteStreamWriter_write_impl(MultibyteStreamWriterObject *self, + PyTypeObject *cls, + PyObject *strobj) +/*[clinic end generated code: output=68ade3aea26410ac input=199f26f68bd8425a]*/ { - if (mbstreamwriter_iwrite(self, strobj)) + _multibytecodec_state *state = PyType_GetModuleState(cls); + assert(state != NULL); + if (mbstreamwriter_iwrite(self, strobj, state->str_write)) { return NULL; - else - Py_RETURN_NONE; + } + Py_RETURN_NONE; } /*[clinic input] _multibytecodec.MultibyteStreamWriter.writelines + cls: defining_class lines: object / [clinic start generated code]*/ static PyObject * -_multibytecodec_MultibyteStreamWriter_writelines(MultibyteStreamWriterObject *self, - PyObject *lines) -/*[clinic end generated code: output=e5c4285ac8e7d522 input=57797fe7008d4e96]*/ +_multibytecodec_MultibyteStreamWriter_writelines_impl(MultibyteStreamWriterObject *self, + PyTypeObject *cls, + PyObject *lines) +/*[clinic end generated code: output=b4c99d2cf23ffb88 input=a6d5fe7c74972a34]*/ { PyObject *strobj; int i, r; @@ -1771,13 +1775,15 @@ _multibytecodec_MultibyteStreamWriter_writelines(MultibyteStreamWriterObject *se return NULL; } + _multibytecodec_state *state = PyType_GetModuleState(cls); + assert(state != NULL); for (i = 0; i < PySequence_Length(lines); i++) { /* length can be changed even within this loop */ strobj = PySequence_GetItem(lines, i); if (strobj == NULL) return NULL; - r = mbstreamwriter_iwrite(self, strobj); + r = mbstreamwriter_iwrite(self, strobj, state->str_write); Py_DECREF(strobj); if (r == -1) return NULL; @@ -1791,11 +1797,16 @@ _multibytecodec_MultibyteStreamWriter_writelines(MultibyteStreamWriterObject *se /*[clinic input] _multibytecodec.MultibyteStreamWriter.reset + + cls: defining_class + / + [clinic start generated code]*/ static PyObject * -_multibytecodec_MultibyteStreamWriter_reset_impl(MultibyteStreamWriterObject *self) -/*[clinic end generated code: output=8f54a4d9b03db5ff input=b56dbcbaf35cc10c]*/ +_multibytecodec_MultibyteStreamWriter_reset_impl(MultibyteStreamWriterObject *self, + PyTypeObject *cls) +/*[clinic end generated code: output=32ef224c2a38aa3d input=28af6a9cd38d1979]*/ { PyObject *pwrt; @@ -1814,10 +1825,14 @@ _multibytecodec_MultibyteStreamWriter_reset_impl(MultibyteStreamWriterObject *se return NULL; assert(PyBytes_Check(pwrt)); + + _multibytecodec_state *state = PyType_GetModuleState(cls); + assert(state != NULL); + if (PyBytes_Size(pwrt) > 0) { PyObject *wr; - wr = _PyObject_CallMethodIdOneArg(self->stream, &PyId_write, pwrt); + wr = _PyObject_CallMethodOneArg(self->stream, state->str_write, pwrt); if (wr == NULL) { Py_DECREF(pwrt); return NULL; @@ -1989,6 +2004,7 @@ _multibytecodec_clear(PyObject *mod) Py_CLEAR(state->decoder_type); Py_CLEAR(state->reader_type); Py_CLEAR(state->writer_type); + Py_CLEAR(state->str_write); return 0; } @@ -2017,6 +2033,10 @@ static int _multibytecodec_exec(PyObject *mod) { _multibytecodec_state *state = _multibytecodec_get_state(mod); + state->str_write = PyUnicode_InternFromString("write"); + if (state->str_write == NULL) { + return -1; + } CREATE_TYPE(mod, state->multibytecodec_type, &multibytecodec_spec); CREATE_TYPE(mod, state->encoder_type, &encoder_spec); CREATE_TYPE(mod, state->decoder_type, &decoder_spec); |