[Python-checkins] CVS: python/dist/src/Lib codecs.py,1.16,1.16.6.1
Tim Peters
tim_one@users.sourceforge.net
2001年7月06日 00:01:32 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv9655/descr/dist/src/Lib
Modified Files:
Tag: descr-branch
codecs.py
Log Message:
THought I'd start the merge with something "obviously easy". Heh.
One thing leads to another, and test_unicode.py fails now. But it's due
to this bug:
>>> "%*s" % (5, "abc")
' abc'
>>> "%*s" % (5, u"abc")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: * wants int
>>>
This *form* of test (a %*s format) didn't used to exist in test_unicode.py,
so my best guess is that this is a legit bug in the descr-branch. If so,
no reason not to check this in; perhaps the true error is somewhere in
still untouched Unicode-specific merges, though.
Index: codecs.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/codecs.py,v
retrieving revision 1.16
retrieving revision 1.16.6.1
diff -C2 -r1.16 -r1.16.6.1
*** codecs.py 2001年01月20日 19:54:20 1.16
--- codecs.py 2001年07月06日 07:01:30 1.16.6.1
***************
*** 8,12 ****
"""#"
! import struct,types,__builtin__
### Registry and builtin stateless codec functions
--- 8,12 ----
"""#"
! import struct, types, __builtin__
### Registry and builtin stateless codec functions
***************
*** 14,23 ****
try:
from _codecs import *
! except ImportError,why:
raise SystemError,\
'Failed to load the builtin codecs: %s' % why
! __all__ = ["register","lookup","open","EncodedFile","BOM","BOM_BE",
! "BOM_LE","BOM32_BE","BOM32_LE","BOM64_BE","BOM64_LE"]
### Constants
--- 14,23 ----
try:
from _codecs import *
! except ImportError, why:
raise SystemError,\
'Failed to load the builtin codecs: %s' % why
! __all__ = ["register", "lookup", "open", "EncodedFile", "BOM", "BOM_BE",
! "BOM_LE", "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE"]
### Constants
***************
*** 26,30 ****
# Byte Order Mark (BOM) and its possible values (BOM_BE, BOM_LE)
#
! BOM = struct.pack('=H',0xFEFF)
#
BOM_BE = BOM32_BE = '376円377円'
--- 26,30 ----
# Byte Order Mark (BOM) and its possible values (BOM_BE, BOM_LE)
#
! BOM = struct.pack('=H', 0xFEFF)
#
BOM_BE = BOM32_BE = '376円377円'
***************
*** 61,65 ****
"""
! def encode(self,input,errors='strict'):
""" Encodes the object input and returns a tuple (output
--- 61,65 ----
"""
! def encode(self, input, errors='strict'):
""" Encodes the object input and returns a tuple (output
***************
*** 80,84 ****
raise NotImplementedError
! def decode(self,input,errors='strict'):
""" Decodes the object input and returns a tuple (output
--- 80,84 ----
raise NotImplementedError
! def decode(self, input, errors='strict'):
""" Decodes the object input and returns a tuple (output
***************
*** 112,116 ****
class StreamWriter(Codec):
! def __init__(self,stream,errors='strict'):
""" Creates a StreamWriter instance.
--- 112,116 ----
class StreamWriter(Codec):
! def __init__(self, stream, errors='strict'):
""" Creates a StreamWriter instance.
***************
*** 135,139 ****
""" Writes the object's contents encoded to self.stream.
"""
! data, consumed = self.encode(object,self.errors)
self.stream.write(data)
--- 135,139 ----
""" Writes the object's contents encoded to self.stream.
"""
! data, consumed = self.encode(object, self.errors)
self.stream.write(data)
***************
*** 157,167 ****
pass
! def __getattr__(self,name,
!
getattr=getattr):
""" Inherit all other methods from the underlying stream.
"""
! return getattr(self.stream,name)
###
--- 157,166 ----
pass
! def __getattr__(self, name,
getattr=getattr):
""" Inherit all other methods from the underlying stream.
"""
! return getattr(self.stream, name)
###
***************
*** 169,173 ****
class StreamReader(Codec):
! def __init__(self,stream,errors='strict'):
""" Creates a StreamReader instance.
--- 168,172 ----
class StreamReader(Codec):
! def __init__(self, stream, errors='strict'):
""" Creates a StreamReader instance.
***************
*** 219,223 ****
try:
object, decodedbytes = decode(data, self.errors)
! except ValueError,why:
# This method is slow but should work under pretty much
# all conditions; at most 10 tries are made
--- 218,222 ----
try:
object, decodedbytes = decode(data, self.errors)
! except ValueError, why:
# This method is slow but should work under pretty much
# all conditions; at most 10 tries are made
***************
*** 251,255 ****
else:
line = self.stream.readline(size)
! return self.decode(line,self.errors)[0]
--- 250,254 ----
else:
line = self.stream.readline(size)
! return self.decode(line, self.errors)[0]
***************
*** 270,274 ****
else:
data = self.stream.read(sizehint)
! return self.decode(data,self.errors)[0].splitlines(1)
def reset(self):
--- 269,273 ----
else:
data = self.stream.read(sizehint)
! return self.decode(data, self.errors)[0].splitlines(1)
def reset(self):
***************
*** 282,293 ****
"""
pass
-
- def __getattr__(self,name,
getattr=getattr):
""" Inherit all other methods from the underlying stream.
"""
! return getattr(self.stream,name)
###
--- 281,291 ----
"""
pass
+ def __getattr__(self, name,
getattr=getattr):
""" Inherit all other methods from the underlying stream.
"""
! return getattr(self.stream, name)
###
***************
*** 306,310 ****
encoding = 'unknown'
! def __init__(self,stream,Reader,Writer,errors='strict'):
""" Creates a StreamReaderWriter instance.
--- 304,308 ----
encoding = 'unknown'
! def __init__(self, stream, Reader, Writer, errors='strict'):
""" Creates a StreamReaderWriter instance.
***************
*** 324,328 ****
self.errors = errors
! def read(self,size=-1):
return self.reader.read(size)
--- 322,326 ----
self.errors = errors
! def read(self, size=-1):
return self.reader.read(size)
***************
*** 336,344 ****
return self.reader.readlines(sizehint)
! def write(self,data):
return self.writer.write(data)
! def writelines(self,list):
return self.writer.writelines(list)
--- 334,342 ----
return self.reader.readlines(sizehint)
! def write(self, data):
return self.writer.write(data)
! def writelines(self, list):
return self.writer.writelines(list)
***************
*** 348,359 ****
self.reader.reset()
self.writer.reset()
-
- def __getattr__(self,name,
getattr=getattr):
""" Inherit all other methods from the underlying stream.
"""
! return getattr(self.stream,name)
###
--- 346,356 ----
self.reader.reset()
self.writer.reset()
+ def __getattr__(self, name,
getattr=getattr):
""" Inherit all other methods from the underlying stream.
"""
! return getattr(self.stream, name)
###
***************
*** 380,384 ****
file_encoding = 'unknown'
! def __init__(self,stream,encode,decode,Reader,Writer,errors='strict'):
""" Creates a StreamRecoder instance which implements a two-way
--- 377,382 ----
file_encoding = 'unknown'
! def __init__(self, stream, encode, decode, Reader, Writer,
! errors='strict'):
""" Creates a StreamRecoder instance which implements a two-way
***************
*** 412,416 ****
self.errors = errors
! def read(self,size=-1):
data = self.reader.read(size)
--- 410,414 ----
self.errors = errors
! def read(self, size=-1):
data = self.reader.read(size)
***************
*** 418,422 ****
return data
! def readline(self,size=None):
if size is None:
--- 416,420 ----
return data
! def readline(self, size=None):
if size is None:
***************
*** 427,431 ****
return data
! def readlines(self,sizehint=None):
if sizehint is None:
--- 425,429 ----
return data
! def readlines(self, sizehint=None):
if sizehint is None:
***************
*** 436,445 ****
return data.splitlines(1)
! def write(self,data):
data, bytesdecoded = self.decode(data, self.errors)
return self.writer.write(data)
! def writelines(self,list):
data = ''.join(list)
--- 434,443 ----
return data.splitlines(1)
! def write(self, data):
data, bytesdecoded = self.decode(data, self.errors)
return self.writer.write(data)
! def writelines(self, list):
data = ''.join(list)
***************
*** 452,462 ****
self.writer.reset()
! def __getattr__(self,name,
!
getattr=getattr):
""" Inherit all other methods from the underlying stream.
"""
! return getattr(self.stream,name)
### Shortcuts
--- 450,459 ----
self.writer.reset()
! def __getattr__(self, name,
getattr=getattr):
""" Inherit all other methods from the underlying stream.
"""
! return getattr(self.stream, name)
### Shortcuts
***************
*** 500,504 ****
if encoding is None:
return file
! (e,d,sr,sw) = lookup(encoding)
srw = StreamReaderWriter(file, sr, sw, errors)
# Add attributes to simplify introspection
--- 497,501 ----
if encoding is None:
return file
! (e, d, sr, sw) = lookup(encoding)
srw = StreamReaderWriter(file, sr, sw, errors)
# Add attributes to simplify introspection
***************
*** 536,540 ****
Reader, Writer = lookup(file_encoding)[2:]
sr = StreamRecoder(file,
! encode,decode,Reader,Writer,
errors)
# Add attributes to simplify introspection
--- 533,537 ----
Reader, Writer = lookup(file_encoding)[2:]
sr = StreamRecoder(file,
! encode, decode, Reader, Writer,
errors)
# Add attributes to simplify introspection
***************
*** 557,560 ****
--- 554,578 ----
res[i]=i
return res
+
+ def make_encoding_map(decoding_map):
+
+ """ Creates an encoding map from a decoding map.
+
+ If a target mapping in the decoding map occurrs multiple
+ times, then that target is mapped to None (undefined mapping),
+ causing an exception when encountered by the charmap codec
+ during translation.
+
+ One example where this happens is cp875.py which decodes
+ multiple character to \u001a.
+
+ """
+ m = {}
+ for k,v in decoding_map.items():
+ if not m.has_key(v):
+ m[v] = k
+ else:
+ m[v] = None
+ return m
### Tests