mirror of
https://github.com/python/cpython.git
synced 2025-07-28 13:44:43 +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;
|
||||
}
|
||||
|
||||
/* Build a codec by calling factory(stream[,errors]) or just
|
||||
factory(errors) depending on whether the given parameters are
|
||||
non-NULL. */
|
||||
/* Helper function to get a codec item */
|
||||
|
||||
static
|
||||
PyObject *build_stream_codec(PyObject *factory,
|
||||
PyObject *stream,
|
||||
const char *errors)
|
||||
PyObject *codec_getitem(const char *encoding, int index)
|
||||
{
|
||||
PyObject *args, *codec;
|
||||
PyObject *codecs;
|
||||
PyObject *v;
|
||||
|
||||
args = args_tuple(stream, errors);
|
||||
if (args == NULL)
|
||||
codecs = _PyCodec_Lookup(encoding);
|
||||
if (codecs == NULL)
|
||||
return NULL;
|
||||
|
||||
codec = PyEval_CallObject(factory, args);
|
||||
Py_DECREF(args);
|
||||
return codec;
|
||||
v = PyTuple_GET_ITEM(codecs, index);
|
||||
Py_DECREF(codecs);
|
||||
Py_INCREF(v);
|
||||
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.
|
||||
|
@ -228,120 +271,38 @@ PyObject *build_stream_codec(PyObject *factory,
|
|||
|
||||
PyObject *PyCodec_Encoder(const char *encoding)
|
||||
{
|
||||
PyObject *codecs;
|
||||
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;
|
||||
return codec_getitem(encoding, 0);
|
||||
}
|
||||
|
||||
PyObject *PyCodec_Decoder(const char *encoding)
|
||||
{
|
||||
PyObject *codecs;
|
||||
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;
|
||||
return codec_getitem(encoding, 1);
|
||||
}
|
||||
|
||||
PyObject *PyCodec_IncrementalEncoder(const char *encoding,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *codecs, *ret, *encoder;
|
||||
|
||||
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;
|
||||
return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
|
||||
}
|
||||
|
||||
PyObject *PyCodec_IncrementalDecoder(const char *encoding,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *codecs, *ret, *decoder;
|
||||
|
||||
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;
|
||||
return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
|
||||
}
|
||||
|
||||
PyObject *PyCodec_StreamReader(const char *encoding,
|
||||
PyObject *stream,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *codecs, *ret;
|
||||
|
||||
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;
|
||||
return codec_getstreamcodec(encoding, stream, errors, 2);
|
||||
}
|
||||
|
||||
PyObject *PyCodec_StreamWriter(const char *encoding,
|
||||
PyObject *stream,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *codecs, *ret;
|
||||
|
||||
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;
|
||||
return codec_getstreamcodec(encoding, stream, errors, 3);
|
||||
}
|
||||
|
||||
/* Encode an object (e.g. an Unicode object) using the given encoding
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue