codecs.getencoder encodes entire string ?

N

nicolas_riesch

When I use an encoder function from codecs module, documentation says
that it encodes the object input and returns a tuple (output object,
length consumed).

I just don't understand why it returns the "length consumed".

Does it means that in some case, the input string can be only partially
converted ?

What can be the use of the "length consumed" value ?


And a last question: can I call this "enc" function from multiple
threads ?
 
J

Jeff Epler

And a last question: can I call this "enc" function from multiple
threads ?

Yes.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFC6QI2Jd01MZaTXX0RAonWAKCsU6HMqRCvpRN7xODVrBvwSsrDMACePUTU
ZED3yr8mF9Hk3kgGzvrBsic=
=M76o
-----END PGP SIGNATURE-----
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

nicolas_riesch said:
I just don't understand why it returns the "length consumed".

Does it means that in some case, the input string can be only partially
converted ?

For an encoder, I believe the answer is "no". For a decoder, it is
a definite yes: if the input does not end with a complete character,
you may have bytes left at the end which did not get decoded.

For an encoder, the same *might* happen if you want to encode
half-surrogates into, say, UTF-8; the encoder might refuse to
encode the half-surrogate, and wait for the other half. Of course,
the current UTF-8 encoder will then just encode the surrogate
codepoint as if it was a proper character.

If you extend the notion of "encoding", similar things may happen
all the time. E.g. a DES encoder may only support multiples of
the block size, and leave bytes at the end.
What can be the use of the "length consumed" value ?

It's primarily intended for stream writers, which may need
to buffer extra characters at the end that did not get encoded,
and wait until more input is provided.

For all practical purposes, you can ignore the length on
encoding. If you are paranoid, assert that it equals the
length of the input.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,981
Latest member
satome

Latest Threads

Top