Mercurial > cpython-lru_cache-weakref
annotate Lib/codecs.py @ 25135:0f0016632be4 legacy-trunk
_formatparam(), set_param(): RFC 2231 encoding support by Oleg
Broytmann in SF patch #600096. Specifically, the former function now
encodes the triplets, while the latter adds optional charset and
language arguments.
| author | Barry Warsaw <barry@python.org> |
|---|---|
| date | Fri, 06 Sep 2002 03:55:04 +0000 |
| parents | 0337dad8403e |
| children | f3a64035ba8b |
| rev | line source |
|---|---|
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """ codecs -- Python Codec Registry, API and helpers. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 Written by Marc-Andre Lemburg (mal@lemburg.com). |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 """#" |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 |
|
18936
7149173e8edc
Patch #444359: Remove unused imports.
Martin v. Löwis <martin@v.loewis.de>
parents:
18904
diff
changeset
|
10 import struct, __builtin__ |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 ### Registry and builtin stateless codec functions |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12051
diff
changeset
|
14 try: |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12051
diff
changeset
|
15 from _codecs import * |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
16 except ImportError, why: |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12051
diff
changeset
|
17 raise SystemError,\ |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12051
diff
changeset
|
18 'Failed to load the builtin codecs: %s' % why |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
20 __all__ = ["register", "lookup", "open", "EncodedFile", "BOM", "BOM_BE", |
|
23658
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
21 "BOM_LE", "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE", |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
22 "BOM_UTF8", "BOM_UTF16", "BOM_UTF16_LE", "BOM_UTF16_BE", |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
23 "BOM_UTF32", "BOM_UTF32_LE", "BOM_UTF32_BE", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
24 "strict_errors", "ignore_errors", "replace_errors", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
25 "xmlcharrefreplace_errors", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
26 "register_error", "lookup_error"] |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15867
diff
changeset
|
27 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 ### Constants |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 # |
|
23658
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
31 # Byte Order Mark (BOM = ZERO WIDTH NO-BREAK SPACE = U+FEFF) |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
32 # and its possible byte string values |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
33 # for UTF8/UTF16/UTF32 output and little/big endian machines |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 # |
|
23658
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
35 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
36 # UTF-8 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
37 BOM_UTF8 = '\xef\xbb\xbf' |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
38 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
39 # UTF-16, little endian |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
40 BOM_LE = BOM_UTF16_LE = '\xff\xfe' |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
41 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
42 # UTF-16, big endian |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
43 BOM_BE = BOM_UTF16_BE = '\xfe\xff' |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
44 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
45 # UTF-32, little endian |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
46 BOM_UTF32_LE = '\xff\xfe\x00\x00' |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 |
|
23658
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
48 # UTF-32, big endian |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
49 BOM_UTF32_BE = '\x00\x00\xfe\xff' |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
50 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
51 # UTF-16, native endianness |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
52 BOM = BOM_UTF16 = struct.pack('=H', 0xFEFF) |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
53 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
54 # UTF-32, native endianness |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
55 BOM_UTF32 = struct.pack('=L', 0x0000FEFF) |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
56 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
57 # Old broken names (don't use in new code) |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
58 BOM32_LE = BOM_UTF16_LE |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
59 BOM32_BE = BOM_UTF16_BE |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
60 BOM64_LE = BOM_UTF32_LE |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
61 BOM64_BE = BOM_UTF32_BE |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 ### Codec base classes (defining the API) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 class Codec: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 """ Defines the interface for stateless encoders/decoders. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 The .encode()/.decode() methods may implement different error |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 handling schemes by providing the errors argument. These |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 string values are defined: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
| 12051 | 74 'strict' - raise a ValueError error (or a subclass) |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 'ignore' - ignore the character and continue with the next |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 'replace' - replace with a suitable replacement character; |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 Python will use the official U+FFFD REPLACEMENT |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 CHARACTER for the builtin Unicode codecs. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 """ |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
81 def encode(self, input, errors='strict'): |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
82 |
|
12010
bd1014215f62
"intput" --> "input" (in docstring); noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents:
11990
diff
changeset
|
83 """ Encodes the object input and returns a tuple (output |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 object, length consumed). |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 errors defines the error handling to apply. It defaults to |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 'strict' handling. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 The method may not store state in the Codec instance. Use |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 StreamCodec for codecs which have to keep state in order to |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 make encoding/decoding efficient. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 The encoder must be able to handle zero length input and |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 return an empty object of the output object type in this |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 situation. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 raise NotImplementedError |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
100 def decode(self, input, errors='strict'): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 """ Decodes the object input and returns a tuple (output |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 object, length consumed). |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 input must be an object which provides the bf_getreadbuf |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 buffer slot. Python strings, buffer objects and memory |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 mapped files are examples of objects providing this slot. |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
108 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 errors defines the error handling to apply. It defaults to |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 'strict' handling. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 The method may not store state in the Codec instance. Use |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 StreamCodec for codecs which have to keep state in order to |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 make encoding/decoding efficient. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 The decoder must be able to handle zero length input and |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 return an empty object of the output object type in this |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 situation. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
120 """ |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 raise NotImplementedError |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 # |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 # The StreamWriter and StreamReader class provide generic working |
| 19967 | 125 # interfaces which can be used to implement new encoding submodules |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 # very easily. See encodings/utf_8.py for an example on how this is |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 # done. |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
128 # |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 class StreamWriter(Codec): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
132 def __init__(self, stream, errors='strict'): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 """ Creates a StreamWriter instance. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 stream must be a file-like object open for writing |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 (binary) data. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 The StreamWriter may implement different error handling |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 schemes by providing the errors keyword argument. These |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 parameters are defined: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 'strict' - raise a ValueError (or a subclass) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 'ignore' - ignore the character and continue with the next |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 'replace'- replace with a suitable replacement character |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 self.stream = stream |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 self.errors = errors |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 |
| 12367 | 151 def write(self, object): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 """ Writes the object's contents encoded to self.stream. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 """ |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
155 data, consumed = self.encode(object, self.errors) |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 self.stream.write(data) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 |
| 12367 | 158 def writelines(self, list): |
| 159 | |
| 160 """ Writes the concatenated list of strings to the stream | |
| 161 using .write(). | |
| 162 """ | |
| 163 self.write(''.join(list)) | |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
164 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 def reset(self): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 """ Flushes and resets the codec buffers used for keeping state. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 Calling this method should ensure that the data on the |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 output is put into a clean state, that allows appending |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 of new fresh data without having to rescan the whole |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 stream to recover state. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 pass |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
177 def __getattr__(self, name, |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 getattr=getattr): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 """ Inherit all other methods from the underlying stream. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 """ |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
182 return getattr(self.stream, name) |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 ### |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 class StreamReader(Codec): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
188 def __init__(self, stream, errors='strict'): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 """ Creates a StreamReader instance. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 stream must be a file-like object open for reading |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 (binary) data. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 The StreamReader may implement different error handling |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 schemes by providing the errors keyword argument. These |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 parameters are defined: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 'strict' - raise a ValueError (or a subclass) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 'ignore' - ignore the character and continue with the next |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 'replace'- replace with a suitable replacement character; |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 self.stream = stream |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 self.errors = errors |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 |
| 12367 | 207 def read(self, size=-1): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 """ Decodes data from the stream self.stream and returns the |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 resulting object. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 size indicates the approximate maximum number of bytes to |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 read from the stream for decoding purposes. The decoder |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 can modify this setting as appropriate. The default value |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 -1 indicates to read and decode as much as possible. size |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 is intended to prevent having to decode huge files in one |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 step. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 The method should use a greedy read strategy meaning that |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 it should read as much data as is allowed within the |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 definition of the encoding and the given size, e.g. if |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 optional encoding endings or state markers are available |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 on the stream, these should be read too. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 # Unsliced reading: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 if size < 0: |
|
15498
46275611cd59
(Patch #102698) Fix for a bug reported by Wade Leftwich:
Andrew M. Kuchling <amk@amk.ca>
parents:
14978
diff
changeset
|
228 return self.decode(self.stream.read(), self.errors)[0] |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
229 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 # Sliced reading: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 read = self.stream.read |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 decode = self.decode |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 data = read(size) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 i = 0 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 while 1: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 try: |
|
15498
46275611cd59
(Patch #102698) Fix for a bug reported by Wade Leftwich:
Andrew M. Kuchling <amk@amk.ca>
parents:
14978
diff
changeset
|
237 object, decodedbytes = decode(data, self.errors) |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
238 except ValueError, why: |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 # This method is slow but should work under pretty much |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 # all conditions; at most 10 tries are made |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 i = i + 1 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 newdata = read(1) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 if not newdata or i > 10: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 raise |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 data = data + newdata |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 else: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 return object |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 |
| 12367 | 249 def readline(self, size=None): |
| 250 | |
| 251 """ Read one line from the input stream and return the | |
| 252 decoded data. | |
| 253 | |
|
12389
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
254 Note: Unlike the .readlines() method, this method inherits |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
255 the line breaking knowledge from the underlying stream's |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
256 .readline() method -- there is currently no support for |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
257 line breaking using the codec decoder due to lack of line |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
258 buffering. Sublcasses should however, if possible, try to |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
259 implement this method using their own knowledge of line |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
260 breaking. |
| 12367 | 261 |
| 262 size, if given, is passed as size argument to the stream's | |
| 263 .readline() method. | |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
264 |
| 12367 | 265 """ |
| 266 if size is None: | |
| 267 line = self.stream.readline() | |
| 268 else: | |
| 269 line = self.stream.readline(size) | |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
270 return self.decode(line, self.errors)[0] |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
271 |
| 12367 | 272 |
|
22149
1e60b1e18145
backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents:
19981
diff
changeset
|
273 def readlines(self, sizehint=None): |
| 12367 | 274 |
| 275 """ Read all lines available on the input stream | |
| 276 and return them as list of lines. | |
| 277 | |
| 278 Line breaks are implemented using the codec's decoder | |
| 279 method and are included in the list entries. | |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
280 |
| 12367 | 281 sizehint, if given, is passed as size argument to the |
| 282 stream's .read() method. | |
| 283 | |
| 284 """ | |
| 285 if sizehint is None: | |
| 286 data = self.stream.read() | |
| 287 else: | |
| 288 data = self.stream.read(sizehint) | |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
289 return self.decode(data, self.errors)[0].splitlines(1) |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 def reset(self): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 """ Resets the codec buffers used for keeping state. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 Note that no stream repositioning should take place. |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
12986
diff
changeset
|
296 This method is primarily intended to be able to recover |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 from decoding errors. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 pass |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
302 def __getattr__(self, name, |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 getattr=getattr): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 """ Inherit all other methods from the underlying stream. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 """ |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
307 return getattr(self.stream, name) |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 ### |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 class StreamReaderWriter: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 |
|
12389
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
313 """ StreamReaderWriter instances allow wrapping streams which |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
314 work in both read and write modes. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
315 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
316 The design is such that one can use the factory functions |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
12986
diff
changeset
|
317 returned by the codec.lookup() function to construct the |
|
12389
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
318 instance. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
319 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
320 """ |
| 12367 | 321 # Optional attributes set by the file wrappers below |
| 322 encoding = 'unknown' | |
| 323 | |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
324 def __init__(self, stream, Reader, Writer, errors='strict'): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 """ Creates a StreamReaderWriter instance. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 stream must be a Stream-like object. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 Reader, Writer must be factory functions or classes |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 providing the StreamReader, StreamWriter interface resp. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 Error handling is done in the same way as defined for the |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 StreamWriter/Readers. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 self.stream = stream |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 self.reader = Reader(stream, errors) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 self.writer = Writer(stream, errors) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 self.errors = errors |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
342 def read(self, size=-1): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 return self.reader.read(size) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 |
| 12534 | 346 def readline(self, size=None): |
| 12367 | 347 |
| 348 return self.reader.readline(size) | |
| 349 | |
| 12534 | 350 def readlines(self, sizehint=None): |
| 12367 | 351 |
| 352 return self.reader.readlines(sizehint) | |
| 353 | |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
354 def write(self, data): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 return self.writer.write(data) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
358 def writelines(self, list): |
| 12367 | 359 |
| 360 return self.writer.writelines(list) | |
| 361 | |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 def reset(self): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 self.reader.reset() |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 self.writer.reset() |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
367 def __getattr__(self, name, |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 getattr=getattr): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 """ Inherit all other methods from the underlying stream. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 """ |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
372 return getattr(self.stream, name) |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 ### |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 class StreamRecoder: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 |
|
12389
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
378 """ StreamRecoder instances provide a frontend - backend |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
379 view of encoding data. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
380 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
381 They use the complete set of APIs returned by the |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
382 codecs.lookup() function to implement their task. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
383 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
384 Data written to the stream is first decoded into an |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
385 intermediate format (which is dependent on the given codec |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
386 combination) and then written to the stream using an instance |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
387 of the provided Writer class. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
388 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
389 In the other direction, data is read from the stream using a |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
390 Reader instance and then return encoded data to the caller. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
391 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
392 """ |
| 12367 | 393 # Optional attributes set by the file wrappers below |
| 394 data_encoding = 'unknown' | |
| 395 file_encoding = 'unknown' | |
| 396 | |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
397 def __init__(self, stream, encode, decode, Reader, Writer, |
|
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
398 errors='strict'): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
399 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
400 """ Creates a StreamRecoder instance which implements a two-way |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
401 conversion: encode and decode work on the frontend (the |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
402 input to .read() and output of .write()) while |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 Reader and Writer work on the backend (reading and |
|
12011
4fa25a292273
Oops, another in the same file; I should read the mail fully before
Fred Drake <fdrake@acm.org>
parents:
12010
diff
changeset
|
404 writing to the stream). |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
405 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
406 You can use these objects to do transparent direct |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 recodings from e.g. latin-1 to utf-8 and back. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
408 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
409 stream must be a file-like object. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 encode, decode must adhere to the Codec interface, Reader, |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 Writer must be factory functions or classes providing the |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 StreamReader, StreamWriter interface resp. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 encode and decode are needed for the frontend translation, |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 Reader and Writer for the backend translation. Unicode is |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 used as intermediate encoding. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
419 Error handling is done in the same way as defined for the |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
420 StreamWriter/Readers. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
421 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 self.stream = stream |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 self.encode = encode |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 self.decode = decode |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 self.reader = Reader(stream, errors) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 self.writer = Writer(stream, errors) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 self.errors = errors |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
429 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
430 def read(self, size=-1): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
431 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 data = self.reader.read(size) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
433 data, bytesencoded = self.encode(data, self.errors) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 return data |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
436 def readline(self, size=None): |
| 12367 | 437 |
| 438 if size is None: | |
| 439 data = self.reader.readline() | |
| 440 else: | |
| 441 data = self.reader.readline(size) | |
| 442 data, bytesencoded = self.encode(data, self.errors) | |
| 443 return data | |
| 444 | |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
445 def readlines(self, sizehint=None): |
| 12367 | 446 |
| 447 if sizehint is None: | |
| 448 data = self.reader.read() | |
| 449 else: | |
| 450 data = self.reader.read(sizehint) | |
| 451 data, bytesencoded = self.encode(data, self.errors) | |
| 452 return data.splitlines(1) | |
| 453 | |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
454 def write(self, data): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
455 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
456 data, bytesdecoded = self.decode(data, self.errors) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
457 return self.writer.write(data) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
459 def writelines(self, list): |
| 12367 | 460 |
| 461 data = ''.join(list) | |
| 462 data, bytesdecoded = self.decode(data, self.errors) | |
| 463 return self.writer.write(data) | |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
464 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
465 def reset(self): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
466 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
467 self.reader.reset() |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
468 self.writer.reset() |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
469 |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
470 def __getattr__(self, name, |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
471 getattr=getattr): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
472 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
473 """ Inherit all other methods from the underlying stream. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
474 """ |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
475 return getattr(self.stream, name) |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
476 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
477 ### Shortcuts |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
478 |
|
12986
5279a6e00d36
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12534
diff
changeset
|
479 def open(filename, mode='rb', encoding=None, errors='strict', buffering=1): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
480 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
481 """ Open an encoded file using the given mode and return |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
482 a wrapped version providing transparent encoding/decoding. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
483 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
484 Note: The wrapped version will only accept the object format |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
485 defined by the codecs, i.e. Unicode objects for most builtin |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
486 codecs. Output is also codec dependent and will usually by |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
487 Unicode as well. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
488 |
|
12986
5279a6e00d36
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12534
diff
changeset
|
489 Files are always opened in binary mode, even if no binary mode |
|
5279a6e00d36
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12534
diff
changeset
|
490 was specified. Thisis done to avoid data loss due to encodings |
|
5279a6e00d36
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12534
diff
changeset
|
491 using 8-bit values. The default file mode is 'rb' meaning to |
|
5279a6e00d36
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12534
diff
changeset
|
492 open the file in binary read mode. |
|
5279a6e00d36
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12534
diff
changeset
|
493 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
494 encoding specifies the encoding which is to be used for the |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
495 the file. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
496 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
497 errors may be given to define the error handling. It defaults |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
498 to 'strict' which causes ValueErrors to be raised in case an |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
499 encoding error occurs. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
500 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
501 buffering has the same meaning as for the builtin open() API. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
502 It defaults to line buffered. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
503 |
|
12389
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
504 The returned wrapped file object provides an extra attribute |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
505 .encoding which allows querying the used encoding. This |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
506 attribute is only available if an encoding was specified as |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
507 parameter. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
508 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
509 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
510 if encoding is not None and \ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
511 'b' not in mode: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
512 # Force opening of the file in binary mode |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
513 mode = mode + 'b' |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 file = __builtin__.open(filename, mode, buffering) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
515 if encoding is None: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
516 return file |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
517 (e, d, sr, sw) = lookup(encoding) |
| 12367 | 518 srw = StreamReaderWriter(file, sr, sw, errors) |
| 519 # Add attributes to simplify introspection | |
| 520 srw.encoding = encoding | |
| 521 return srw | |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
522 |
| 12367 | 523 def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
524 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 """ Return a wrapped version of file which provides transparent |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 encoding translation. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
528 Strings written to the wrapped file are interpreted according |
| 12367 | 529 to the given data_encoding and then written to the original |
| 530 file as string using file_encoding. The intermediate encoding | |
| 531 will usually be Unicode but depends on the specified codecs. | |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 |
| 12367 | 533 Strings are read from the file using file_encoding and then |
| 534 passed back to the caller as string using data_encoding. | |
| 535 | |
| 536 If file_encoding is not given, it defaults to data_encoding. | |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
537 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
538 errors may be given to define the error handling. It defaults |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 to 'strict' which causes ValueErrors to be raised in case an |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
540 encoding error occurs. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
541 |
|
12389
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
542 The returned wrapped file object provides two extra attributes |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
543 .data_encoding and .file_encoding which reflect the given |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
544 parameters of the same name. The attributes can be used for |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
545 introspection by Python programs. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
546 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
547 """ |
| 12367 | 548 if file_encoding is None: |
| 549 file_encoding = data_encoding | |
| 550 encode, decode = lookup(data_encoding)[:2] | |
| 551 Reader, Writer = lookup(file_encoding)[2:] | |
| 552 sr = StreamRecoder(file, | |
|
17839
c872fdf10130
Just changed "x,y" to "x, y" everywhere (i.e., inserted horizontal space
Tim Peters <tim.peters@gmail.com>
parents:
16070
diff
changeset
|
553 encode, decode, Reader, Writer, |
| 12367 | 554 errors) |
| 555 # Add attributes to simplify introspection | |
| 556 sr.data_encoding = data_encoding | |
| 557 sr.file_encoding = file_encoding | |
| 558 return sr | |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
559 |
|
19981
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
560 ### Helpers for codec lookup |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
561 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
562 def getencoder(encoding): |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
563 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
564 """ Lookup up the codec for the given encoding and return |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
565 its encoder function. |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
566 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
567 Raises a LookupError in case the encoding cannot be found. |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
568 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
569 """ |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
570 return lookup(encoding)[0] |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
571 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
572 def getdecoder(encoding): |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
573 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
574 """ Lookup up the codec for the given encoding and return |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
575 its decoder function. |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
576 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
577 Raises a LookupError in case the encoding cannot be found. |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
578 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
579 """ |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
580 return lookup(encoding)[1] |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
581 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
582 def getreader(encoding): |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
583 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
584 """ Lookup up the codec for the given encoding and return |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
585 its StreamReader class or factory function. |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
586 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
587 Raises a LookupError in case the encoding cannot be found. |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
588 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
589 """ |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
590 return lookup(encoding)[2] |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
591 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
592 def getwriter(encoding): |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
593 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
594 """ Lookup up the codec for the given encoding and return |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
595 its StreamWriter class or factory function. |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
596 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
597 Raises a LookupError in case the encoding cannot be found. |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
598 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
599 """ |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
600 return lookup(encoding)[3] |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
601 |
|
15706
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
602 ### Helpers for charmap-based codecs |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
603 |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
604 def make_identity_dict(rng): |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
605 |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
606 """ make_identity_dict(rng) -> dict |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
607 |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
608 Return a dictionary where elements of the rng sequence are |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
609 mapped to themselves. |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15706
diff
changeset
|
610 |
|
15706
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
611 """ |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
612 res = {} |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
613 for i in rng: |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
614 res[i]=i |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
615 return res |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
616 |
|
17843
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
617 def make_encoding_map(decoding_map): |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
618 |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
619 """ Creates an encoding map from a decoding map. |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
620 |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
621 If a target mapping in the decoding map occurrs multiple |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
622 times, then that target is mapped to None (undefined mapping), |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
623 causing an exception when encountered by the charmap codec |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
624 during translation. |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
625 |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
626 One example where this happens is cp875.py which decodes |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
627 multiple character to \u001a. |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
628 |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
629 """ |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
630 m = {} |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
631 for k,v in decoding_map.items(): |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22149
diff
changeset
|
632 if not v in m: |
|
17843
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
633 m[v] = k |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
634 else: |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
635 m[v] = None |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
636 return m |
|
17978
5e87a08abb05
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17843
diff
changeset
|
637 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
638 ### error handlers |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
639 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
640 strict_errors = lookup_error("strict") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
641 ignore_errors = lookup_error("ignore") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
642 replace_errors = lookup_error("replace") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
643 xmlcharrefreplace_errors = lookup_error("xmlcharrefreplace") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
644 backslashreplace_errors = lookup_error("backslashreplace") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
645 |
|
18904
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
646 # Tell modulefinder that using codecs probably needs the encodings |
|
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
647 # package |
|
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
648 _false = 0 |
|
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
649 if _false: |
|
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
650 import encodings |
|
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
651 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
652 ### Tests |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
653 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
654 if __name__ == '__main__': |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
655 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
656 import sys |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
657 |
| 12367 | 658 # Make stdout translate Latin-1 output into UTF-8 output |
| 659 sys.stdout = EncodedFile(sys.stdout, 'latin-1', 'utf-8') | |
|
12373
d63b57ac617d
Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents:
12367
diff
changeset
|
660 |
| 12367 | 661 # Have stdin translate Latin-1 input into UTF-8 input |
| 662 sys.stdin = EncodedFile(sys.stdin, 'utf-8', 'latin-1') |
