8 encode
= codecs
.utf_32_encode
10 def decode(input, errors
='strict'):
11 return codecs
.utf_32_decode(input, errors
, True)
13 class IncrementalEncoder(codecs
.IncrementalEncoder
):
14 def __init__(self
, errors
='strict'):
15 codecs
.IncrementalEncoder
.__init
__(self
, errors
)
18 def encode(self
, input, final
=False):
19 if self
.encoder
is None:
20 result
= codecs
.utf_32_encode(input, self
.errors
)[0]
21 if sys
.byteorder
== 'little':
22 self
.encoder
= codecs
.utf_32_le_encode
24 self
.encoder
= codecs
.utf_32_be_encode
26 return self
.encoder(input, self
.errors
)[0]
29 codecs
.IncrementalEncoder
.reset(self
)
33 # state info we return to the caller:
34 # 0: stream is in natural order for this platform
35 # 2: endianness hasn't been determined yet
36 # (we're never writing in unnatural order)
37 return (2 if self
.encoder
is None else 0)
39 def setstate(self
, state
):
43 if sys
.byteorder
== 'little':
44 self
.encoder
= codecs
.utf_32_le_encode
46 self
.encoder
= codecs
.utf_32_be_encode
48 class IncrementalDecoder(codecs
.BufferedIncrementalDecoder
):
49 def __init__(self
, errors
='strict'):
50 codecs
.BufferedIncrementalDecoder
.__init
__(self
, errors
)
53 def _buffer_decode(self
, input, errors
, final
):
54 if self
.decoder
is None:
55 (output
, consumed
, byteorder
) = \
56 codecs
.utf_32_ex_decode(input, errors
, 0, final
)
58 self
.decoder
= codecs
.utf_32_le_decode
60 self
.decoder
= codecs
.utf_32_be_decode
62 raise UnicodeError("UTF-32 stream does not start with BOM")
63 return (output
, consumed
)
64 return self
.decoder(input, self
.errors
, final
)
67 codecs
.BufferedIncrementalDecoder
.reset(self
)
71 # additonal state info from the base class must be None here,
72 # as it isn't passed along to the caller
73 state
= codecs
.BufferedIncrementalDecoder
.getstate(self
)[0]
74 # additional state info we pass to the caller:
75 # 0: stream is in natural order for this platform
76 # 1: stream is in unnatural order
77 # 2: endianness hasn't been determined yet
78 if self
.decoder
is None:
80 addstate
= int((sys
.byteorder
== "big") !=
81 (self
.decoder
is codecs
.utf_32_be_decode
))
82 return (state
, addstate
)
84 def setstate(self
, state
):
85 # state[1] will be ignored by BufferedIncrementalDecoder.setstate()
86 codecs
.BufferedIncrementalDecoder
.setstate(self
, state
)
89 self
.decoder
= (codecs
.utf_32_be_decode
90 if sys
.byteorder
== "big"
91 else codecs
.utf_32_le_decode
)
93 self
.decoder
= (codecs
.utf_32_le_decode
94 if sys
.byteorder
== "big"
95 else codecs
.utf_32_be_decode
)
99 class StreamWriter(codecs
.StreamWriter
):
100 def __init__(self
, stream
, errors
='strict'):
101 self
.bom_written
= False
102 codecs
.StreamWriter
.__init
__(self
, stream
, errors
)
104 def encode(self
, input, errors
='strict'):
105 self
.bom_written
= True
106 result
= codecs
.utf_32_encode(input, errors
)
107 if sys
.byteorder
== 'little':
108 self
.encode
= codecs
.utf_32_le_encode
110 self
.encode
= codecs
.utf_32_be_encode
113 class StreamReader(codecs
.StreamReader
):
116 codecs
.StreamReader
.reset(self
)
119 except AttributeError:
122 def decode(self
, input, errors
='strict'):
123 (object, consumed
, byteorder
) = \
124 codecs
.utf_32_ex_decode(input, errors
, 0, False)
126 self
.decode
= codecs
.utf_32_le_decode
128 self
.decode
= codecs
.utf_32_be_decode
130 raise UnicodeError,"UTF-32 stream does not start with BOM"
131 return (object, consumed
)
133 ### encodings module API
136 return codecs
.CodecInfo(
140 incrementalencoder
=IncrementalEncoder
,
141 incrementaldecoder
=IncrementalDecoder
,
142 streamreader
=StreamReader
,
143 streamwriter
=StreamWriter
,