[Zope-Checkins] SVN: Zope/trunk/ Collector #1490 - Added a new zope.conf option to control the character set used to encode unicode data that reaches ZPublisher without any specified encoding.

Chris Withers chris at simplistix.co.uk
Sat Oct 8 14:26:17 EDT 2005


Log message for revision 38974:
  Collector #1490 - Added a new zope.conf option to control the character set used to encode unicode data that reaches ZPublisher without any specified encoding.

Changed:
  U   Zope/trunk/doc/CHANGES.txt
  U   Zope/trunk/lib/python/ZPublisher/Converters.py
  U   Zope/trunk/lib/python/ZPublisher/HTTPRequest.py
  U   Zope/trunk/lib/python/ZPublisher/HTTPResponse.py
  U   Zope/trunk/lib/python/Zope2/Startup/datatypes.py
  U   Zope/trunk/lib/python/Zope2/Startup/zopeschema.xml
  U   Zope/trunk/skel/etc/zope.conf.in

-=-
Modified: Zope/trunk/doc/CHANGES.txt
===================================================================
--- Zope/trunk/doc/CHANGES.txt	2005-10-08 16:59:24 UTC (rev 38973)
+++ Zope/trunk/doc/CHANGES.txt	2005-10-08 18:26:16 UTC (rev 38974)
@@ -26,6 +26,10 @@
 
     Features added
 
+      - Collector #1490: Added a new zope.conf option to control the
+        character set used to encode unicode data that reaches
+        ZPublisher without any specified encoding.
+
       - AccessControl, Acquisition, App, OFS, webdav, PluginIndexes,
         ZCatalog and ZCTextIndex: Added some Zope 3 style interfaces.
         This makes the bridged interfaces shipped with Five obsolete.

Modified: Zope/trunk/lib/python/ZPublisher/Converters.py
===================================================================
--- Zope/trunk/lib/python/ZPublisher/Converters.py	2005-10-08 16:59:24 UTC (rev 38973)
+++ Zope/trunk/lib/python/ZPublisher/Converters.py	2005-10-08 18:26:16 UTC (rev 38974)
@@ -17,10 +17,13 @@
 from DateTime import DateTime
 from cgi import escape
 
+# This may get overwritten during configuration
+default_encoding = 'iso-8859-15'
+
 def field2string(v):
     if hasattr(v,'read'): return v.read()
-    elif isinstance(v,UnicodeType) :
-        return v.encode('iso-8859-15')
+    elif isinstance(v,UnicodeType):
+        return v.encode(default_encoding)
     else:
         return str(v)
 

Modified: Zope/trunk/lib/python/ZPublisher/HTTPRequest.py
===================================================================
--- Zope/trunk/lib/python/ZPublisher/HTTPRequest.py	2005-10-08 16:59:24 UTC (rev 38973)
+++ Zope/trunk/lib/python/ZPublisher/HTTPRequest.py	2005-10-08 18:26:16 UTC (rev 38974)
@@ -25,6 +25,9 @@
 from maybe_lock import allocate_lock
 xmlrpc=None # Placeholder for module that we'll import if we have to.
 
+# This may get overwritten during configuration
+default_encoding = 'iso-8859-15'
+
 isCGI_NAME = {
         'SERVER_SOFTWARE' : 1,
         'SERVER_NAME' : 1,
@@ -522,7 +525,7 @@
                                 if hasattr(converter,'convert_unicode'):
                                     item = converter.convert_unicode(item)
                                 else:
-                                    item = converter(item.encode('iso-8859-15'))
+                                    item = converter(item.encode(default_encoding))
                             else:
                                 item=converter(item)
 

Modified: Zope/trunk/lib/python/ZPublisher/HTTPResponse.py
===================================================================
--- Zope/trunk/lib/python/ZPublisher/HTTPResponse.py	2005-10-08 16:59:24 UTC (rev 38973)
+++ Zope/trunk/lib/python/ZPublisher/HTTPResponse.py	2005-10-08 18:26:16 UTC (rev 38974)
@@ -26,6 +26,8 @@
 
 nl2sp = maketrans('\n',' ')
 
+# This may get overwritten during configuration
+default_encoding = 'iso-8859-15'
 
 # Enable APPEND_TRACEBACKS to make Zope append tracebacks like it used to,
 # but a better solution is to make standard_error_message display error_tb.
@@ -444,7 +446,7 @@
                 encoding = match.group(1)
                 return body.encode(encoding)
         # Use the default character encoding
-        return body.encode('iso-8859-15','replace')
+        return body.encode(default_encoding,'replace')
 
     def setBase(self,base):
         """Set the base URL for the returned document.

Modified: Zope/trunk/lib/python/Zope2/Startup/datatypes.py
===================================================================
--- Zope/trunk/lib/python/Zope2/Startup/datatypes.py	2005-10-08 16:59:24 UTC (rev 38973)
+++ Zope/trunk/lib/python/Zope2/Startup/datatypes.py	2005-10-08 18:26:16 UTC (rev 38974)
@@ -211,3 +211,13 @@
                 return (real_root, real_path, container_class)
         raise LookupError('Nothing known about mount path %s' % mount_path)
     
+def default_zpublisher_encoding(value):
+    # This is a bit clunky but necessary :-(
+    # These modules are imported during the configuration process
+    # so a module-level call to getConfiguration in any of them
+    # results in getting config data structure without the necessary
+    # value in it.
+    from ZPublisher import Converters, HTTPRequest, HTTPResponse
+    Converters.default_encoding = value
+    HTTPRequest.default_encoding = value
+    HTTPResponse.default_encoding = value

Modified: Zope/trunk/lib/python/Zope2/Startup/zopeschema.xml
===================================================================
--- Zope/trunk/lib/python/Zope2/Startup/zopeschema.xml	2005-10-08 16:59:24 UTC (rev 38973)
+++ Zope/trunk/lib/python/Zope2/Startup/zopeschema.xml	2005-10-08 18:26:16 UTC (rev 38974)
@@ -813,5 +813,13 @@
 
   <section type="zoperunner" name="*" attribute="runner"/>
 
+  <key name="default-zpublisher-encoding" datatype=".default_zpublisher_encoding">
+     <description>
+       This key controls what character set is used to encode unicode
+       data that reaches ZPublisher without any other specified encoding.
+     </description>
+     <metadefault>iso-8859-15</metadefault>
+  </key>
 
+
 </schema>

Modified: Zope/trunk/skel/etc/zope.conf.in
===================================================================
--- Zope/trunk/skel/etc/zope.conf.in	2005-10-08 16:59:24 UTC (rev 38973)
+++ Zope/trunk/skel/etc/zope.conf.in	2005-10-08 18:26:16 UTC (rev 38974)
@@ -848,6 +848,17 @@
 #
 #    large-file-threshold 1Mb
 
+# Directive: default_zpublisher_encoding
+#
+# Description:
+#     This controls what character set is used to encode unicode
+#     data that reaches ZPublisher without any other specified encoding.
+#
+# Default: iso-8859-15
+#
+# Example:
+#
+#    default_zpublisher_encoding utf-8
 
 # Directives: servers
 #



More information about the Zope-Checkins mailing list