Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libcodecs.tex @ 16413:8819867f14ef legacy-trunk
Patch derived from Trent's 101162: a Python/C API testing framework.
STILL NEEDS UNIX BUILD CHANGES.
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Fri, 02 Feb 2001 05:57:15 +0000 |
| parents | 6f1fd8500398 |
| children | bf4b1b08a2c6 |
| rev | line source |
|---|---|
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 \section{\module{codecs} --- |
|
12274
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
2 Codec registry and base classes} |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
3 |
|
12274
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
4 \declaremodule{standard}{codecs} |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 \modulesynopsis{Encode and decode data and streams.} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
6 \moduleauthor{Marc-Andre Lemburg}{mal@lemburg.com} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
7 \sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
8 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
10 \index{Unicode} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
11 \index{Codecs} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
12 \indexii{Codecs}{encode} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 \indexii{Codecs}{decode} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 \index{streams} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 \indexii{stackable}{streams} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
18 This module defines base classes for standard Python codecs (encoders |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 and decoders) and provides access to the internal Python codec |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 registry which manages the codec lookup process. |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
22 It defines the following functions: |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
23 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
24 \begin{funcdesc}{register}{search_function} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
25 Register a codec search function. Search functions are expected to |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
26 take one argument, the encoding name in all lower case letters, and |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 return a tuple of functions \code{(\var{encoder}, \var{decoder}, \var{stream_reader}, |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
28 \var{stream_writer})} taking the following arguments: |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
29 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 \var{encoder} and \var{decoder}: These must be functions or methods |
|
15186
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
31 which have the same interface as the |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
32 \method{encode()}/\method{decode()} methods of Codec instances (see |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
33 Codec Interface). The functions/methods are expected to work in a |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
34 stateless mode. |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
35 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
36 \var{stream_reader} and \var{stream_writer}: These have to be |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
37 factory functions providing the following interface: |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
38 |
|
15186
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
39 \code{factory(\var{stream}, \var{errors}='strict')} |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
40 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
41 The factory functions must return objects providing the interfaces |
|
12274
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
42 defined by the base classes \class{StreamWriter} and |
|
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
43 \class{StreamReader}, respectively. Stream codecs can maintain |
|
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
44 state. |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
45 |
|
12274
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
46 Possible values for errors are \code{'strict'} (raise an exception |
|
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
47 in case of an encoding error), \code{'replace'} (replace malformed |
|
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
48 data with a suitable replacement marker, such as \character{?}) and |
|
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
49 \code{'ignore'} (ignore malformed data and continue without further |
|
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
50 notice). |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
52 In case a search function cannot find a given encoding, it should |
|
12274
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
53 return \code{None}. |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
54 \end{funcdesc} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
55 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
56 \begin{funcdesc}{lookup}{encoding} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 Looks up a codec tuple in the Python codec registry and returns the |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
58 function tuple as defined above. |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
59 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
60 Encodings are first looked up in the registry's cache. If not found, |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
61 the list of registered search functions is scanned. If no codecs tuple |
|
12274
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
62 is found, a \exception{LookupError} is raised. Otherwise, the codecs |
|
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
63 tuple is stored in the cache and returned to the caller. |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 \end{funcdesc} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
66 To simplify working with encoded files or stream, the module |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 also defines these utility functions: |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
68 |
|
13696
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
69 \begin{funcdesc}{open}{filename, mode\optional{, encoding\optional{, |
|
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
70 errors\optional{, buffering}}}} |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
71 Open an encoded file using the given \var{mode} and return |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
72 a wrapped version providing transparent encoding/decoding. |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
73 |
|
12274
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
74 \strong{Note:} The wrapped version will only accept the object format |
|
13696
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
75 defined by the codecs, i.e.\ Unicode objects for most built-in |
|
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
76 codecs. Output is also codec-dependent and will usually be Unicode as |
|
12274
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
77 well. |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
78 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
79 \var{encoding} specifies the encoding which is to be used for the |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
80 the file. |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
81 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 \var{errors} may be given to define the error handling. It defaults |
|
13696
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
83 to \code{'strict'} which causes a \exception{ValueError} to be raised |
|
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
84 in case an encoding error occurs. |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 |
|
12274
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
86 \var{buffering} has the same meaning as for the built-in |
|
559b25a79032
Make sure the \declaremodule uses the right name for the module!
Fred Drake <fdrake@acm.org>
parents:
12260
diff
changeset
|
87 \function{open()} function. It defaults to line buffered. |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
88 \end{funcdesc} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
89 |
|
13696
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
90 \begin{funcdesc}{EncodedFile}{file, input\optional{, |
|
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
91 output\optional{, errors}}} |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
92 Return a wrapped version of file which provides transparent |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
93 encoding translation. |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
94 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
95 Strings written to the wrapped file are interpreted according to the |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
96 given \var{input} encoding and then written to the original file as |
|
13696
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
97 strings using the \var{output} encoding. The intermediate encoding will |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
98 usually be Unicode but depends on the specified codecs. |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
99 |
|
13696
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
100 If \var{output} is not given, it defaults to \var{input}. |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
101 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
102 \var{errors} may be given to define the error handling. It defaults to |
|
13696
bb364d6f9f69
Fix small typos and markup consistency nits.
Fred Drake <fdrake@acm.org>
parents:
12274
diff
changeset
|
103 \code{'strict'}, which causes \exception{ValueError} to be raised in case |
|
12260
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
104 an encoding error occurs. |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
105 \end{funcdesc} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
106 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
107 The module also provides the following constants which are useful |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
108 for reading and writing to platform dependent files: |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
109 |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
110 \begin{datadesc}{BOM} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
111 \dataline{BOM_BE} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
112 \dataline{BOM_LE} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
113 \dataline{BOM32_BE} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
114 \dataline{BOM32_LE} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
115 \dataline{BOM64_BE} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
116 \dataline{BOM64_LE} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 These constants define the byte order marks (BOM) used in data |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
118 streams to indicate the byte order used in the stream or file. |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
119 \constant{BOM} is either \constant{BOM_BE} or \constant{BOM_LE} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
120 depending on the platform's native byte order, while the others |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
121 represent big endian (\samp{_BE} suffix) and little endian |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
122 (\samp{_LE} suffix) byte order using 32-bit and 64-bit encodings. |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
123 \end{datadesc} |
|
8c0f863ba50c
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
124 |
|
16127
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
125 |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
126 \begin{seealso} |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
127 \seeurl{http://sourceforge.net/projects/python-codecs/}{A |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
128 SourceForge project working on additional support for Asian |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
129 codecs for use with Python. They are in the early stages of |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
130 development at the time of this writing --- look in their |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
131 FTP area for downloadable files.} |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
132 \end{seealso} |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
133 |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
134 |
|
15186
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
135 \subsection{Codec Base Classes} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
136 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
137 The \module{codecs} defines a set of base classes which define the |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
138 interface and can also be used to easily write you own codecs for use |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
139 in Python. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
140 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
141 Each codec has to define four interfaces to make it usable as codec in |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
142 Python: stateless encoder, stateless decoder, stream reader and stream |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
143 writer. The stream reader and writers typically reuse the stateless |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
144 encoder/decoder to implement the file protocols. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
145 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
146 The \class{Codec} class defines the interface for stateless |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
147 encoders/decoders. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
148 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
149 To simplify and standardize error handling, the \method{encode()} and |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
150 \method{decode()} methods may implement different error handling |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
151 schemes by providing the \var{errors} string argument. The following |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
152 string values are defined and implemented by all standard Python |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
153 codecs: |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
154 |
|
16127
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
155 \begin{tableii}{l|l}{code}{Value}{Meaning} |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
156 \lineii{'strict'}{Raise \exception{ValueError} (or a subclass); |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
157 this is the default.} |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
158 \lineii{'ignore'}{Ignore the character and continue with the next.} |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
159 \lineii{'replace'}{Replace with a suitable replacement character; |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
160 Python will use the official U+FFFD REPLACEMENT |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
161 CHARACTER for the built-in Unicode codecs.} |
|
6f1fd8500398
Added link to the "Python Codecs" project at SourceForge.
Fred Drake <fdrake@acm.org>
parents:
15186
diff
changeset
|
162 \end{tableii} |
|
15186
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
163 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
164 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
165 \subsubsection{Codec Objects \label{codec-objects}} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
166 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
167 The \class{Codec} class defines these methods which also define the |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
168 function interfaces of the stateless encoder and decoder: |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
169 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
170 \begin{methoddesc}{encode}{input\optional{, errors}} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
171 Encodes the object \var{input} and returns a tuple (output object, |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
172 length consumed). |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
173 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
174 \var{errors} defines the error handling to apply. It defaults to |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
175 \code{'strict'} handling. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
176 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
177 The method may not store state in the \class{Codec} instance. Use |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
178 \class{StreamCodec} for codecs which have to keep state in order to |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
179 make encoding/decoding efficient. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
180 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
181 The encoder must be able to handle zero length input and return an |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
182 empty object of the output object type in this situation. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
183 \end{methoddesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
184 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
185 \begin{methoddesc}{decode}{input\optional{, errors}} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
186 Decodes the object \var{input} and returns a tuple (output object, |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
187 length consumed). |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
188 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
189 \var{input} must be an object which provides the \code{bf_getreadbuf} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
190 buffer slot. Python strings, buffer objects and memory mapped files |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
191 are examples of objects providing this slot. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
192 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
193 \var{errors} defines the error handling to apply. It defaults to |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
194 \code{'strict'} handling. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
195 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
196 The method may not store state in the \class{Codec} instance. Use |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
197 \class{StreamCodec} for codecs which have to keep state in order to |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
198 make encoding/decoding efficient. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
199 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
200 The decoder must be able to handle zero length input and return an |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
201 empty object of the output object type in this situation. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
202 \end{methoddesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
203 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
204 The \class{StreamWriter} and \class{StreamReader} classes provide |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
205 generic working interfaces which can be used to implement new |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
206 encodings submodules very easily. See \module{encodings.utf_8} for an |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
207 example on how this is done. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
208 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
209 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
210 \subsubsection{StreamWriter Objects \label{stream-writer-objects}} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
211 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
212 The \class{StreamWriter} class is a subclass of \class{Codec} and |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
213 defines the following methods which every stream writer must define in |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
214 order to be compatible to the Python codec registry. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
215 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
216 \begin{classdesc}{StreamWriter}{stream\optional{, errors}} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
217 Constructor for a \class{StreamWriter} instance. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
218 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
219 All stream writers must provide this constructor interface. They are |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
220 free to add additional keyword arguments, but only the ones defined |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
221 here are used by the Python codec registry. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
222 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
223 \var{stream} must be a file-like object open for writing (binary) |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
224 data. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
225 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
226 The \class{StreamWriter} may implement different error handling |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
227 schemes by providing the \var{errors} keyword argument. These |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
228 parameters are defined: |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
229 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
230 \begin{itemize} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
231 \item \code{'strict'} Raise \exception{ValueError} (or a subclass); |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
232 this is the default. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
233 \item \code{'ignore'} Ignore the character and continue with the next. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
234 \item \code{'replace'} Replace with a suitable replacement character |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
235 \end{itemize} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
236 \end{classdesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
237 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
238 \begin{methoddesc}{write}{object} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
239 Writes the object's contents encoded to the stream. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
240 \end{methoddesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
241 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
242 \begin{methoddesc}{writelines}{list} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
243 Writes the concatenated list of strings to the stream (possibly by |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
244 reusing the \method{write()} method). |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
245 \end{methoddesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
246 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
247 \begin{methoddesc}{reset}{} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
248 Flushes and resets the codec buffers used for keeping state. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
249 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
250 Calling this method should ensure that the data on the output is put |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
251 into a clean state, that allows appending of new fresh data without |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
252 having to rescan the whole stream to recover state. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
253 \end{methoddesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
254 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
255 In addition to the above methods, the \class{StreamWriter} must also |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
256 inherit all other methods and attribute from the underlying stream. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
257 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
258 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
259 \subsubsection{StreamReader Objects \label{stream-reader-objects}} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
260 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
261 The \class{StreamReader} class is a subclass of \class{Codec} and |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
262 defines the following methods which every stream reader must define in |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
263 order to be compatible to the Python codec registry. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
264 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
265 \begin{classdesc}{StreamReader}{stream\optional{, errors}} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
266 Constructor for a \class{StreamReader} instance. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
267 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
268 All stream readers must provide this constructor interface. They are |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
269 free to add additional keyword arguments, but only the ones defined |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
270 here are used by the Python codec registry. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
271 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
272 \var{stream} must be a file-like object open for reading (binary) |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
273 data. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
274 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
275 The \class{StreamReader} may implement different error handling |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
276 schemes by providing the \var{errors} keyword argument. These |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
277 parameters are defined: |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
278 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
279 \begin{itemize} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
280 \item \code{'strict'} Raise \exception{ValueError} (or a subclass); |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
281 this is the default. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
282 \item \code{'ignore'} Ignore the character and continue with the next. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
283 \item \code{'replace'} Replace with a suitable replacement character. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
284 \end{itemize} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
285 \end{classdesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
286 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
287 \begin{methoddesc}{read}{\optional{size}} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
288 Decodes data from the stream and returns the resulting object. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
289 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
290 \var{size} indicates the approximate maximum number of bytes to read |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
291 from the stream for decoding purposes. The decoder can modify this |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
292 setting as appropriate. The default value -1 indicates to read and |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
293 decode as much as possible. \var{size} is intended to prevent having |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
294 to decode huge files in one step. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
295 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
296 The method should use a greedy read strategy meaning that it should |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
297 read as much data as is allowed within the definition of the encoding |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
298 and the given size, e.g. if optional encoding endings or state |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
299 markers are available on the stream, these should be read too. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
300 \end{methoddesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
301 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
302 \begin{methoddesc}{readline}{[size]} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
303 Read one line from the input stream and return the |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
304 decoded data. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
305 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
306 Note: Unlike the \method{readlines()} method, this method inherits |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
307 the line breaking knowledge from the underlying stream's |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
308 \method{readline()} method -- there is currently no support for line |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
309 breaking using the codec decoder due to lack of line buffering. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
310 Sublcasses should however, if possible, try to implement this method |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
311 using their own knowledge of line breaking. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
312 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
313 \var{size}, if given, is passed as size argument to the stream's |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
314 \method{readline()} method. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
315 \end{methoddesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
316 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
317 \begin{methoddesc}{readlines}{[sizehint]} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
318 Read all lines available on the input stream and return them as list |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
319 of lines. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
320 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
321 Line breaks are implemented using the codec's decoder method and are |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
322 included in the list entries. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
323 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
324 \var{sizehint}, if given, is passed as \var{size} argument to the |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
325 stream's \method{read()} method. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
326 \end{methoddesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
327 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
328 \begin{methoddesc}{reset}{} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
329 Resets the codec buffers used for keeping state. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
330 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
331 Note that no stream repositioning should take place. This method is |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
332 primarily intended to be able to recover from decoding errors. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
333 \end{methoddesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
334 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
335 In addition to the above methods, the \class{StreamReader} must also |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
336 inherit all other methods and attribute from the underlying stream. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
337 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
338 The next two base classes are included for convenience. They are not |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
339 needed by the codec registry, but may provide useful in practice. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
340 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
341 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
342 \subsubsection{StreamReaderWriter Objects \label{stream-reader-writer}} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
343 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
344 The \class{StreamReaderWriter} allows wrapping streams which work in |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
345 both read and write modes. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
346 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
347 The design is such that one can use the factory functions returned by |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
348 the \function{lookup()} function to construct the instance. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
349 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
350 \begin{classdesc}{StreamReaderWriter}{stream, Reader, Writer, errors} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
351 Creates a \class{StreamReaderWriter} instance. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
352 \var{stream} must be a file-like object. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
353 \var{Reader} and \var{Writer} must be factory functions or classes |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
354 providing the \class{StreamReader} and \class{StreamWriter} interface |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
355 resp. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
356 Error handling is done in the same way as defined for the |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
357 stream readers and writers. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
358 \end{classdesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
359 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
360 \class{StreamReaderWriter} instances define the combined interfaces of |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
361 \class{StreamReader} and \class{StreamWriter} classes. They inherit |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
362 all other methods and attribute from the underlying stream. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
363 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
364 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
365 \subsubsection{StreamRecoder Objects \label{stream-recoder-objects}} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
366 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
367 The \class{StreamRecoder} provide a frontend - backend view of |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
368 encoding data which is sometimes useful when dealing with different |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
369 encoding environments. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
370 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
371 The design is such that one can use the factory functions returned by |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
372 the \function{lookup()} function to construct the instance. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
373 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
374 \begin{classdesc}{StreamRecoder}{stream, encode, decode, |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
375 Reader, Writer, errors} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
376 Creates a \class{StreamRecoder} instance which implements a two-way |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
377 conversion: \var{encode} and \var{decode} work on the frontend (the |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
378 input to \method{read()} and output of \method{write()}) while |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
379 \var{Reader} and \var{Writer} work on the backend (reading and |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
380 writing to the stream). |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
381 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
382 You can use these objects to do transparent direct recodings from |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
383 e.g.\ Latin-1 to UTF-8 and back. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
384 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
385 \var{stream} must be a file-like object. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
386 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
387 \var{encode}, \var{decode} must adhere to the \class{Codec} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
388 interface, \var{Reader}, \var{Writer} must be factory functions or |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
389 classes providing objects of the the \class{StreamReader} and |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
390 \class{StreamWriter} interface respectively. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
391 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
392 \var{encode} and \var{decode} are needed for the frontend |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
393 translation, \var{Reader} and \var{Writer} for the backend |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
394 translation. The intermediate format used is determined by the two |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
395 sets of codecs, e.g. the Unicode codecs will use Unicode as |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
396 intermediate encoding. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
397 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
398 Error handling is done in the same way as defined for the |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
399 stream readers and writers. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
400 \end{classdesc} |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
401 |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
402 \class{StreamRecoder} instances define the combined interfaces of |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
403 \class{StreamReader} and \class{StreamWriter} classes. They inherit |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
404 all other methods and attribute from the underlying stream. |
|
b2892cbc63db
Marc-Andre Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
13696
diff
changeset
|
405 |
