[Python-checkins] python/nondist/sandbox/datetime obj_delta.c,1.11,1.12

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Sun, 01 Dec 2002 16:49:52 -0800


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

Modified Files:
	obj_delta.c 
Log Message:
accum() and delta_new():  just skip over keyword args that weren't given.
This is quicker in almost all cases, and gets rid of a wart in accum().


Index: obj_delta.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_delta.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** obj_delta.c	2 Dec 2002 00:41:33 -0000	1.11
--- obj_delta.c	2 Dec 2002 00:49:50 -0000	1.12
***************
*** 339,344 ****
   * microseconds left over (this can happen if num is a float type) are
   * added into *leftover.
-  * If num is NULL, no computation is done, and sofar is returned (after
-  * incrementing its refcount).
   * Note that there are many ways this can give an error (NULL) return.
   */
--- 339,342 ----
***************
*** 350,357 ****
  	PyObject *sum;
  
! 	if (num == NULL) {
! 		Py_INCREF(sofar);
! 		return sofar;
! 	}
  
  	if (PyInt_Check(num) || PyLong_Check(num)) {
--- 348,352 ----
  	PyObject *sum;
  
! 	assert(num != NULL);
  
  	if (PyInt_Check(num) || PyLong_Check(num)) {
***************
*** 444,453 ****
  	PyObject *week = NULL;
  
! 	PyObject *x = NULL;
! 	PyObject *y = NULL;
  	double leftover_us = 0.0;
  
- 	PyObject *one;
- 
  	static char *keywords[] = {
  		"days", "seconds", "microseconds", "milliseconds",
--- 439,446 ----
  	PyObject *week = NULL;
  
! 	PyObject *x = NULL;	/* running sum of microseconds */
! 	PyObject *y = NULL;	/* temp sum of microseconds */
  	double leftover_us = 0.0;
  
  	static char *keywords[] = {
  		"days", "seconds", "microseconds", "milliseconds",
***************
*** 465,514 ****
  		goto Done;
  
! 	one = PyInt_FromLong(1);
! 	if (one == NULL)
! 		goto Done;
! 	y = accum("microseconds", x, us, one, &leftover_us);
! 	Py_DECREF(one);
! 	Py_DECREF(x);
! 	x = y;
! 	if (x == NULL)
! 		goto Done;
! 
! 	y = accum("milliseconds", x, ms, us_per_ms, &leftover_us);
! 	Py_DECREF(x);
! 	x = y;
! 	if (x == NULL)
! 		goto Done;
! 
! 	y = accum("seconds", x, second, us_per_second, &leftover_us);
! 	Py_DECREF(x);
! 	x = y;
! 	if (x == NULL)
! 		goto Done;
! 
! 	y = accum("minutes", x, minute, us_per_minute, &leftover_us);
! 	Py_DECREF(x);
! 	x = y;
! 	if (x == NULL)
! 		goto Done;
! 
! 	y = accum("hours", x, hour, us_per_hour, &leftover_us);
! 	Py_DECREF(x);
! 	x = y;
! 	if (x == NULL)
! 		goto Done;
! 
! 	y = accum("days", x, day, us_per_day, &leftover_us);
! 	Py_DECREF(x);
! 	x = y;
! 	if (x == NULL)
! 		goto Done;
! 
! 	y = accum("weeks", x, week, us_per_week, &leftover_us);
! 	Py_DECREF(x);
! 	x = y;
! 	if (x == NULL)
! 		goto Done;
! 
  	if (leftover_us) {
  		/* Round to nearest whole # of us, and add into x. */
--- 458,514 ----
  		goto Done;
  
! 	if (us) {
! 		PyObject *one = PyInt_FromLong(1);
! 		if (one == NULL)
! 			goto Done;
! 		y = accum("microseconds", x, us, one, &leftover_us);
! 		Py_DECREF(one);
! 		Py_DECREF(x);
! 		x = y;
! 		if (x == NULL)
! 			goto Done;
! 	}
! 	if (ms) {
! 		y = accum("milliseconds", x, ms, us_per_ms, &leftover_us);
! 		Py_DECREF(x);
! 		x = y;
! 		if (x == NULL)
! 			goto Done;
! 	}
! 	if (second) {
! 		y = accum("seconds", x, second, us_per_second, &leftover_us);
! 		Py_DECREF(x);
! 		x = y;
! 		if (x == NULL)
! 			goto Done;
! 	}
! 	if (minute) {
! 		y = accum("minutes", x, minute, us_per_minute, &leftover_us);
! 		Py_DECREF(x);
! 		x = y;
! 		if (x == NULL)
! 			goto Done;
! 	}
! 	if (hour) {
! 		y = accum("hours", x, hour, us_per_hour, &leftover_us);
! 		Py_DECREF(x);
! 		x = y;
! 		if (x == NULL)
! 			goto Done;
! 	}
! 	if (day) {
! 		y = accum("days", x, day, us_per_day, &leftover_us);
! 		Py_DECREF(x);
! 		x = y;
! 		if (x == NULL)
! 			goto Done;
! 	}
! 	if (week) {
! 		y = accum("weeks", x, week, us_per_week, &leftover_us);
! 		Py_DECREF(x);
! 		x = y;
! 		if (x == NULL)
! 			goto Done;
! 	}
  	if (leftover_us) {
  		/* Round to nearest whole # of us, and add into x. */