mirror of
https://github.com/python/cpython.git
synced 2025-07-30 22:54:16 +00:00
Fix wrong argument format in PyCodec_IncrementalEncoder() and
PyCodec_IncrementalDecoder(). Factor out common code from PyCodec_Encoder()/PyCodec_Decoder(), PyCodec_IncrementalEncoder()/PyCodec_IncrementalDecoder() and PyCodec_StreamReader()/PyCodec_StreamWriter().
This commit is contained in:
parent
23e408603c
commit
d53850a2be
1 changed files with 62 additions and 101 deletions
163
Python/codecs.c
163
Python/codecs.c
|
@ -200,24 +200,67 @@ PyObject *args_tuple(PyObject *object,
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Build a codec by calling factory(stream[,errors]) or just
|
/* Helper function to get a codec item */
|
||||||
factory(errors) depending on whether the given parameters are
|
|
||||||
non-NULL. */
|
|
||||||
|
|
||||||
static
|
static
|
||||||
PyObject *build_stream_codec(PyObject *factory,
|
PyObject *codec_getitem(const char *encoding, int index)
|
||||||
PyObject *stream,
|
|
||||||
const char *errors)
|
|
||||||
{
|
{
|
||||||
PyObject *args, *codec;
|
PyObject *codecs;
|
||||||
|
PyObject *v;
|
||||||
|
|
||||||
args = args_tuple(stream, errors);
|
codecs = _PyCodec_Lookup(encoding);
|
||||||
if (args == NULL)
|
if (codecs == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
v = PyTuple_GET_ITEM(codecs, index);
|
||||||
codec = PyEval_CallObject(factory, args);
|
Py_DECREF(codecs);
|
||||||
Py_DECREF(args);
|
Py_INCREF(v);
|
||||||
return codec;
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Helper function to create an incremental codec. */
|
||||||
|
|
||||||
|
static
|
||||||
|
PyObject *codec_getincrementalcodec(const char *encoding,
|
||||||
|
const char *errors,
|
||||||
|
const char *attrname)
|
||||||
|
{
|
||||||
|
PyObject *codecs, *ret, *inccodec;
|
||||||
|
|
||||||
|
codecs = _PyCodec_Lookup(encoding);
|
||||||
|
if (codecs == NULL)
|
||||||
|
return NULL;
|
||||||
|
inccodec = PyObject_GetAttrString(codecs, attrname);
|
||||||
|
if (inccodec == NULL) {
|
||||||
|
Py_DECREF(codecs);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (errors)
|
||||||
|
ret = PyObject_CallFunction(inccodec, "s", errors);
|
||||||
|
else
|
||||||
|
ret = PyObject_CallFunction(inccodec, NULL);
|
||||||
|
Py_DECREF(inccodec);
|
||||||
|
Py_DECREF(codecs);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Helper function to create a stream codec. */
|
||||||
|
|
||||||
|
static
|
||||||
|
PyObject *codec_getstreamcodec(const char *encoding,
|
||||||
|
PyObject *stream,
|
||||||
|
const char *errors,
|
||||||
|
const int index)
|
||||||
|
{
|
||||||
|
PyObject *codecs, *streamcodec;
|
||||||
|
|
||||||
|
codecs = _PyCodec_Lookup(encoding);
|
||||||
|
if (codecs == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
streamcodec = PyEval_CallFunction(
|
||||||
|
PyTuple_GET_ITEM(codecs, index), "Os", stream, errors);
|
||||||
|
Py_DECREF(codecs);
|
||||||
|
return streamcodec;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Convenience APIs to query the Codec registry.
|
/* Convenience APIs to query the Codec registry.
|
||||||
|
@ -228,120 +271,38 @@ PyObject *build_stream_codec(PyObject *factory,
|
||||||
|
|
||||||
PyObject *PyCodec_Encoder(const char *encoding)
|
PyObject *PyCodec_Encoder(const char *encoding)
|
||||||
{
|
{
|
||||||
PyObject *codecs;
|
return codec_getitem(encoding, 0);
|
||||||
PyObject *v;
|
|
||||||
|
|
||||||
codecs = _PyCodec_Lookup(encoding);
|
|
||||||
if (codecs == NULL)
|
|
||||||
goto onError;
|
|
||||||
v = PyTuple_GET_ITEM(codecs,0);
|
|
||||||
Py_DECREF(codecs);
|
|
||||||
Py_INCREF(v);
|
|
||||||
return v;
|
|
||||||
|
|
||||||
onError:
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *PyCodec_Decoder(const char *encoding)
|
PyObject *PyCodec_Decoder(const char *encoding)
|
||||||
{
|
{
|
||||||
PyObject *codecs;
|
return codec_getitem(encoding, 1);
|
||||||
PyObject *v;
|
|
||||||
|
|
||||||
codecs = _PyCodec_Lookup(encoding);
|
|
||||||
if (codecs == NULL)
|
|
||||||
goto onError;
|
|
||||||
v = PyTuple_GET_ITEM(codecs,1);
|
|
||||||
Py_DECREF(codecs);
|
|
||||||
Py_INCREF(v);
|
|
||||||
return v;
|
|
||||||
|
|
||||||
onError:
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *PyCodec_IncrementalEncoder(const char *encoding,
|
PyObject *PyCodec_IncrementalEncoder(const char *encoding,
|
||||||
const char *errors)
|
const char *errors)
|
||||||
{
|
{
|
||||||
PyObject *codecs, *ret, *encoder;
|
return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
|
||||||
|
|
||||||
codecs = _PyCodec_Lookup(encoding);
|
|
||||||
if (codecs == NULL)
|
|
||||||
goto onError;
|
|
||||||
encoder = PyObject_GetAttrString(codecs, "incrementalencoder");
|
|
||||||
if (encoder == NULL) {
|
|
||||||
Py_DECREF(codecs);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (errors)
|
|
||||||
ret = PyObject_CallFunction(encoder, "O", errors);
|
|
||||||
else
|
|
||||||
ret = PyObject_CallFunction(encoder, NULL);
|
|
||||||
Py_DECREF(encoder);
|
|
||||||
Py_DECREF(codecs);
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
onError:
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *PyCodec_IncrementalDecoder(const char *encoding,
|
PyObject *PyCodec_IncrementalDecoder(const char *encoding,
|
||||||
const char *errors)
|
const char *errors)
|
||||||
{
|
{
|
||||||
PyObject *codecs, *ret, *decoder;
|
return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
|
||||||
|
|
||||||
codecs = _PyCodec_Lookup(encoding);
|
|
||||||
if (codecs == NULL)
|
|
||||||
goto onError;
|
|
||||||
decoder = PyObject_GetAttrString(codecs, "incrementaldecoder");
|
|
||||||
if (decoder == NULL) {
|
|
||||||
Py_DECREF(codecs);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (errors)
|
|
||||||
ret = PyObject_CallFunction(decoder, "O", errors);
|
|
||||||
else
|
|
||||||
ret = PyObject_CallFunction(decoder, NULL);
|
|
||||||
Py_DECREF(decoder);
|
|
||||||
Py_DECREF(codecs);
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
onError:
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *PyCodec_StreamReader(const char *encoding,
|
PyObject *PyCodec_StreamReader(const char *encoding,
|
||||||
PyObject *stream,
|
PyObject *stream,
|
||||||
const char *errors)
|
const char *errors)
|
||||||
{
|
{
|
||||||
PyObject *codecs, *ret;
|
return codec_getstreamcodec(encoding, stream, errors, 2);
|
||||||
|
|
||||||
codecs = _PyCodec_Lookup(encoding);
|
|
||||||
if (codecs == NULL)
|
|
||||||
goto onError;
|
|
||||||
ret = build_stream_codec(PyTuple_GET_ITEM(codecs,2),stream,errors);
|
|
||||||
Py_DECREF(codecs);
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
onError:
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *PyCodec_StreamWriter(const char *encoding,
|
PyObject *PyCodec_StreamWriter(const char *encoding,
|
||||||
PyObject *stream,
|
PyObject *stream,
|
||||||
const char *errors)
|
const char *errors)
|
||||||
{
|
{
|
||||||
PyObject *codecs, *ret;
|
return codec_getstreamcodec(encoding, stream, errors, 3);
|
||||||
|
|
||||||
codecs = _PyCodec_Lookup(encoding);
|
|
||||||
if (codecs == NULL)
|
|
||||||
goto onError;
|
|
||||||
ret = build_stream_codec(PyTuple_GET_ITEM(codecs,3),stream,errors);
|
|
||||||
Py_DECREF(codecs);
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
onError:
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Encode an object (e.g. an Unicode object) using the given encoding
|
/* Encode an object (e.g. an Unicode object) using the given encoding
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue