[Python-checkins] r43045 - in python/trunk: Doc/lib/libcodecs.tex Include/codecs.h Lib/codecs.py Lib/encodings/__init__.py Lib/encodings/ascii.py Lib/encodings/base64_codec.py Lib/encodings/bz2_codec.py Lib/encodings/charmap.py Lib/encodings/cp03

Neal Norwitz nnorwitz at gmail.com
Thu Mar 16 08:39:35 CET 2006


On 3/15/06, walter.doerwald <python-checkins at python.org> wrote:
> Author: walter.doerwald
> Date: Wed Mar 15 12:35:15 2006
> New Revision: 43045
>
> Modified:
>    python/trunk/Lib/test/test_codecs.py
>    python/trunk/Python/codecs.c
>
> Log:
> Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
> of tuple) that provides incremental decoders and encoders (a way to use
> stateful codecs without the stream API). Functions
> codecs.getincrementaldecoder() and codecs.getincrementalencoder() have
> been added.

Walter,

In Python/codecs.c, I have a couple of questions.  The code is at the
bottom of the message.

1) PyObject_CallFunction(encoder, "O", errors); doesn't look correct. 
errors is a const char *, not a PyObject*.  Shouldn't the "O" be "s"? 
If it should, that probably means there are no tests with errors set,
can you add tests for this condition?

2) It looks like this code is duplicated except for the string passed
to PyObject_GetAttrString().  Can you write a helper function and pass
in the string?  That would cut the code in half.

3) Is the onError label necessary?  It looks like it's only used in
one place and the code might be easier to follow if you changed the
goto to a return NULL;

n
--
+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;
+}
+
+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;
+}


More information about the Python-checkins mailing list