[Checkins] SVN: zope.httpform/trunk/ 100% test coverage of the parser module. :-) Also:

Shane Hathaway shane at hathawaymix.org
Sun Jun 14 03:42:25 EDT 2009


Log message for revision 100938:
  100% test coverage of the parser module. :-)  Also:
  
  - Default to UTF-8 decoding
  
  - Fixed a test failure on Python 2.6: cgi.FieldStorage now parses
    the query string even on POST.  We shouldn't be testing things
    in other libraries.
  
  

Changed:
  U   zope.httpform/trunk/CHANGES.txt
  U   zope.httpform/trunk/src/zope/httpform/README.txt
  U   zope.httpform/trunk/src/zope/httpform/parser.py

-=-
Modified: zope.httpform/trunk/CHANGES.txt
===================================================================
--- zope.httpform/trunk/CHANGES.txt	2009-06-14 03:13:12 UTC (rev 100937)
+++ zope.httpform/trunk/CHANGES.txt	2009-06-14 07:42:25 UTC (rev 100938)
@@ -4,8 +4,10 @@
 Version 1.0.2 (unreleased)
 --------------------------
 
-- ...
+- Default to UTF-8 decoding
 
+- Fixed a test failure on Python 2.6
+
 Version 1.0.1 (2009-02-07)
 --------------------------
 

Modified: zope.httpform/trunk/src/zope/httpform/README.txt
===================================================================
--- zope.httpform/trunk/src/zope/httpform/README.txt	2009-06-14 03:13:12 UTC (rev 100937)
+++ zope.httpform/trunk/src/zope/httpform/README.txt	2009-06-14 07:42:25 UTC (rev 100938)
@@ -407,12 +407,6 @@
     >>> pprint(parse(env))
     {u'x': 1, u'y': 2}
 
-The query string is ignored on POST.
-
-    >>> env = {'REQUEST_METHOD': 'POST', 'QUERY_STRING': 'x=1'}
-    >>> pprint(parse(env))
-    {}
-
 No form parsing is done for content types that don't look like forms.
 
     >>> input_fp = StringIO("x:int=1&y:int=2")

Modified: zope.httpform/trunk/src/zope/httpform/parser.py
===================================================================
--- zope.httpform/trunk/src/zope/httpform/parser.py	2009-06-14 03:13:12 UTC (rev 100937)
+++ zope.httpform/trunk/src/zope/httpform/parser.py	2009-06-14 07:42:25 UTC (rev 100938)
@@ -64,12 +64,15 @@
 REC = RECORD | RECORDS
 CONVERTED = 32
 
+def decode_utf8(s):
+    """Decode a UTF-8 string"""
+    return unicode(s, 'utf-8')
 
 class FormParser(object):
     """Form data parser."""
     implements(IFormParser)
 
-    def __init__(self, env, wsgi_input=None, to_unicode=None):
+    def __init__(self, env, wsgi_input=None, to_unicode=decode_utf8):
         """Create a form parser for the given WSGI or CGI environment.
 
         The wsgi_input parameter provides the request input stream.
@@ -77,16 +80,13 @@
         the request input stream from 'wsgi.input' in the environment.
 
         If to_unicode is specified, it is the function to use
-        to convert input byte strings to Unicode.
+        to convert input byte strings to Unicode.  Otherwise, UTF-8
+        encoding is assumed.
         """
         self._env = env
         if wsgi_input is None:
             wsgi_input = env.get('wsgi.input')
         self._wsgi_input = wsgi_input
-        if to_unicode is None:
-            # use the default encoding
-            def to_unicode(s):
-                return s.decode()
         self._to_unicode = to_unicode
 
     def parse(self):
@@ -410,15 +410,10 @@
     def __init__(self, field_storage):
 
         f = field_storage.file
-        if hasattr(f, '__methods__'):
-            methods = f.__methods__
-        else:
-            methods = ['close', 'fileno', 'flush', 'isatty',
-                'read', 'readline', 'readlines', 'seek',
-                'tell', 'truncate', 'write', 'writelines',
-                'name']
-
         d = self.__dict__
+        methods = ['close', 'fileno', 'flush', 'isatty',
+            'read', 'readline', 'readlines', 'seek',
+            'tell', 'truncate', 'write', 'writelines', 'name']
         for m in methods:
             if hasattr(f, m):
                 d[m] = getattr(f, m)
@@ -427,6 +422,6 @@
         self.filename = unicode(field_storage.filename, 'UTF-8')
 
 
-def parse(env, wsgi_input=None, to_unicode=None):
+def parse(env, wsgi_input=None, to_unicode=decode_utf8):
     """Shortcut for creating a FormParser and calling the parse() method."""
     return FormParser(env, wsgi_input, to_unicode).parse()



More information about the Checkins mailing list