datalen, ctx->errors, final ? MBENC_FLUSH : 0);
if (r == NULL) {
/* 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;
goto errorexit;
}
mbiencoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
MultibyteIncrementalEncoderObject *self;
- PyObject *codec;
+ PyObject *codec = 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",
incnewkwarglist, &errors))
return NULL;
if (self == 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->pendingsize = 0;
self->errors = internal_error_callback(errors);
self->codec->encinit(&self->state, self->codec->config) != 0)
goto errorexit;
+ Py_DECREF(codec);
return (PyObject *)self;
errorexit:
Py_XDECREF(self);
+ Py_XDECREF(codec);
return NULL;
}
mbidecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
MultibyteIncrementalDecoderObject *self;
- PyObject *codec;
+ PyObject *codec = 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",
incnewkwarglist, &errors))
return NULL;
if (self == 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->pendingsize = 0;
self->errors = internal_error_callback(errors);
self->codec->decinit(&self->state, self->codec->config) != 0)
goto errorexit;
+ Py_DECREF(codec);
return (PyObject *)self;
errorexit:
Py_XDECREF(self);
+ Py_XDECREF(codec);
return NULL;
}
mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
MultibyteStreamReaderObject *self;
- PyObject *codec, *stream;
+ PyObject *stream, *codec = 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",
streamkwarglist, &stream, &errors))
return NULL;
if (self == 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->stream = stream;
Py_INCREF(stream);
self->codec->decinit(&self->state, self->codec->config) != 0)
goto errorexit;
+ Py_DECREF(codec);
return (PyObject *)self;
errorexit:
Py_XDECREF(self);
+ Py_XDECREF(codec);
return NULL;
}
if (wr == NULL)
return -1;
+ Py_DECREF(wr);
return 0;
}
mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
MultibyteStreamWriterObject *self;
- PyObject *codec, *stream;
+ PyObject *stream, *codec = 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",
streamkwarglist, &stream, &errors))
return NULL;
if (self == 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->stream = stream;
Py_INCREF(stream);
self->codec->encinit(&self->state, self->codec->config) != 0)
goto errorexit;
+ Py_DECREF(codec);
return (PyObject *)self;
errorexit:
Py_XDECREF(self);
+ Py_XDECREF(codec);
return NULL;
}