[Zope3-checkins] CVS: Zope3/src/pythonlib/compat22 - csv.py:1.2

Fred L. Drake, Jr. fred at zope.com
Tue Sep 2 13:04:48 EDT 2003


Update of /cvs-repository/Zope3/src/pythonlib/compat22
In directory cvs.zope.org:/tmp/cvs-serv2642

Modified Files:
	csv.py 
Log Message:
update to revision 1.8 from Python's CVS:
- remove uses of eval()
- tighten up what exceptions are caught
- guard against Python build's without complex()
- use long names for re compilation options


=== Zope3/src/pythonlib/compat22/csv.py 1.1 => 1.2 ===
--- Zope3/src/pythonlib/compat22/csv.py:1.1	Mon Jun  9 13:39:21 2003
+++ Zope3/src/pythonlib/compat22/csv.py	Tue Sep  2 12:04:48 2003
@@ -148,6 +148,11 @@
             rows.append(self._dict_to_list(rowdict))
         return self.writer.writerows(rows)
 
+# Guard Sniffer's type checking against builds that exclude complex()
+try:
+    complex
+except NameError:
+    complex = float
 
 class Sniffer:
     '''
@@ -202,7 +207,7 @@
                       '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)',   #  ".*?",
                       '(?P<delim>>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)',  # ,".*?"
                       '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)'):                            #  ".*?" (no delim, no space)
-            regexp = re.compile(restr, re.S | re.M)
+            regexp = re.compile(restr, re.DOTALL | re.MULTILINE)
             matches = regexp.findall(data)
             if matches:
                 break
@@ -360,13 +365,6 @@
         # Finally, a 'vote' is taken at the end for each column, adding or
         # subtracting from the likelihood of the first row being a header.
 
-        def seval(item):
-            """
-            Strips parens from item prior to calling eval in an
-            attempt to make it safer
-            """
-            return eval(item.replace('(', '').replace(')', ''))
-
         rdr = reader(StringIO(sample), self.sniff(sample))
 
         header = rdr.next() # assume first row is header
@@ -386,18 +384,21 @@
                 continue # skip rows that have irregular number of columns
 
             for col in columnTypes.keys():
-                try:
+
+                for thisType in [int, long, float, complex]:
                     try:
-                        # is it a built-in type (besides string)?
-                        thisType = type(seval(row[col]))
-                    except OverflowError:
-                        # a long int?
-                        thisType = type(seval(row[col] + 'L'))
-                        thisType = type(0) # treat long ints as int
-                except:
+                        thisType(row[col])
+                        break
+                    except (ValueError, OverflowError):
+                        pass
+                else:
                     # fallback to length of string
                     thisType = len(row[col])
 
+                # treat longs as ints
+                if thisType == long:
+                    thisType = int
+
                 if thisType != columnTypes[col]:
                     if columnTypes[col] is None: # add new column type
                         columnTypes[col] = thisType
@@ -417,8 +418,8 @@
                     hasHeader -= 1
             else: # attempt typecast
                 try:
-                    eval("%s(%s)" % (colType.__name__, header[col]))
-                except:
+                    colType(header[col])
+                except (ValueError, TypeError):
                     hasHeader += 1
                 else:
                     hasHeader -= 1




More information about the Zope3-Checkins mailing list