annotate Lib/codecs.py @ 16413:8819867f14ef legacy-trunk

Patch derived from Trent's 101162: a Python/C API testing framework. STILL NEEDS UNIX BUILD CHANGES.
author Tim Peters <tim.peters@gmail.com>
date Fri, 02 Feb 2001 05:57:15 +0000
parents b5345e99596b
children c872fdf10130
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 import struct,types,__builtin__
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 *
bc861add5d71 Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents: 12051
diff changeset
16 except ImportError,why:
bc861add5d71 Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents: 12051
diff changeset
17 raise SystemError,\
bc861add5d71 Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents: 12051
diff changeset
18 'Failed to load the builtin codecs: %s' % why
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
20 __all__ = ["register","lookup","open","EncodedFile","BOM","BOM_BE",
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
21 "BOM_LE","BOM32_BE","BOM32_LE","BOM64_BE","BOM64_LE"]
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
22
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 ### Constants
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 #
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # Byte Order Mark (BOM) and its possible values (BOM_BE, BOM_LE)
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 BOM = struct.pack('=H',0xFEFF)
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 BOM_BE = BOM32_BE = '\376\377'
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15706
diff changeset
31 # corresponds to Unicode U+FEFF in UTF-16 on big endian
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15706
diff changeset
32 # platforms == ZERO WIDTH NO-BREAK SPACE
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
33 BOM_LE = BOM32_LE = '\377\376'
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15706
diff changeset
34 # corresponds to Unicode U+FFFE in UTF-16 on little endian
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15706
diff changeset
35 # platforms == defined as being an illegal Unicode character
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 #
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # 64-bit Byte Order Marks
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 #
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 BOM64_BE = '\000\000\376\377'
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15706
diff changeset
41 # corresponds to Unicode U+0000FEFF in UCS-4
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 BOM64_LE = '\377\376\000\000'
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15706
diff changeset
43 # corresponds to Unicode U+0000FFFE in UCS-4
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 ### 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
47
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 class Codec:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 """ 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
51
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 The .encode()/.decode() methods may implement different error
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 handling schemes by providing the errors argument. These
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 string values are defined:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
12051
d89556c04ef9 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12011
diff changeset
56 '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
57 '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
58 '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
59 Python will use the official U+FFFD REPLACEMENT
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 CHARACTER for the builtin Unicode codecs.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 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
64
12010
bd1014215f62 "intput" --> "input" (in docstring); noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents: 11990
diff changeset
65 """ 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
66 object, length consumed).
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 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
69 'strict' handling.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 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
72 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
73 make encoding/decoding efficient.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 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
76 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
77 situation.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 raise NotImplementedError
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 def decode(self,input,errors='strict'):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 """ 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
85 object, length consumed).
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 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
88 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
89 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
90
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 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
92 'strict' handling.
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 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
95 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
96 make encoding/decoding efficient.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 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
99 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
100 situation.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
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
102 """
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 raise NotImplementedError
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 #
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 # The StreamWriter and StreamReader class provide generic working
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 # interfaces which can be used to implement new encodings submodules
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 # 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
109 # 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
110 #
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 class StreamWriter(Codec):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 def __init__(self,stream,errors='strict'):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 """ Creates a StreamWriter instance.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 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
119 (binary) data.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 The StreamWriter may implement different error handling
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 schemes by providing the errors keyword argument. These
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 parameters are defined:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 '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
126 '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
127 '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
128
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 self.stream = stream
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 self.errors = errors
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
133 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
134
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 """ 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
136 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 data, consumed = self.encode(object,self.errors)
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 self.stream.write(data)
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
140 def writelines(self, list):
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
141
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
142 """ Writes the concatenated list of strings to the stream
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
143 using .write().
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
144 """
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
145 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
146
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 def reset(self):
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 """ 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
150
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 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
152 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
153 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
154 stream to recover state.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 pass
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 def __getattr__(self,name,
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 getattr=getattr):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 """ 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
164 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 return getattr(self.stream,name)
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 ###
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 class StreamReader(Codec):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 def __init__(self,stream,errors='strict'):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 """ Creates a StreamReader instance.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 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
176 (binary) data.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
177
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 The StreamReader may implement different error handling
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 schemes by providing the errors keyword argument. These
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 parameters are defined:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 '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
183 '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
184 '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
185
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 self.stream = stream
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 self.errors = errors
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
190 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
191
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 """ 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
193 resulting object.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 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
196 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
197 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
198 -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
199 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
200 step.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 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
203 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
204 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
205 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
206 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
207
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 # Unsliced reading:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 if size < 0:
15498
46275611cd59 (Patch #102698) Fix for a bug reported by Wade Leftwich:
Andrew M. Kuchling <amk@amk.ca>
parents: 14978
diff changeset
211 return self.decode(self.stream.read(), self.errors)[0]
12373
d63b57ac617d Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents: 12367
diff changeset
212
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 # Sliced reading:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 read = self.stream.read
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 decode = self.decode
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 data = read(size)
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 i = 0
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 while 1:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 try:
15498
46275611cd59 (Patch #102698) Fix for a bug reported by Wade Leftwich:
Andrew M. Kuchling <amk@amk.ca>
parents: 14978
diff changeset
220 object, decodedbytes = decode(data, self.errors)
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 except ValueError,why:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 # This method is slow but should work under pretty much
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 # all conditions; at most 10 tries are made
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 i = i + 1
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 newdata = read(1)
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 if not newdata or i > 10:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 raise
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 data = data + newdata
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 else:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 return object
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
232 def readline(self, size=None):
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
233
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
234 """ Read one line from the input stream and return the
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
235 decoded data.
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
236
12389
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
237 Note: Unlike the .readlines() method, this method inherits
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
238 the line breaking knowledge from the underlying stream's
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
239 .readline() method -- there is currently no support for
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
240 line breaking using the codec decoder due to lack of line
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
241 buffering. Sublcasses should however, if possible, try to
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
242 implement this method using their own knowledge of line
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
243 breaking.
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
244
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
245 size, if given, is passed as size argument to the stream's
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
246 .readline() method.
12373
d63b57ac617d Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents: 12367
diff changeset
247
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
248 """
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
249 if size is None:
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
250 line = self.stream.readline()
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
251 else:
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
252 line = self.stream.readline(size)
15498
46275611cd59 (Patch #102698) Fix for a bug reported by Wade Leftwich:
Andrew M. Kuchling <amk@amk.ca>
parents: 14978
diff changeset
253 return self.decode(line,self.errors)[0]
12373
d63b57ac617d Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents: 12367
diff changeset
254
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
255
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
256 def readlines(self, sizehint=0):
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
257
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
258 """ Read all lines available on the input stream
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
259 and return them as list of lines.
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
260
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
261 Line breaks are implemented using the codec's decoder
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
262 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
263
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
264 sizehint, if given, is passed as size argument to the
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
265 stream's .read() method.
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
266
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
267 """
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
268 if sizehint is None:
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
269 data = self.stream.read()
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
270 else:
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
271 data = self.stream.read(sizehint)
15498
46275611cd59 (Patch #102698) Fix for a bug reported by Wade Leftwich:
Andrew M. Kuchling <amk@amk.ca>
parents: 14978
diff changeset
272 return self.decode(data,self.errors)[0].splitlines(1)
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 def reset(self):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
275
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 """ 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
277
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 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
279 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
280 from decoding errors.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
281
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 pass
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 def __getattr__(self,name,
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 getattr=getattr):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 """ 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
290 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 return getattr(self.stream,name)
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
292
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 ###
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
294
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 class StreamReaderWriter:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
12389
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
297 """ StreamReaderWriter instances allow wrapping streams which
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
298 work in both read and write modes.
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
299
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
300 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
301 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
302 instance.
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
303
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
304 """
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
305 # Optional attributes set by the file wrappers below
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
306 encoding = 'unknown'
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
307
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 def __init__(self,stream,Reader,Writer,errors='strict'):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 """ Creates a StreamReaderWriter instance.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
311
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 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
313
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 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
315 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
316
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 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
318 StreamWriter/Readers.
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 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 self.stream = stream
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 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
323 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
324 self.errors = errors
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 def read(self,size=-1):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 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
329
12534
189298a1fab4 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12389
diff changeset
330 def readline(self, size=None):
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
331
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
332 return self.reader.readline(size)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
333
12534
189298a1fab4 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12389
diff changeset
334 def readlines(self, sizehint=None):
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
335
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
336 return self.reader.readlines(sizehint)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
337
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 def write(self,data):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
339
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 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
341
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
342 def writelines(self,list):
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
343
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
344 return self.writer.writelines(list)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
345
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 def reset(self):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
347
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 self.reader.reset()
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 self.writer.reset()
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
350
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 def __getattr__(self,name,
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
352
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 getattr=getattr):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 """ 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
356 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 return getattr(self.stream,name)
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
358
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 ###
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
360
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 class StreamRecoder:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
12389
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
363 """ StreamRecoder instances provide a frontend - backend
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
364 view of encoding data.
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
365
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
366 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
367 codecs.lookup() function to implement their task.
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
368
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
369 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
370 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
371 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
372 of the provided Writer class.
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
373
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
374 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
375 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
376
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
377 """
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
378 # Optional attributes set by the file wrappers below
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
379 data_encoding = 'unknown'
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
380 file_encoding = 'unknown'
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
381
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 def __init__(self,stream,encode,decode,Reader,Writer,errors='strict'):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
383
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 """ 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
385 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
386 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
387 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
388 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
389
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 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
391 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
392
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 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
394
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 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
396 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
397 StreamReader, StreamWriter interface resp.
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 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
400 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
401 used as intermediate encoding.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
402
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 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
404 StreamWriter/Readers.
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 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 self.stream = stream
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 self.encode = encode
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 self.decode = decode
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 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
411 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
412 self.errors = errors
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
413
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 def read(self,size=-1):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
415
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 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
417 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
418 return data
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
419
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
420 def readline(self,size=None):
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
421
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
422 if size is None:
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
423 data = self.reader.readline()
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
424 else:
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
425 data = self.reader.readline(size)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
426 data, bytesencoded = self.encode(data, self.errors)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
427 return data
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
428
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
429 def readlines(self,sizehint=None):
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
430
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
431 if sizehint is None:
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
432 data = self.reader.read()
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
433 else:
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
434 data = self.reader.read(sizehint)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
435 data, bytesencoded = self.encode(data, self.errors)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
436 return data.splitlines(1)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
437
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 def write(self,data):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
439
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 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
441 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
442
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
443 def writelines(self,list):
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
444
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
445 data = ''.join(list)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
446 data, bytesdecoded = self.decode(data, self.errors)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
447 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
448
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 def reset(self):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
450
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 self.reader.reset()
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 self.writer.reset()
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
453
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 def __getattr__(self,name,
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
455
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 getattr=getattr):
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
457
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 """ 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
459 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 return getattr(self.stream,name)
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
461
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 ### Shortcuts
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
463
12986
5279a6e00d36 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12534
diff changeset
464 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
465
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 """ 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
467 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
468
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 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
470 defined by the codecs, i.e. Unicode objects for most builtin
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 codecs. Output is also codec dependent and will usually by
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 Unicode as well.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
473
12986
5279a6e00d36 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12534
diff changeset
474 Files are always opened in binary mode, even if no binary mode
5279a6e00d36 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12534
diff changeset
475 was specified. Thisis done to avoid data loss due to encodings
5279a6e00d36 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12534
diff changeset
476 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
477 open the file in binary read mode.
5279a6e00d36 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12534
diff changeset
478
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 encoding specifies the encoding which is to be used for the
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 the file.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
481
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 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
483 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
484 encoding error occurs.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
485
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 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
487 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
488
12389
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
489 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
490 .encoding which allows querying the used encoding. This
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
491 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
492 parameter.
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
493
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 """
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 if encoding is not None and \
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 'b' not in mode:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 # Force opening of the file in binary mode
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 mode = mode + 'b'
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 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
500 if encoding is None:
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 return file
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 (e,d,sr,sw) = lookup(encoding)
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
503 srw = StreamReaderWriter(file, sr, sw, errors)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
504 # Add attributes to simplify introspection
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
505 srw.encoding = encoding
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
506 return srw
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
507
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
508 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
509
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 """ 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
511 encoding translation.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
512
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 Strings written to the wrapped file are interpreted according
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
514 to the given data_encoding and then written to the original
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
515 file as string using file_encoding. The intermediate encoding
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
516 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
517
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
518 Strings are read from the file using file_encoding and then
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
519 passed back to the caller as string using data_encoding.
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
520
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
521 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
522
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 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
524 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
525 encoding error occurs.
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
526
12389
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
527 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
528 .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
529 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
530 introspection by Python programs.
e5b0bea03cfa M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12373
diff changeset
531
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 """
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
533 if file_encoding is None:
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
534 file_encoding = data_encoding
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
535 encode, decode = lookup(data_encoding)[:2]
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
536 Reader, Writer = lookup(file_encoding)[2:]
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
537 sr = StreamRecoder(file,
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
538 encode,decode,Reader,Writer,
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
539 errors)
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
540 # Add attributes to simplify introspection
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
541 sr.data_encoding = data_encoding
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
542 sr.file_encoding = file_encoding
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
543 return sr
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
544
15706
bf9bb0e3dd34 This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents: 15498
diff changeset
545 ### 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
546
bf9bb0e3dd34 This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents: 15498
diff changeset
547 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
548
bf9bb0e3dd34 This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents: 15498
diff changeset
549 """ 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
550
bf9bb0e3dd34 This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents: 15498
diff changeset
551 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
552 mapped to themselves.
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15706
diff changeset
553
15706
bf9bb0e3dd34 This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents: 15498
diff changeset
554 """
bf9bb0e3dd34 This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents: 15498
diff changeset
555 res = {}
bf9bb0e3dd34 This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents: 15498
diff changeset
556 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
557 res[i]=i
bf9bb0e3dd34 This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents: 15498
diff changeset
558 return res
bf9bb0e3dd34 This patch changes the default behaviour of the builtin charmap
Marc-André Lemburg <mal@egenix.com>
parents: 15498
diff changeset
559
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 ### 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
561
11990
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 if __name__ == '__main__':
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
563
f104251b6f8f Module codecs -- Python Codec Registry, API and helpers. Written by
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 import sys
12373
d63b57ac617d Deleted trailing whitespace. This is really a way to be able to add
Guido van Rossum <guido@python.org>
parents: 12367
diff changeset
565
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
566 # Make stdout translate Latin-1 output into UTF-8 output
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
567 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
568
12367
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
569 # Have stdin translate Latin-1 input into UTF-8 input
677e2147829a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12195
diff changeset
570 sys.stdin = EncodedFile(sys.stdin, 'utf-8', 'latin-1')