Skip to content

Commit

Permalink
bpo-46541: Remove usage of _Py_IDENTIFIER from multibytecodec (GH-31475)
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 authored Mar 1, 2022
1 parent c60e6b6 commit 0cc6364
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 25 deletions.
72 changes: 65 additions & 7 deletions Modules/cjkcodecs/clinic/multibytecodec.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 38 additions & 18 deletions Modules/cjkcodecs/multibytecodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

#define PY_SSIZE_T_CLEAN
#define NEEDS_PY_IDENTIFIER
#include "Python.h"
#include "structmember.h" // PyMemberDef
#include "multibytecodec.h"
Expand All @@ -19,6 +18,7 @@ typedef struct {
PyTypeObject *reader_type;
PyTypeObject *writer_type;
PyTypeObject *multibytecodec_type;
PyObject *str_write;
} _multibytecodec_state;

static _multibytecodec_state *
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -1715,15 +1713,15 @@ static PyType_Spec reader_spec = {

static int
mbstreamwriter_iwrite(MultibyteStreamWriterObject *self,
PyObject *unistr)
PyObject *unistr, PyObject *str_write)
{
PyObject *str, *wr;

str = encoder_encode_stateful(STATEFUL_ECTX(self), unistr, 0);
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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0cc6364

Please sign in to comment.