[Python-checkins] python/nondist/sandbox/csv _csv.c,1.33,1.34

davecole@users.sourceforge.net davecole@users.sourceforge.net
Sun, 16 Feb 2003 03:25:19 -0800


Update of /cvsroot/python/python/nondist/sandbox/csv
In directory sc8-pr-cvs1:/tmp/cvs-serv24912

Modified Files:
	_csv.c 
Log Message:
Get Python to determine whether or not field data is numeric when
quoting is QUOTE_NONNUMERIC by trying to convert to float.


Index: _csv.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/csv/_csv.c,v
retrieving revision 1.33
retrieving revision 1.34
diff -C2 -d -r1.33 -r1.34
*** _csv.c	16 Feb 2003 11:04:58 -0000	1.33
--- _csv.c	16 Feb 2003 11:25:17 -0000	1.34
***************
*** 880,887 ****
  			*quoted = 1;
  			rec_len++;
! 		} else if (dialect->quoting == QUOTE_NONNUMERIC && 
!                            !*quoted && 
!                            !(isdigit(c) || c == '+' || c == '-' || c == '.'))
! 			*quoted = 1;
  
  		/* Some special characters need to be escaped.  If we have a
--- 880,884 ----
  			*quoted = 1;
  			rec_len++;
! 		}
  
  		/* Some special characters need to be escaped.  If we have a
***************
*** 965,974 ****
  
  static int
! join_append(WriterObj *self, char *field, int quote_empty)
  {
! 	int rec_len, quoted;
  
! 	quoted = 0;
! 	rec_len = join_append_data(self, field, quote_empty, &quoted, 0);
  	if (rec_len < 0)
  		return 0;
--- 962,970 ----
  
  static int
! join_append(WriterObj *self, char *field, int *quoted, int quote_empty)
  {
! 	int rec_len;
  
! 	rec_len = join_append_data(self, field, quote_empty, quoted, 0);
  	if (rec_len < 0)
  		return 0;
***************
*** 978,982 ****
  		return 0;
  
! 	self->rec_len = join_append_data(self, field, quote_empty, &quoted, 1);
  	self->num_fields++;
  
--- 974,978 ----
  		return 0;
  
! 	self->rec_len = join_append_data(self, field, quote_empty, quoted, 1);
  	self->num_fields++;
  
***************
*** 1012,1015 ****
--- 1008,1012 ----
  csv_writerow(WriterObj *self, PyObject *seq)
  {
+         DialectObj *dialect = self->dialect;
  	int len, i;
  
***************
*** 1027,1030 ****
--- 1024,1028 ----
  		PyObject *field;
  		int append_ok;
+ 		int quoted;
  
  		field = PySequence_GetItem(seq, i);
***************
*** 1032,1042 ****
  			return NULL;
  
  		if (PyString_Check(field)) {
  			append_ok = join_append(self, PyString_AsString(field),
!                                                 len == 1);
  			Py_DECREF(field);
  		}
  		else if (field == Py_None) {
! 			append_ok = join_append(self, "", len == 1);
  			Py_DECREF(field);
  		}
--- 1030,1053 ----
  			return NULL;
  
+ 		quoted = 0;
+ 		if (dialect->quoting == QUOTE_NONNUMERIC) {
+ 			PyObject *num;
+ 
+ 			num = PyNumber_Float(field);
+ 			if (num == NULL) {
+ 				quoted = 1;
+ 				PyErr_Clear();
+ 			} else {
+ 				Py_DECREF(num);
+ 			}
+ 		}
+ 
  		if (PyString_Check(field)) {
  			append_ok = join_append(self, PyString_AsString(field),
!                                                 &quoted, len == 1);
  			Py_DECREF(field);
  		}
  		else if (field == Py_None) {
! 			append_ok = join_append(self, "", &quoted, len == 1);
  			Py_DECREF(field);
  		}
***************
*** 1050,1054 ****
  
  			append_ok = join_append(self, PyString_AsString(str), 
!                                                 len == 1);
  			Py_DECREF(str);
  		}
--- 1061,1065 ----
  
  			append_ok = join_append(self, PyString_AsString(str), 
!                                                 &quoted, len == 1);
  			Py_DECREF(str);
  		}