Mercurial > cpython-lru_cache-weakref
annotate Lib/codecs.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | bf82c1d04e06 |
| children |
| 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 |
|
26616
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
10 import __builtin__, sys |
|
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: |
|
37080
6771f0e08b60
Change raise statement to PEP 8 style.
Walter Dörwald <walter@livinglogic.de>
parents:
37024
diff
changeset
|
17 raise SystemError('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
|
18 |
|
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
|
19 __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
|
20 "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
|
21 "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
|
22 "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
|
23 "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
|
24 "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
|
25 "register_error", "lookup_error"] |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15867
diff
changeset
|
26 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 ### Constants |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 # |
|
23658
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
30 # 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
|
31 # 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
|
32 # 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
|
33 # |
|
23658
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
34 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
35 # 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
|
36 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
|
37 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
38 # 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
|
39 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
|
40 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
41 # 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
|
42 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
|
43 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
44 # 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
|
45 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
|
46 |
|
23658
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
47 # 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
|
48 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
|
49 |
|
26616
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
50 if sys.byteorder == 'little': |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
51 |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
52 # UTF-16, native endianness |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
53 BOM = BOM_UTF16 = BOM_UTF16_LE |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
54 |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
55 # UTF-32, native endianness |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
56 BOM_UTF32 = BOM_UTF32_LE |
|
23658
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
57 |
|
26616
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
58 else: |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
59 |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
60 # UTF-16, native endianness |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
61 BOM = BOM_UTF16 = BOM_UTF16_BE |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
62 |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
63 # UTF-32, native endianness |
|
20e0755f3554
Patch to make _codecs a builtin module. This is necessary since
Marc-André Lemburg <mal@egenix.com>
parents:
26293
diff
changeset
|
64 BOM_UTF32 = BOM_UTF32_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
|
65 |
|
94aa4eb9bb7d
Add constants BOM_UTF8, BOM_UTF16, BOM_UTF16_LE, BOM_UTF16_BE,
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
66 # 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 ### 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
|
74 |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
75 class CodecInfo(tuple): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
76 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
77 def __new__(cls, encode, decode, streamreader=None, streamwriter=None, |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
78 incrementalencoder=None, incrementaldecoder=None, name=None): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
79 self = tuple.__new__(cls, (encode, decode, streamreader, streamwriter)) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
80 self.name = name |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
81 self.encode = encode |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
82 self.decode = decode |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
83 self.incrementalencoder = incrementalencoder |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
84 self.incrementaldecoder = incrementaldecoder |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
85 self.streamwriter = streamwriter |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
86 self.streamreader = streamreader |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
87 return self |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
88 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
89 def __repr__(self): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
90 return "<%s.%s object for encoding %s at 0x%x>" % (self.__class__.__module__, self.__class__.__name__, self.name, id(self)) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
91 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 class Codec: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 """ 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
|
95 |
|
26293
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
96 The .encode()/.decode() methods may use different error |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 handling schemes by providing the errors argument. These |
|
26293
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
98 string values are predefined: |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 |
| 12051 | 100 '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
|
101 '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
|
102 '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
|
103 Python will use the official U+FFFD REPLACEMENT |
|
26293
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
104 CHARACTER for the builtin Unicode codecs on |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
105 decoding and '?' on encoding. |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
106 'xmlcharrefreplace' - Replace with the appropriate XML |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
107 character reference (only for encoding). |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
108 'backslashreplace' - Replace with backslashed escape sequences |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
109 (only for encoding). |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
110 |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
111 The set of allowed values can be extended via register_error. |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 """ |
|
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
|
114 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
|
115 |
|
12010
bd1014215f62
"intput" --> "input" (in docstring); noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents:
11990
diff
changeset
|
116 """ 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
|
117 object, length consumed). |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 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
|
120 'strict' handling. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 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
|
123 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
|
124 make encoding/decoding efficient. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 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
|
127 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
|
128 situation. |
|
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 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 raise NotImplementedError |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 |
|
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
|
133 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
|
134 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 """ 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
|
136 object, length consumed). |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 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
|
139 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
|
140 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
|
141 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 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
|
143 'strict' handling. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 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
|
146 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
|
147 make encoding/decoding efficient. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 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
|
150 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
|
151 situation. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 |
|
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
|
153 """ |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 raise NotImplementedError |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
156 class IncrementalEncoder(object): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
157 """ |
|
37024
f70c036489ec
Add some versionadded info to new incremental codec docs and fix doco nits.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36996
diff
changeset
|
158 An IncrementalEncoder encodes an input in multiple steps. The input can be |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
159 passed piece by piece to the encode() method. The IncrementalEncoder remembers |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
160 the state of the Encoding process between calls to encode(). |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
161 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
162 def __init__(self, errors='strict'): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
163 """ |
|
37024
f70c036489ec
Add some versionadded info to new incremental codec docs and fix doco nits.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36996
diff
changeset
|
164 Creates an IncrementalEncoder instance. |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
165 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
166 The IncrementalEncoder may use different error handling schemes by |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
167 providing the errors keyword argument. See the module docstring |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
168 for a list of possible values. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
169 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
170 self.errors = errors |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
171 self.buffer = "" |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
172 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
173 def encode(self, input, final=False): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
174 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
175 Encodes input and returns the resulting object. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
176 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
177 raise NotImplementedError |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
178 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
179 def reset(self): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
180 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
181 Resets the encoder to the initial state. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
182 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
183 |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
184 def getstate(self): |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
185 """ |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
186 Return the current state of the encoder. |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
187 """ |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
188 return 0 |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
189 |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
190 def setstate(self, state): |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
191 """ |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
192 Set the current state of the encoder. state must have been |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
193 returned by getstate(). |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
194 """ |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
195 |
|
37719
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
196 class BufferedIncrementalEncoder(IncrementalEncoder): |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
197 """ |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
198 This subclass of IncrementalEncoder can be used as the baseclass for an |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
199 incremental encoder if the encoder must keep some of the output in a |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
200 buffer between calls to encode(). |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
201 """ |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
202 def __init__(self, errors='strict'): |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
203 IncrementalEncoder.__init__(self, errors) |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
204 self.buffer = "" # unencoded input that is kept between calls to encode() |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
205 |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
206 def _buffer_encode(self, input, errors, final): |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
207 # Overwrite this method in subclasses: It must encode input |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
208 # and return an (output, length consumed) tuple |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
209 raise NotImplementedError |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
210 |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
211 def encode(self, input, final=False): |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
212 # encode input (taking the buffer into account) |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
213 data = self.buffer + input |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
214 (result, consumed) = self._buffer_encode(data, self.errors, final) |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
215 # keep unencoded input until the next call |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
216 self.buffer = data[consumed:] |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
217 return result |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
218 |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
219 def reset(self): |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
220 IncrementalEncoder.reset(self) |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
221 self.buffer = "" |
|
cf71c3814b0b
Add a BufferedIncrementalEncoder class that can be used for implementing
Walter Dörwald <walter@livinglogic.de>
parents:
37716
diff
changeset
|
222 |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
223 def getstate(self): |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
224 return self.buffer or 0 |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
225 |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
226 def setstate(self, state): |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
227 self.buffer = state or "" |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
228 |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
229 class IncrementalDecoder(object): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
230 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
231 An IncrementalDecoder decodes an input in multiple steps. The input can be |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
232 passed piece by piece to the decode() method. The IncrementalDecoder |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
233 remembers the state of the decoding process between calls to decode(). |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
234 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
235 def __init__(self, errors='strict'): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
236 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
237 Creates a IncrementalDecoder instance. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
238 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
239 The IncrementalDecoder may use different error handling schemes by |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
240 providing the errors keyword argument. See the module docstring |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
241 for a list of possible values. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
242 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
243 self.errors = errors |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
244 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
245 def decode(self, input, final=False): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
246 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
247 Decodes input and returns the resulting object. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
248 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
249 raise NotImplementedError |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
250 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
251 def reset(self): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
252 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
253 Resets the decoder to the initial state. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
254 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
255 |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
256 def getstate(self): |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
257 """ |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
258 Return the current state of the decoder. |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
259 |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
260 This must be a (buffered_input, additional_state_info) tuple. |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
261 buffered_input must be a bytes object containing bytes that |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
262 were passed to decode() that have not yet been converted. |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
263 additional_state_info must be a non-negative integer |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
264 representing the state of the decoder WITHOUT yet having |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
265 processed the contents of buffered_input. In the initial state |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
266 and after reset(), getstate() must return (b"", 0). |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
267 """ |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
268 return (b"", 0) |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
269 |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
270 def setstate(self, state): |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
271 """ |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
272 Set the current state of the decoder. |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
273 |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
274 state must have been returned by getstate(). The effect of |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
275 setstate((b"", 0)) must be equivalent to reset(). |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
276 """ |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
277 |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
278 class BufferedIncrementalDecoder(IncrementalDecoder): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
279 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
280 This subclass of IncrementalDecoder can be used as the baseclass for an |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
281 incremental decoder if the decoder must be able to handle incomplete byte |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
282 sequences. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
283 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
284 def __init__(self, errors='strict'): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
285 IncrementalDecoder.__init__(self, errors) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
286 self.buffer = "" # undecoded input that is kept between calls to decode() |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
287 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
288 def _buffer_decode(self, input, errors, final): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
289 # Overwrite this method in subclasses: It must decode input |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
290 # and return an (output, length consumed) tuple |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
291 raise NotImplementedError |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
292 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
293 def decode(self, input, final=False): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
294 # decode input (taking the buffer into account) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
295 data = self.buffer + input |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
296 (result, consumed) = self._buffer_decode(data, self.errors, final) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
297 # keep undecoded input until the next call |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
298 self.buffer = data[consumed:] |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
299 return result |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
300 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
301 def reset(self): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
302 IncrementalDecoder.reset(self) |
|
37716
1d5a4c82061e
Fix wrong attribute name.
Walter Dörwald <walter@livinglogic.de>
parents:
37080
diff
changeset
|
303 self.buffer = "" |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
304 |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
305 def getstate(self): |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
306 # additional state info is always 0 |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
307 return (self.buffer, 0) |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
308 |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
309 def setstate(self, state): |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
310 # ignore additional state info |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
311 self.buffer = state[0] |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
40239
diff
changeset
|
312 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 # |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 # The StreamWriter and StreamReader class provide generic working |
| 19967 | 315 # 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
|
316 # 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
|
317 # 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
|
318 # |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 class StreamWriter(Codec): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 |
|
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
|
322 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
|
323 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 """ Creates a StreamWriter instance. |
|
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 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
|
327 (binary) data. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 |
|
26293
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
329 The StreamWriter may use different error handling |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 schemes by providing the errors keyword argument. These |
|
26293
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
331 parameters are predefined: |
|
11990
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 '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
|
334 '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
|
335 'replace'- replace with a suitable replacement character |
|
26293
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
336 'xmlcharrefreplace' - Replace with the appropriate XML |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
337 character reference. |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
338 'backslashreplace' - Replace with backslashed escape |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
339 sequences (only for encoding). |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 |
|
26293
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
341 The set of allowed parameter values can be extended via |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
342 register_error. |
|
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 self.stream = stream |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 self.errors = errors |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 |
| 12367 | 347 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
|
348 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 """ 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
|
350 """ |
|
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
|
351 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
|
352 self.stream.write(data) |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 |
| 12367 | 354 def writelines(self, list): |
| 355 | |
| 356 """ Writes the concatenated list of strings to the stream | |
| 357 using .write(). | |
| 358 """ | |
| 359 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
|
360 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 def reset(self): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 """ 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
|
364 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 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
|
366 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
|
367 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
|
368 stream to recover state. |
|
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 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 pass |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 |
|
61859
bf82c1d04e06
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61847
diff
changeset
|
373 def seek(self, offset, whence=0): |
|
bf82c1d04e06
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61847
diff
changeset
|
374 self.stream.seek(offset, whence) |
|
bf82c1d04e06
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61847
diff
changeset
|
375 if whence == 0 and offset == 0: |
|
bf82c1d04e06
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61847
diff
changeset
|
376 self.reset() |
|
bf82c1d04e06
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61847
diff
changeset
|
377 |
|
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
|
378 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
|
379 getattr=getattr): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 """ 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
|
382 """ |
|
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
|
383 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
|
384 |
|
40239
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
385 def __enter__(self): |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
386 return self |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
387 |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
388 def __exit__(self, type, value, tb): |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
389 self.stream.close() |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
390 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
391 ### |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 class StreamReader(Codec): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 |
|
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
|
395 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
|
396 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
397 """ Creates a StreamReader instance. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
398 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
399 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
|
400 (binary) data. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
401 |
|
26293
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
402 The StreamReader may use different error handling |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 schemes by providing the errors keyword argument. These |
|
26293
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
404 parameters are predefined: |
|
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 '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
|
407 '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
|
408 '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
|
409 |
|
26293
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
410 The set of allowed parameter values can be extended via |
|
ff5b510eba6b
Add missing documentation for the PEP 293 functionality to
Walter Dörwald <walter@livinglogic.de>
parents:
26082
diff
changeset
|
411 register_error. |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 self.stream = stream |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 self.errors = errors |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
415 self.bytebuffer = "" |
|
35269
2b117eb18944
Make attributes and local variables in the StreamReader str objects instead
Walter Dörwald <walter@livinglogic.de>
parents:
34924
diff
changeset
|
416 # For str->str decoding this will stay a str |
|
2b117eb18944
Make attributes and local variables in the StreamReader str objects instead
Walter Dörwald <walter@livinglogic.de>
parents:
34924
diff
changeset
|
417 # For str->unicode decoding the first read will promote it to unicode |
|
2b117eb18944
Make attributes and local variables in the StreamReader str objects instead
Walter Dörwald <walter@livinglogic.de>
parents:
34924
diff
changeset
|
418 self.charbuffer = "" |
|
35589
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
419 self.linebuffer = None |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
420 |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
421 def decode(self, input, errors='strict'): |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
422 raise NotImplementedError |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
423 |
|
35415
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
424 def read(self, size=-1, chars=-1, firstline=False): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 """ 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
|
427 resulting object. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
429 chars indicates the number of characters to read from the |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
430 stream. read() will never return more than chars |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
431 characters, but it might return less, if there are not enough |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
432 characters available. |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
433 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 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
|
435 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
|
436 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
|
437 -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
|
438 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
|
439 step. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
440 |
|
35415
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
441 If firstline is true, and a UnicodeDecodeError happens |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
442 after the first line terminator in the input only the first line |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
443 will be returned, the rest of the input will be kept until the |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
444 next call to read(). |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
445 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
446 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
|
447 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
|
448 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
|
449 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
|
450 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
|
451 """ |
|
35589
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
452 # If we have lines cached, first merge them back into characters |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
453 if self.linebuffer: |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
454 self.charbuffer = "".join(self.linebuffer) |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
455 self.linebuffer = None |
|
36121
686306a67e9b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
35589
diff
changeset
|
456 |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
457 # read until we get the required number of characters (if available) |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
458 while True: |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
459 # can the request can be satisfied from the character buffer? |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
460 if chars < 0: |
|
36855
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36121
diff
changeset
|
461 if size < 0: |
|
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36121
diff
changeset
|
462 if self.charbuffer: |
|
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36121
diff
changeset
|
463 break |
|
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36121
diff
changeset
|
464 elif len(self.charbuffer) >= size: |
| 34214 | 465 break |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
466 else: |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
467 if len(self.charbuffer) >= chars: |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
468 break |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
469 # we need more data |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
470 if size < 0: |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
471 newdata = self.stream.read() |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
472 else: |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
473 newdata = self.stream.read(size) |
| 34214 | 474 # decode bytes (those remaining from the last call included) |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
475 data = self.bytebuffer + newdata |
|
35415
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
476 try: |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
477 newchars, decodedbytes = self.decode(data, self.errors) |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
478 except UnicodeDecodeError, exc: |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
479 if firstline: |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
480 newchars, decodedbytes = self.decode(data[:exc.start], self.errors) |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
481 lines = newchars.splitlines(True) |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
482 if len(lines)<=1: |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
483 raise |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
484 else: |
|
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
485 raise |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
486 # keep undecoded bytes until the next call |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
487 self.bytebuffer = data[decodedbytes:] |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
488 # put new characters in the character buffer |
| 34214 | 489 self.charbuffer += newchars |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
490 # there was no data available |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
491 if not newdata: |
| 34214 | 492 break |
| 493 if chars < 0: | |
| 494 # Return everything we've got | |
| 495 result = self.charbuffer | |
|
35269
2b117eb18944
Make attributes and local variables in the StreamReader str objects instead
Walter Dörwald <walter@livinglogic.de>
parents:
34924
diff
changeset
|
496 self.charbuffer = "" |
| 34214 | 497 else: |
| 498 # Return the first chars characters | |
| 499 result = self.charbuffer[:chars] | |
| 500 self.charbuffer = self.charbuffer[chars:] | |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
501 return result |
|
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
|
502 |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
503 def readline(self, size=None, keepends=True): |
| 12367 | 504 |
| 505 """ Read one line from the input stream and return the | |
| 506 decoded data. | |
| 507 | |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
508 size, if given, is passed as size argument to the |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
509 read() 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
|
510 |
| 12367 | 511 """ |
|
35589
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
512 # If we have lines cached from an earlier read, return |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
513 # them unconditionally |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
514 if self.linebuffer: |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
515 line = self.linebuffer[0] |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
516 del self.linebuffer[0] |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
517 if len(self.linebuffer) == 1: |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
518 # revert to charbuffer mode; we might need more data |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
519 # next time |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
520 self.charbuffer = self.linebuffer[0] |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
521 self.linebuffer = None |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
522 if not keepends: |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
523 line = line.splitlines(False)[0] |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
524 return line |
|
36121
686306a67e9b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
35589
diff
changeset
|
525 |
| 34214 | 526 readsize = size or 72 |
|
35269
2b117eb18944
Make attributes and local variables in the StreamReader str objects instead
Walter Dörwald <walter@livinglogic.de>
parents:
34924
diff
changeset
|
527 line = "" |
| 34214 | 528 # If size is given, we call read() only once |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
529 while True: |
|
35415
ff2e66fc0a29
Return complete lines from codec stream readers
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
530 data = self.read(readsize, firstline=True) |
| 34214 | 531 if data: |
| 34924 | 532 # If we're at a "\r" read one extra character (which might |
| 533 # be a "\n") to get a proper line ending. If the stream is | |
|
34923
bc123b9cef7f
If the data read from the bytestream in readline() ends in a '\r' read one more
Walter Dörwald <walter@livinglogic.de>
parents:
34866
diff
changeset
|
534 # temporarily exhausted we return the wrong line ending. |
|
35269
2b117eb18944
Make attributes and local variables in the StreamReader str objects instead
Walter Dörwald <walter@livinglogic.de>
parents:
34924
diff
changeset
|
535 if data.endswith("\r"): |
|
34865
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34761
diff
changeset
|
536 data += self.read(size=1, chars=1) |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34761
diff
changeset
|
537 |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
538 line += data |
| 34214 | 539 lines = line.splitlines(True) |
| 540 if lines: | |
|
35589
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
541 if len(lines) > 1: |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
542 # More than one line result; the first line is a full line |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
543 # to return |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
544 line = lines[0] |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
545 del lines[0] |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
546 if len(lines) > 1: |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
547 # cache the remaining lines |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
548 lines[-1] += self.charbuffer |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
549 self.linebuffer = lines |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
550 self.charbuffer = None |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
551 else: |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
552 # only one remaining line, put it back into charbuffer |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
553 self.charbuffer = lines[0] + self.charbuffer |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
554 if not keepends: |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
555 line = line.splitlines(False)[0] |
|
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
556 break |
| 34214 | 557 line0withend = lines[0] |
| 558 line0withoutend = lines[0].splitlines(False)[0] | |
| 559 if line0withend != line0withoutend: # We really have a line end | |
| 560 # Put the rest back together and keep it until the next call | |
|
35269
2b117eb18944
Make attributes and local variables in the StreamReader str objects instead
Walter Dörwald <walter@livinglogic.de>
parents:
34924
diff
changeset
|
561 self.charbuffer = "".join(lines[1:]) + self.charbuffer |
| 34214 | 562 if keepends: |
| 563 line = line0withend | |
| 564 else: | |
| 565 line = line0withoutend | |
| 34387 | 566 break |
| 34214 | 567 # we didn't get anything or this was our only try |
| 34387 | 568 if not data or size is not None: |
| 34214 | 569 if line and not keepends: |
| 570 line = line.splitlines(False)[0] | |
| 571 break | |
| 572 if readsize<8000: | |
| 573 readsize *= 2 | |
| 574 return line | |
|
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
|
575 |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
576 def readlines(self, sizehint=None, keepends=True): |
| 12367 | 577 |
| 578 """ Read all lines available on the input stream | |
| 579 and return them as list of lines. | |
| 580 | |
| 581 Line breaks are implemented using the codec's decoder | |
| 582 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
|
583 |
|
31591
26a80f0af9f3
Ignore sizehint argument. Fixes SF #844561.
Marc-André Lemburg <mal@egenix.com>
parents:
27618
diff
changeset
|
584 sizehint, if given, is ignored since there is no efficient |
|
26a80f0af9f3
Ignore sizehint argument. Fixes SF #844561.
Marc-André Lemburg <mal@egenix.com>
parents:
27618
diff
changeset
|
585 way to finding the true end-of-line. |
| 12367 | 586 |
| 587 """ | |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
31591
diff
changeset
|
588 data = self.read() |
|
33782
a52de911b284
SF #1048865: Fix a trivial typo that breaks StreamReader.readlines()
Hye-Shik Chang <hyeshik@gmail.com>
parents:
33428
diff
changeset
|
589 return data.splitlines(keepends) |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
590 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
591 def reset(self): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
592 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
593 """ 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
|
594 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
595 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
|
596 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
|
597 from decoding errors. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
598 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
599 """ |
| 34751 | 600 self.bytebuffer = "" |
| 601 self.charbuffer = u"" | |
|
35589
5f2785722cba
Patch #1268314: Cache lines in StreamReader.readlines for performance.
Martin v. Löwis <martin@v.loewis.de>
parents:
35508
diff
changeset
|
602 self.linebuffer = None |
| 34751 | 603 |
| 34753 | 604 def seek(self, offset, whence=0): |
| 34751 | 605 """ Set the input stream's current position. |
| 606 | |
| 607 Resets the codec buffers used for keeping state. | |
| 608 """ | |
|
61859
bf82c1d04e06
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61847
diff
changeset
|
609 self.stream.seek(offset, whence) |
| 34751 | 610 self.reset() |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
611 |
|
26082
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
612 def next(self): |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
613 |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
614 """ Return the next decoded line from the input stream.""" |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
615 line = self.readline() |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
616 if line: |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
617 return line |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
618 raise StopIteration |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
619 |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
620 def __iter__(self): |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
621 return self |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
622 |
|
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
|
623 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
|
624 getattr=getattr): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
625 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
626 """ 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
|
627 """ |
|
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
|
628 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
|
629 |
|
40239
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
630 def __enter__(self): |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
631 return self |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
632 |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
633 def __exit__(self, type, value, tb): |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
634 self.stream.close() |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
635 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
636 ### |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
637 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
638 class StreamReaderWriter: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
639 |
|
12389
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
640 """ StreamReaderWriter instances allow wrapping streams which |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
641 work in both read and write modes. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
642 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
643 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
|
644 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
|
645 instance. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
646 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
647 """ |
| 12367 | 648 # Optional attributes set by the file wrappers below |
| 649 encoding = 'unknown' | |
| 650 | |
|
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
|
651 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
|
652 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
653 """ Creates a StreamReaderWriter instance. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
654 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
655 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
|
656 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
657 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
|
658 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
|
659 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
660 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
|
661 StreamWriter/Readers. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
662 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
663 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
664 self.stream = stream |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
665 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
|
666 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
|
667 self.errors = errors |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
668 |
|
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
|
669 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
|
670 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
671 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
|
672 |
| 12534 | 673 def readline(self, size=None): |
| 12367 | 674 |
| 675 return self.reader.readline(size) | |
| 676 | |
| 12534 | 677 def readlines(self, sizehint=None): |
| 12367 | 678 |
| 679 return self.reader.readlines(sizehint) | |
| 680 | |
|
26082
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
681 def next(self): |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
682 |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
683 """ Return the next decoded line from the input stream.""" |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
684 return self.reader.next() |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
685 |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
686 def __iter__(self): |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
687 return self |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
688 |
|
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
|
689 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
|
690 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
691 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
|
692 |
|
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
|
693 def writelines(self, list): |
| 12367 | 694 |
| 695 return self.writer.writelines(list) | |
| 696 | |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
697 def reset(self): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
698 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
699 self.reader.reset() |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
700 self.writer.reset() |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
701 |
|
61847
99e9ed214aad
Issue #6268: Fix seek() method of codecs.open(), don't read the BOM twice
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59147
diff
changeset
|
702 def seek(self, offset, whence=0): |
|
61859
bf82c1d04e06
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61847
diff
changeset
|
703 self.stream.seek(offset, whence) |
|
bf82c1d04e06
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61847
diff
changeset
|
704 self.reader.reset() |
|
bf82c1d04e06
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61847
diff
changeset
|
705 if whence == 0 and offset == 0: |
|
bf82c1d04e06
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61847
diff
changeset
|
706 self.writer.reset() |
|
61847
99e9ed214aad
Issue #6268: Fix seek() method of codecs.open(), don't read the BOM twice
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59147
diff
changeset
|
707 |
|
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
|
708 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
|
709 getattr=getattr): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
710 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
711 """ 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
|
712 """ |
|
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
|
713 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
|
714 |
|
40239
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
715 # these are needed to make "with codecs.open(...)" work properly |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
716 |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
717 def __enter__(self): |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
718 return self |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
719 |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
720 def __exit__(self, type, value, tb): |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
721 self.stream.close() |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
722 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
723 ### |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
724 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
725 class StreamRecoder: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
726 |
|
12389
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
727 """ StreamRecoder instances provide a frontend - backend |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
728 view of encoding data. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
729 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
730 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
|
731 codecs.lookup() function to implement their task. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
732 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
733 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
|
734 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
|
735 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
|
736 of the provided Writer class. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
737 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
738 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
|
739 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
|
740 |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
741 """ |
| 12367 | 742 # Optional attributes set by the file wrappers below |
| 743 data_encoding = 'unknown' | |
| 744 file_encoding = 'unknown' | |
| 745 | |
|
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
|
746 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
|
747 errors='strict'): |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
748 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
749 """ 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
|
750 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
|
751 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
|
752 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
|
753 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
|
754 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
755 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
|
756 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
|
757 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
758 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
|
759 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
760 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
|
761 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
|
762 StreamReader, StreamWriter interface resp. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
763 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
764 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
|
765 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
|
766 used as intermediate encoding. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
767 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
768 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
|
769 StreamWriter/Readers. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
770 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
771 """ |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
772 self.stream = stream |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
773 self.encode = encode |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
774 self.decode = decode |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
775 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
|
776 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
|
777 self.errors = errors |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
778 |
|
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
|
779 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
|
780 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
781 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
|
782 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
|
783 return data |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
784 |
|
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
|
785 def readline(self, size=None): |
| 12367 | 786 |
| 787 if size is None: | |
| 788 data = self.reader.readline() | |
| 789 else: | |
| 790 data = self.reader.readline(size) | |
| 791 data, bytesencoded = self.encode(data, self.errors) | |
| 792 return data | |
| 793 | |
|
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
|
794 def readlines(self, sizehint=None): |
| 12367 | 795 |
|
31591
26a80f0af9f3
Ignore sizehint argument. Fixes SF #844561.
Marc-André Lemburg <mal@egenix.com>
parents:
27618
diff
changeset
|
796 data = self.reader.read() |
| 12367 | 797 data, bytesencoded = self.encode(data, self.errors) |
| 798 return data.splitlines(1) | |
| 799 | |
|
26082
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
800 def next(self): |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
801 |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
802 """ Return the next decoded line from the input stream.""" |
|
35508
1f2af259968b
SF bug #1235646: codecs.StreamRecoder.next() now reencodes the data it reads
Walter Dörwald <walter@livinglogic.de>
parents:
35415
diff
changeset
|
803 data = self.reader.next() |
|
1f2af259968b
SF bug #1235646: codecs.StreamRecoder.next() now reencodes the data it reads
Walter Dörwald <walter@livinglogic.de>
parents:
35415
diff
changeset
|
804 data, bytesencoded = self.encode(data, self.errors) |
|
1f2af259968b
SF bug #1235646: codecs.StreamRecoder.next() now reencodes the data it reads
Walter Dörwald <walter@livinglogic.de>
parents:
35415
diff
changeset
|
805 return data |
|
26082
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
806 |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
807 def __iter__(self): |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
808 return self |
|
f3a64035ba8b
Add next() and __iter__() methods to StreamReader, StreamReaderWriter
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
809 |
|
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
|
810 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
|
811 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
812 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
|
813 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
|
814 |
|
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
|
815 def writelines(self, list): |
| 12367 | 816 |
| 817 data = ''.join(list) | |
| 818 data, bytesdecoded = self.decode(data, self.errors) | |
| 819 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
|
820 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
821 def reset(self): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
822 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
823 self.reader.reset() |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
824 self.writer.reset() |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
825 |
|
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
|
826 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
|
827 getattr=getattr): |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
828 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
829 """ 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
|
830 """ |
|
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
|
831 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
|
832 |
|
40239
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
833 def __enter__(self): |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
834 return self |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
835 |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
836 def __exit__(self, type, value, tb): |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
837 self.stream.close() |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
838 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
839 ### Shortcuts |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
840 |
|
12986
5279a6e00d36
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12534
diff
changeset
|
841 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
|
842 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
843 """ 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
|
844 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
|
845 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
846 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
|
847 defined by the codecs, i.e. Unicode objects for most builtin |
| 34761 | 848 codecs. Output is also codec dependent and will usually be |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
849 Unicode as well. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
850 |
|
12986
5279a6e00d36
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12534
diff
changeset
|
851 Files are always opened in binary mode, even if no binary mode |
| 27618 | 852 was specified. This is done to avoid data loss due to encodings |
|
12986
5279a6e00d36
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12534
diff
changeset
|
853 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
|
854 open the file in binary read mode. |
|
5279a6e00d36
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12534
diff
changeset
|
855 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
856 encoding specifies the encoding which is to be used for the |
| 27618 | 857 file. |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
858 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
859 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
|
860 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
|
861 encoding error occurs. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
862 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
863 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
|
864 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
|
865 |
|
12389
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
866 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
|
867 .encoding which allows querying the used encoding. This |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
868 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
|
869 parameter. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
870 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
871 """ |
|
59147
7e3c47354da3
#691291: codecs.open() should not convert end of lines on reading and writing.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
46107
diff
changeset
|
872 if encoding is not None: |
|
7e3c47354da3
#691291: codecs.open() should not convert end of lines on reading and writing.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
46107
diff
changeset
|
873 if 'U' in mode: |
|
7e3c47354da3
#691291: codecs.open() should not convert end of lines on reading and writing.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
46107
diff
changeset
|
874 # No automatic conversion of '\n' is done on reading and writing |
|
7e3c47354da3
#691291: codecs.open() should not convert end of lines on reading and writing.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
46107
diff
changeset
|
875 mode = mode.strip().replace('U', '') |
|
7e3c47354da3
#691291: codecs.open() should not convert end of lines on reading and writing.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
46107
diff
changeset
|
876 if mode[:1] not in set('rwa'): |
|
7e3c47354da3
#691291: codecs.open() should not convert end of lines on reading and writing.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
46107
diff
changeset
|
877 mode = 'r' + mode |
|
7e3c47354da3
#691291: codecs.open() should not convert end of lines on reading and writing.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
46107
diff
changeset
|
878 if 'b' not in mode: |
|
7e3c47354da3
#691291: codecs.open() should not convert end of lines on reading and writing.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
46107
diff
changeset
|
879 # Force opening of the file in binary mode |
|
7e3c47354da3
#691291: codecs.open() should not convert end of lines on reading and writing.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
46107
diff
changeset
|
880 mode = mode + 'b' |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
881 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
|
882 if encoding is None: |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
883 return file |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
884 info = lookup(encoding) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
885 srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors) |
| 12367 | 886 # Add attributes to simplify introspection |
| 887 srw.encoding = encoding | |
| 888 return srw | |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
889 |
| 12367 | 890 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
|
891 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
892 """ 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
|
893 encoding translation. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
894 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
895 Strings written to the wrapped file are interpreted according |
| 12367 | 896 to the given data_encoding and then written to the original |
| 897 file as string using file_encoding. The intermediate encoding | |
| 898 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
|
899 |
| 12367 | 900 Strings are read from the file using file_encoding and then |
| 901 passed back to the caller as string using data_encoding. | |
| 902 | |
| 903 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
|
904 |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
905 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
|
906 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
|
907 encoding error occurs. |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
908 |
|
12389
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
909 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
|
910 .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
|
911 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
|
912 introspection by Python programs. |
|
e5b0bea03cfa
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12373
diff
changeset
|
913 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
914 """ |
| 12367 | 915 if file_encoding is None: |
| 916 file_encoding = data_encoding | |
|
40239
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
917 data_info = lookup(data_encoding) |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
918 file_info = lookup(file_encoding) |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
919 sr = StreamRecoder(file, data_info.encode, data_info.decode, |
|
7c4058681224
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
Georg Brandl <georg@python.org>
parents:
37719
diff
changeset
|
920 file_info.streamreader, file_info.streamwriter, errors) |
| 12367 | 921 # Add attributes to simplify introspection |
| 922 sr.data_encoding = data_encoding | |
| 923 sr.file_encoding = file_encoding | |
| 924 return sr | |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
925 |
|
19981
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
926 ### 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
|
927 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
928 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
|
929 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
930 """ 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
|
931 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
|
932 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
933 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
|
934 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
935 """ |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
936 return lookup(encoding).encode |
|
19981
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
937 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
938 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
|
939 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
940 """ 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
|
941 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
|
942 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
943 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
|
944 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
945 """ |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
946 return lookup(encoding).decode |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
947 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
948 def getincrementalencoder(encoding): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
949 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
950 """ Lookup up the codec for the given encoding and return |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
951 its IncrementalEncoder class or factory function. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
952 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
953 Raises a LookupError in case the encoding cannot be found |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
954 or the codecs doesn't provide an incremental encoder. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
955 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
956 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
957 encoder = lookup(encoding).incrementalencoder |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
958 if encoder is None: |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
959 raise LookupError(encoding) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
960 return encoder |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
961 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
962 def getincrementaldecoder(encoding): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
963 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
964 """ Lookup up the codec for the given encoding and return |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
965 its IncrementalDecoder class or factory function. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
966 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
967 Raises a LookupError in case the encoding cannot be found |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
968 or the codecs doesn't provide an incremental decoder. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
969 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
970 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
971 decoder = lookup(encoding).incrementaldecoder |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
972 if decoder is None: |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
973 raise LookupError(encoding) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
974 return decoder |
|
19981
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
975 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
976 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
|
977 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
978 """ 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
|
979 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
|
980 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
981 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
|
982 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
983 """ |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
984 return lookup(encoding).streamreader |
|
19981
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
985 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
986 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
|
987 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
988 """ 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
|
989 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
|
990 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
991 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
|
992 |
|
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
993 """ |
|
36996
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
994 return lookup(encoding).streamwriter |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
995 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
996 def iterencode(iterator, encoding, errors='strict', **kwargs): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
997 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
998 Encoding iterator. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
999 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1000 Encodes the input strings from the iterator using a IncrementalEncoder. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1001 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1002 errors and kwargs are passed through to the IncrementalEncoder |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1003 constructor. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1004 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1005 encoder = getincrementalencoder(encoding)(errors, **kwargs) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1006 for input in iterator: |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1007 output = encoder.encode(input) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1008 if output: |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1009 yield output |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1010 output = encoder.encode("", True) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1011 if output: |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1012 yield output |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1013 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1014 def iterdecode(iterator, encoding, errors='strict', **kwargs): |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1015 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1016 Decoding iterator. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1017 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1018 Decodes the input strings from the iterator using a IncrementalDecoder. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1019 |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1020 errors and kwargs are passed through to the IncrementalDecoder |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1021 constructor. |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1022 """ |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1023 decoder = getincrementaldecoder(encoding)(errors, **kwargs) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1024 for input in iterator: |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1025 output = decoder.decode(input) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1026 if output: |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1027 yield output |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1028 output = decoder.decode("", True) |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1029 if output: |
|
7fa4c29d7030
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
Walter Dörwald <walter@livinglogic.de>
parents:
36855
diff
changeset
|
1030 yield output |
|
19981
0fd95200ab6e
Added new helpers for easy access to codecs. Docs will follow.
Marc-André Lemburg <mal@egenix.com>
parents:
19967
diff
changeset
|
1031 |
|
15706
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
1032 ### 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
|
1033 |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
1034 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
|
1035 |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
1036 """ 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
|
1037 |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
1038 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
|
1039 mapped to themselves. |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15706
diff
changeset
|
1040 |
|
15706
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
1041 """ |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
1042 res = {} |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
1043 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
|
1044 res[i]=i |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
1045 return res |
|
bf9bb0e3dd34
This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents:
15498
diff
changeset
|
1046 |
|
17843
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1047 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
|
1048 |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1049 """ 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
|
1050 |
| 27618 | 1051 If a target mapping in the decoding map occurs multiple |
|
17843
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1052 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
|
1053 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
|
1054 during translation. |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1055 |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1056 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
|
1057 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
|
1058 |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1059 """ |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1060 m = {} |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1061 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
|
1062 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
|
1063 m[v] = k |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1064 else: |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1065 m[v] = None |
|
3e256f9d99ad
Moved the encoding map building logic from the individual mapping
Marc-André Lemburg <mal@egenix.com>
parents:
17839
diff
changeset
|
1066 return m |
|
17978
5e87a08abb05
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17843
diff
changeset
|
1067 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
1068 ### 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
|
1069 |
|
34707
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1070 try: |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1071 strict_errors = lookup_error("strict") |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1072 ignore_errors = lookup_error("ignore") |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1073 replace_errors = lookup_error("replace") |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1074 xmlcharrefreplace_errors = lookup_error("xmlcharrefreplace") |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1075 backslashreplace_errors = lookup_error("backslashreplace") |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1076 except LookupError: |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1077 # In --disable-unicode builds, these error handler are missing |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1078 strict_errors = None |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1079 ignore_errors = None |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1080 replace_errors = None |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1081 xmlcharrefreplace_errors = None |
|
0d44d9071e97
Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents:
34387
diff
changeset
|
1082 backslashreplace_errors = None |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
23658
diff
changeset
|
1083 |
|
18904
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
1084 # 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
|
1085 # package |
|
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
1086 _false = 0 |
|
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
1087 if _false: |
|
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
1088 import encodings |
|
5af1ee422575
Add dead imports of modules that are "magically" imported.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
1089 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1090 ### 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
|
1091 |
|
11990
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1092 if __name__ == '__main__': |
|
f104251b6f8f
Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1093 |
| 12367 | 1094 # Make stdout translate Latin-1 output into UTF-8 output |
| 1095 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
|
1096 |
| 12367 | 1097 # Have stdin translate Latin-1 input into UTF-8 input |
| 1098 sys.stdin = EncodedFile(sys.stdin, 'utf-8', 'latin-1') |
