diff options
author | Walter Dörwald <walter@livinglogic.de> | 2006-03-16 21:46:40 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2006-03-16 21:46:40 (GMT) |
commit | d53850a2be3fc086493ec7feaa6c4e757de3482e (patch) | |
tree | d45e90d28c85b0450807b3049bb7bd1067dc531f | |
parent | 23e408603cffc2c1e515230deb7239e95f6173cb (diff) | |
download | cpython-d53850a2be3fc086493ec7feaa6c4e757de3482e.zip cpython-d53850a2be3fc086493ec7feaa6c4e757de3482e.tar.gz cpython-d53850a2be3fc086493ec7feaa6c4e757de3482e.tar.bz2 |
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().
-rw-r--r-- | Python/codecs.c | 153 |
1 files changed, 57 insertions, 96 deletions
diff --git a/Python/codecs.c b/Python/codecs.c index 0e8c374..532f1a6 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -200,148 +200,109 @@ 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 *args, *codec; - - args = args_tuple(stream, errors); - if (args == NULL) - return NULL; - - codec = PyEval_CallObject(factory, args); - Py_DECREF(args); - return codec; -} - -/* Convenience APIs to query the Codec registry. - - All APIs return a codec object with incremented refcount. - - */ - -PyObject *PyCodec_Encoder(const char *encoding) +PyObject *codec_getitem(const char *encoding, int index) { PyObject *codecs; PyObject *v; codecs = _PyCodec_Lookup(encoding); if (codecs == NULL) - goto onError; - v = PyTuple_GET_ITEM(codecs,0); + return NULL; + v = PyTuple_GET_ITEM(codecs, index); Py_DECREF(codecs); Py_INCREF(v); return v; - - onError: - return NULL; } -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; +/* Helper function to create an incremental codec. */ - onError: - return NULL; -} - -PyObject *PyCodec_IncrementalEncoder(const char *encoding, - const char *errors) +static +PyObject *codec_getincrementalcodec(const char *encoding, + const char *errors, + const char *attrname) { - PyObject *codecs, *ret, *encoder; + PyObject *codecs, *ret, *inccodec; codecs = _PyCodec_Lookup(encoding); if (codecs == NULL) - goto onError; - encoder = PyObject_GetAttrString(codecs, "incrementalencoder"); - if (encoder == NULL) { + return NULL; + inccodec = PyObject_GetAttrString(codecs, attrname); + if (inccodec == NULL) { Py_DECREF(codecs); return NULL; } if (errors) - ret = PyObject_CallFunction(encoder, "O", errors); + ret = PyObject_CallFunction(inccodec, "s", errors); else - ret = PyObject_CallFunction(encoder, NULL); - Py_DECREF(encoder); + ret = PyObject_CallFunction(inccodec, NULL); + Py_DECREF(inccodec); Py_DECREF(codecs); return ret; - - onError: - return NULL; } -PyObject *PyCodec_IncrementalDecoder(const char *encoding, - const char *errors) +/* Helper function to create a stream codec. */ + +static +PyObject *codec_getstreamcodec(const char *encoding, + PyObject *stream, + const char *errors, + const int index) { - PyObject *codecs, *ret, *decoder; + PyObject *codecs, *streamcodec; 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); + + streamcodec = PyEval_CallFunction( + PyTuple_GET_ITEM(codecs, index), "Os", stream, errors); Py_DECREF(codecs); - return ret; + return streamcodec; +} - onError: - return NULL; +/* Convenience APIs to query the Codec registry. + + All APIs return a codec object with incremented refcount. + + */ + +PyObject *PyCodec_Encoder(const char *encoding) +{ + return codec_getitem(encoding, 0); +} + +PyObject *PyCodec_Decoder(const char *encoding) +{ + return codec_getitem(encoding, 1); +} + +PyObject *PyCodec_IncrementalEncoder(const char *encoding, + const char *errors) +{ + return codec_getincrementalcodec(encoding, errors, "incrementalencoder"); +} + +PyObject *PyCodec_IncrementalDecoder(const char *encoding, + const char *errors) +{ + 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 |