mirror of
https://github.com/python/cpython.git
synced 2025-12-09 02:35:14 +00:00
Fix reference leaks introduced by the recent incremental codec
changes.
This commit is contained in:
parent
40108c97fb
commit
b9c03e999f
2 changed files with 52 additions and 41 deletions
|
|
@ -758,7 +758,9 @@ encoder_encode_stateful(MultibyteStatefulEncoderContext *ctx,
|
||||||
datalen, ctx->errors, final ? MBENC_FLUSH : 0);
|
datalen, ctx->errors, final ? MBENC_FLUSH : 0);
|
||||||
if (r == NULL) {
|
if (r == NULL) {
|
||||||
/* recover the original pending buffer */
|
/* recover the original pending buffer */
|
||||||
memcpy(ctx->pending, inbuf_tmp, Py_UNICODE_SIZE * origpending);
|
if (origpending > 0)
|
||||||
|
memcpy(ctx->pending, inbuf_tmp,
|
||||||
|
Py_UNICODE_SIZE * origpending);
|
||||||
ctx->pendingsize = origpending;
|
ctx->pendingsize = origpending;
|
||||||
goto errorexit;
|
goto errorexit;
|
||||||
}
|
}
|
||||||
|
|
@ -887,17 +889,9 @@ static PyObject *
|
||||||
mbiencoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
mbiencoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
MultibyteIncrementalEncoderObject *self;
|
MultibyteIncrementalEncoderObject *self;
|
||||||
PyObject *codec;
|
PyObject *codec = NULL;
|
||||||
char *errors = NULL;
|
char *errors = NULL;
|
||||||
|
|
||||||
codec = PyObject_GetAttrString((PyObject *)type, "codec");
|
|
||||||
if (codec == NULL)
|
|
||||||
return NULL;
|
|
||||||
if (!MultibyteCodec_Check(codec)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:IncrementalEncoder",
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:IncrementalEncoder",
|
||||||
incnewkwarglist, &errors))
|
incnewkwarglist, &errors))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
@ -906,6 +900,14 @@ mbiencoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
codec = PyObject_GetAttrString((PyObject *)type, "codec");
|
||||||
|
if (codec == NULL)
|
||||||
|
goto errorexit;
|
||||||
|
if (!MultibyteCodec_Check(codec)) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
|
||||||
|
goto errorexit;
|
||||||
|
}
|
||||||
|
|
||||||
self->codec = ((MultibyteCodecObject *)codec)->codec;
|
self->codec = ((MultibyteCodecObject *)codec)->codec;
|
||||||
self->pendingsize = 0;
|
self->pendingsize = 0;
|
||||||
self->errors = internal_error_callback(errors);
|
self->errors = internal_error_callback(errors);
|
||||||
|
|
@ -915,10 +917,12 @@ mbiencoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
self->codec->encinit(&self->state, self->codec->config) != 0)
|
self->codec->encinit(&self->state, self->codec->config) != 0)
|
||||||
goto errorexit;
|
goto errorexit;
|
||||||
|
|
||||||
|
Py_DECREF(codec);
|
||||||
return (PyObject *)self;
|
return (PyObject *)self;
|
||||||
|
|
||||||
errorexit:
|
errorexit:
|
||||||
Py_XDECREF(self);
|
Py_XDECREF(self);
|
||||||
|
Py_XDECREF(codec);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1080,17 +1084,9 @@ static PyObject *
|
||||||
mbidecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
mbidecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
MultibyteIncrementalDecoderObject *self;
|
MultibyteIncrementalDecoderObject *self;
|
||||||
PyObject *codec;
|
PyObject *codec = NULL;
|
||||||
char *errors = NULL;
|
char *errors = NULL;
|
||||||
|
|
||||||
codec = PyObject_GetAttrString((PyObject *)type, "codec");
|
|
||||||
if (codec == NULL)
|
|
||||||
return NULL;
|
|
||||||
if (!MultibyteCodec_Check(codec)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:IncrementalDecoder",
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:IncrementalDecoder",
|
||||||
incnewkwarglist, &errors))
|
incnewkwarglist, &errors))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
@ -1099,6 +1095,14 @@ mbidecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
codec = PyObject_GetAttrString((PyObject *)type, "codec");
|
||||||
|
if (codec == NULL)
|
||||||
|
goto errorexit;
|
||||||
|
if (!MultibyteCodec_Check(codec)) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
|
||||||
|
goto errorexit;
|
||||||
|
}
|
||||||
|
|
||||||
self->codec = ((MultibyteCodecObject *)codec)->codec;
|
self->codec = ((MultibyteCodecObject *)codec)->codec;
|
||||||
self->pendingsize = 0;
|
self->pendingsize = 0;
|
||||||
self->errors = internal_error_callback(errors);
|
self->errors = internal_error_callback(errors);
|
||||||
|
|
@ -1108,10 +1112,12 @@ mbidecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
self->codec->decinit(&self->state, self->codec->config) != 0)
|
self->codec->decinit(&self->state, self->codec->config) != 0)
|
||||||
goto errorexit;
|
goto errorexit;
|
||||||
|
|
||||||
|
Py_DECREF(codec);
|
||||||
return (PyObject *)self;
|
return (PyObject *)self;
|
||||||
|
|
||||||
errorexit:
|
errorexit:
|
||||||
Py_XDECREF(self);
|
Py_XDECREF(self);
|
||||||
|
Py_XDECREF(codec);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1381,17 +1387,9 @@ static PyObject *
|
||||||
mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
MultibyteStreamReaderObject *self;
|
MultibyteStreamReaderObject *self;
|
||||||
PyObject *codec, *stream;
|
PyObject *stream, *codec = NULL;
|
||||||
char *errors = NULL;
|
char *errors = NULL;
|
||||||
|
|
||||||
codec = PyObject_GetAttrString((PyObject *)type, "codec");
|
|
||||||
if (codec == NULL)
|
|
||||||
return NULL;
|
|
||||||
if (!MultibyteCodec_Check(codec)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:StreamReader",
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:StreamReader",
|
||||||
streamkwarglist, &stream, &errors))
|
streamkwarglist, &stream, &errors))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
@ -1400,6 +1398,14 @@ mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
codec = PyObject_GetAttrString((PyObject *)type, "codec");
|
||||||
|
if (codec == NULL)
|
||||||
|
goto errorexit;
|
||||||
|
if (!MultibyteCodec_Check(codec)) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
|
||||||
|
goto errorexit;
|
||||||
|
}
|
||||||
|
|
||||||
self->codec = ((MultibyteCodecObject *)codec)->codec;
|
self->codec = ((MultibyteCodecObject *)codec)->codec;
|
||||||
self->stream = stream;
|
self->stream = stream;
|
||||||
Py_INCREF(stream);
|
Py_INCREF(stream);
|
||||||
|
|
@ -1411,10 +1417,12 @@ mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
self->codec->decinit(&self->state, self->codec->config) != 0)
|
self->codec->decinit(&self->state, self->codec->config) != 0)
|
||||||
goto errorexit;
|
goto errorexit;
|
||||||
|
|
||||||
|
Py_DECREF(codec);
|
||||||
return (PyObject *)self;
|
return (PyObject *)self;
|
||||||
|
|
||||||
errorexit:
|
errorexit:
|
||||||
Py_XDECREF(self);
|
Py_XDECREF(self);
|
||||||
|
Py_XDECREF(codec);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1501,6 +1509,7 @@ mbstreamwriter_iwrite(MultibyteStreamWriterObject *self,
|
||||||
if (wr == NULL)
|
if (wr == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
Py_DECREF(wr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1583,17 +1592,9 @@ static PyObject *
|
||||||
mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
MultibyteStreamWriterObject *self;
|
MultibyteStreamWriterObject *self;
|
||||||
PyObject *codec, *stream;
|
PyObject *stream, *codec = NULL;
|
||||||
char *errors = NULL;
|
char *errors = NULL;
|
||||||
|
|
||||||
codec = PyObject_GetAttrString((PyObject *)type, "codec");
|
|
||||||
if (codec == NULL)
|
|
||||||
return NULL;
|
|
||||||
if (!MultibyteCodec_Check(codec)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:StreamWriter",
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:StreamWriter",
|
||||||
streamkwarglist, &stream, &errors))
|
streamkwarglist, &stream, &errors))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
@ -1602,6 +1603,14 @@ mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
codec = PyObject_GetAttrString((PyObject *)type, "codec");
|
||||||
|
if (codec == NULL)
|
||||||
|
goto errorexit;
|
||||||
|
if (!MultibyteCodec_Check(codec)) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
|
||||||
|
goto errorexit;
|
||||||
|
}
|
||||||
|
|
||||||
self->codec = ((MultibyteCodecObject *)codec)->codec;
|
self->codec = ((MultibyteCodecObject *)codec)->codec;
|
||||||
self->stream = stream;
|
self->stream = stream;
|
||||||
Py_INCREF(stream);
|
Py_INCREF(stream);
|
||||||
|
|
@ -1613,10 +1622,12 @@ mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
self->codec->encinit(&self->state, self->codec->config) != 0)
|
self->codec->encinit(&self->state, self->codec->config) != 0)
|
||||||
goto errorexit;
|
goto errorexit;
|
||||||
|
|
||||||
|
Py_DECREF(codec);
|
||||||
return (PyObject *)self;
|
return (PyObject *)self;
|
||||||
|
|
||||||
errorexit:
|
errorexit:
|
||||||
Py_XDECREF(self);
|
Py_XDECREF(self);
|
||||||
|
Py_XDECREF(codec);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,7 @@ typedef struct {
|
||||||
#define ERROR_REPLACE (PyObject *)(3)
|
#define ERROR_REPLACE (PyObject *)(3)
|
||||||
#define ERROR_ISCUSTOM(p) ((p) < ERROR_STRICT || ERROR_REPLACE < (p))
|
#define ERROR_ISCUSTOM(p) ((p) < ERROR_STRICT || ERROR_REPLACE < (p))
|
||||||
#define ERROR_DECREF(p) do { \
|
#define ERROR_DECREF(p) do { \
|
||||||
if (ERROR_ISCUSTOM(p)) { \
|
if (p != NULL && ERROR_ISCUSTOM(p)) { \
|
||||||
Py_DECREF(p); \
|
Py_DECREF(p); \
|
||||||
} \
|
} \
|
||||||
} while (0);
|
} while (0);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue