Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="stream-writer-objects.html" /> | |
13 | <link rel="prev" href="node130.html" /> | |
14 | <link rel="parent" href="node130.html" /> | |
15 | <link rel="next" href="stream-writer-objects.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>4.9.1.1 Codec Objects </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="4.9.1 Codec Base Classes" | |
25 | href="node130.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="4.9.1 Codec Base Classes" | |
28 | href="node130.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="4.9.1.2 StreamWriter Objects" | |
31 | href="stream-writer-objects.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="node130.html">4.9.1 Codec Base Classes</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="node130.html">4.9.1 Codec Base Classes</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="stream-writer-objects.html">4.9.1.2 StreamWriter Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION006911000000000000000"></A><A NAME="codec-objects"></A> | |
56 | <BR> | |
57 | 4.9.1.1 Codec Objects | |
58 | </H3> | |
59 | ||
60 | <P> | |
61 | The <tt class="class">Codec</tt> class defines these methods which also define the | |
62 | function interfaces of the stateless encoder and decoder: | |
63 | ||
64 | <P> | |
65 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
66 | <td><nobr><b><tt id='l2h-1005' xml:id='l2h-1005' class="method">encode</tt></b>(</nobr></td> | |
67 | <td><var>input</var><big>[</big><var>, errors</var><big>]</big><var></var>)</td></tr></table></dt> | |
68 | <dd> | |
69 | Encodes the object <var>input</var> and returns a tuple (output object, | |
70 | length consumed). While codecs are not restricted to use with Unicode, in | |
71 | a Unicode context, encoding converts a Unicode object to a plain string | |
72 | using a particular character set encoding (e.g., <code>cp1252</code> or | |
73 | <code>iso-8859-1</code>). | |
74 | ||
75 | <P> | |
76 | <var>errors</var> defines the error handling to apply. It defaults to | |
77 | <code>'strict'</code> handling. | |
78 | ||
79 | <P> | |
80 | The method may not store state in the <tt class="class">Codec</tt> instance. Use | |
81 | <tt class="class">StreamCodec</tt> for codecs which have to keep state in order to | |
82 | make encoding/decoding efficient. | |
83 | ||
84 | <P> | |
85 | The encoder must be able to handle zero length input and return an | |
86 | empty object of the output object type in this situation. | |
87 | </dl> | |
88 | ||
89 | <P> | |
90 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
91 | <td><nobr><b><tt id='l2h-1006' xml:id='l2h-1006' class="method">decode</tt></b>(</nobr></td> | |
92 | <td><var>input</var><big>[</big><var>, errors</var><big>]</big><var></var>)</td></tr></table></dt> | |
93 | <dd> | |
94 | Decodes the object <var>input</var> and returns a tuple (output object, | |
95 | length consumed). In a Unicode context, decoding converts a plain string | |
96 | encoded using a particular character set encoding to a Unicode object. | |
97 | ||
98 | <P> | |
99 | <var>input</var> must be an object which provides the <code>bf_getreadbuf</code> | |
100 | buffer slot. Python strings, buffer objects and memory mapped files | |
101 | are examples of objects providing this slot. | |
102 | ||
103 | <P> | |
104 | <var>errors</var> defines the error handling to apply. It defaults to | |
105 | <code>'strict'</code> handling. | |
106 | ||
107 | <P> | |
108 | The method may not store state in the <tt class="class">Codec</tt> instance. Use | |
109 | <tt class="class">StreamCodec</tt> for codecs which have to keep state in order to | |
110 | make encoding/decoding efficient. | |
111 | ||
112 | <P> | |
113 | The decoder must be able to handle zero length input and return an | |
114 | empty object of the output object type in this situation. | |
115 | </dl> | |
116 | ||
117 | <P> | |
118 | The <tt class="class">StreamWriter</tt> and <tt class="class">StreamReader</tt> classes provide | |
119 | generic working interfaces which can be used to implement new | |
120 | encodings submodules very easily. See <tt class="module">encodings.utf_8</tt> for an | |
121 | example on how this is done. | |
122 | ||
123 | <P> | |
124 | ||
125 | <DIV CLASS="navigation"> | |
126 | <div class='online-navigation'> | |
127 | <p></p><hr /> | |
128 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
129 | <tr> | |
130 | <td class='online-navigation'><a rel="prev" title="4.9.1 Codec Base Classes" | |
131 | href="node130.html"><img src='../icons/previous.png' | |
132 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
133 | <td class='online-navigation'><a rel="parent" title="4.9.1 Codec Base Classes" | |
134 | href="node130.html"><img src='../icons/up.png' | |
135 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
136 | <td class='online-navigation'><a rel="next" title="4.9.1.2 StreamWriter Objects" | |
137 | href="stream-writer-objects.html"><img src='../icons/next.png' | |
138 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
139 | <td align="center" width="100%">Python Library Reference</td> | |
140 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
141 | href="contents.html"><img src='../icons/contents.png' | |
142 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
143 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
144 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
145 | <td class='online-navigation'><a rel="index" title="Index" | |
146 | href="genindex.html"><img src='../icons/index.png' | |
147 | border='0' height='32' alt='Index' width='32' /></A></td> | |
148 | </tr></table> | |
149 | <div class='online-navigation'> | |
150 | <b class="navlabel">Previous:</b> | |
151 | <a class="sectref" rel="prev" href="node130.html">4.9.1 Codec Base Classes</A> | |
152 | <b class="navlabel">Up:</b> | |
153 | <a class="sectref" rel="parent" href="node130.html">4.9.1 Codec Base Classes</A> | |
154 | <b class="navlabel">Next:</b> | |
155 | <a class="sectref" rel="next" href="stream-writer-objects.html">4.9.1.2 StreamWriter Objects</A> | |
156 | </div> | |
157 | </div> | |
158 | <hr /> | |
159 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
160 | </DIV> | |
161 | <!--End of Navigation Panel--> | |
162 | <ADDRESS> | |
163 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
164 | </ADDRESS> | |
165 | </BODY> | |
166 | </HTML> |