[Zope-Checkins] CVS: Zope/lib/python/ZPublisher - Converters.py:1.16

Toby Dickenson tdickenson@geminidataloggers.com
Tue, 7 May 2002 13:54:31 -0400


Update of /cvs-repository/Zope/lib/python/ZPublisher
In directory cvs.zope.org:/tmp/cvs-serv19454

Modified Files:
	Converters.py 
Log Message:
refactored much code that conditionally called v.read(). Also, allow the input to a converter to be a unicode string. This is not used by ZPublisher, but is helpful for other code that uses the converters such as OFS.PropertyManager

=== Zope/lib/python/ZPublisher/Converters.py 1.15 => 1.16 ===
     if hasattr(v,'read'): return v.read()
     elif isinstance(v,UnicodeType) :
-        return v
+        return v.encode('latin1')
     else:  
         return str(v)
 
 def field2text(v, nl=re.compile('\r\n|\n\r').search):
-    if hasattr(v,'read'): v=v.read()
-    else: v=str(v)
+    v = field2string(v)
     mo = nl(v)
     if mo is None: return v
     l = mo.start(0)
@@ -42,16 +41,14 @@
     return '\n'.join(r)
 
 def field2required(v):
-    if hasattr(v,'read'): v=v.read()
-    else: v=str(v)
+    v = field2string(v)
     if v.strip(): return v
     raise ValueError, 'No input for required field<p>'
 
 def field2int(v):
     if type(v) in (ListType, TupleType):
         return map(field2int, v)
-    if hasattr(v,'read'): v=v.read()
-    else: v=str(v)
+    v = field2string(v)
     if v:
         try: return int(v)
         except ValueError:
@@ -63,8 +60,7 @@
 def field2float(v):
     if type(v) in (ListType, TupleType):
         return map(field2float, v)
-    if hasattr(v,'read'): v=v.read()
-    else: v=str(v)
+    v = field2string(v)
     if v:
         try: return float(v)
         except ValueError:
@@ -77,9 +73,7 @@
 def field2long(v):
     if type(v) in (ListType, TupleType):
         return map(field2long, v)
-    if hasattr(v,'read'): v=v.read()
-    else: v=str(v)
-
+    v = field2string(v)
     # handle trailing 'L' if present.
     if v[-1:] in ('L', 'l'):
         v = v[:-1]
@@ -92,8 +86,7 @@
     raise ValueError, 'Empty entry when <strong>integer</strong> expected'
 
 def field2tokens(v):
-    if hasattr(v,'read'): v=v.read()
-    else: v=str(v)
+    v = field2string(v)
     return v.split()
 
 def field2lines(v):
@@ -106,12 +99,11 @@
 
 def field2date(v):
     from DateTime import DateTime
-    if hasattr(v,'read'): v=v.read()
-    else: v=str(v)
+    v = field2string(v)
     return DateTime(v)
 
 def field2boolean(v):
-    return v
+    return not not v
 
 class _unicode_converter:
     def __call__(self,v):