Changeset 1087
- Timestamp:
- 05/02/06 17:12:30
- Files:
-
- trunk/cherrypy/lib/encodings.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/lib/encodings.py
r1078 r1087 50 50 # Encoding 51 51 52 def encode(encoding=None ):52 def encode(encoding=None, errors='strict'): 53 53 ct = cherrypy.response.headers.elements("Content-Type") 54 54 if ct: … … 56 56 if ct.value.lower().startswith("text/"): 57 57 # Set "charset=..." param on response Content-Type header 58 ct.params['charset'] = find_acceptable_charset(encoding )58 ct.params['charset'] = find_acceptable_charset(encoding, errors=errors) 59 59 cherrypy.response.headers["Content-Type"] = str(ct) 60 60 61 def encode_stream(encoding ):61 def encode_stream(encoding, errors='strict'): 62 62 """Encode a streaming response body. 63 63 … … 67 67 def encoder(body): 68 68 for line in body: 69 yield line.encode(encoding )69 yield line.encode(encoding, errors) 70 70 cherrypy.response.body = encoder(cherrypy.response.body) 71 71 return True 72 72 73 def encode_string(encoding ):73 def encode_string(encoding, errors='strict'): 74 74 """Encode a buffered response body.""" 75 75 try: 76 76 body = [] 77 77 for chunk in cherrypy.response.body: 78 body.append(chunk.encode(encoding ))78 body.append(chunk.encode(encoding, errors)) 79 79 cherrypy.response.body = body 80 80 except UnicodeError: … … 83 83 return True 84 84 85 def find_acceptable_charset(encoding=None, default_encoding='utf-8' ):85 def find_acceptable_charset(encoding=None, default_encoding='utf-8', errors='strict'): 86 86 response = cherrypy.response 87 87 … … 99 99 if encoding is not None: 100 100 # If specified, force this encoding to be used, or fail. 101 if encoder(encoding ):101 if encoder(encoding, errors): 102 102 return encoding 103 103 else: … … 110 110 # Any character-set is acceptable. 111 111 charsets = [] 112 if encoder(default_encoding ):112 if encoder(default_encoding, errors): 113 113 return default_encoding 114 114 else: … … 124 124 if iso not in charsets: 125 125 attempted_charsets.append(iso) 126 if encoder(iso ):126 if encoder(iso, errors): 127 127 return iso 128 128 … … 133 133 if default_encoding not in attempted_charsets: 134 134 attempted_charsets.append(default_encoding) 135 if encoder(default_encoding ):135 if encoder(default_encoding, errors): 136 136 return default_encoding 137 137 else: … … 139 139 if encoding not in attempted_charsets: 140 140 attempted_charsets.append(encoding) 141 if encoder(encoding ):141 if encoder(encoding, errors): 142 142 return encoding 143 143

