[Python-checkins] python/dist/src/Modules datetimemodule.c,1.18,1.19

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Mon, 30 Dec 2002 12:52:35 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv32300/python/Modules

Modified Files:
	datetimemodule.c 
Log Message:
A step on the way to making tzinfo classes writable by mortals:  get rid
of the timetz case.  A tzinfo method will always see a datetimetz arg,
or None, now.  In the former case, it's still possible that it will get
a datetimetz argument belonging to a different timezone.  That will get
fixed next.


Index: datetimemodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/datetimemodule.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** datetimemodule.c	27 Dec 2002 02:26:16 -0000	1.18
--- datetimemodule.c	30 Dec 2002 20:52:32 -0000	1.19
***************
*** 565,577 ****
  }
  
! /* Return tzinfo.methname(self), without any checking of results.
   * If tzinfo is None, returns None.
   */
  static PyObject *
! call_tzinfo_method(PyObject *self, PyObject *tzinfo, char *methname)
  {
  	PyObject *result;
  
! 	assert(self && tzinfo && methname);
  	assert(check_tzinfo_subclass(tzinfo) >= 0);
  	if (tzinfo == Py_None) {
--- 565,577 ----
  }
  
! /* Return tzinfo.methname(tzinfoarg), without any checking of results.
   * If tzinfo is None, returns None.
   */
  static PyObject *
! call_tzinfo_method(PyObject *tzinfo, char *methname, PyObject *tzinfoarg)
  {
  	PyObject *result;
  
! 	assert(tzinfo && methname && tzinfoarg);
  	assert(check_tzinfo_subclass(tzinfo) >= 0);
  	if (tzinfo == Py_None) {
***************
*** 580,584 ****
  	}
  	else
! 		result = PyObject_CallMethod(tzinfo, methname, "O", self);
  	return result;
  }
--- 580,584 ----
  	}
  	else
! 		result = PyObject_CallMethod(tzinfo, methname, "O", tzinfoarg);
  	return result;
  }
***************
*** 613,618 ****
  }
  
! /* Internal helper.
!  * Call getattr(tzinfo, name)(tzinfoarg), and extract an int from the
   * result.  tzinfo must be an instance of the tzinfo class.  If the method
   * returns None, this returns 0 and sets *none to 1.  If the method doesn't
--- 613,618 ----
  }
  
! 
! /* Call getattr(tzinfo, name)(tzinfoarg), and extract an int from the
   * result.  tzinfo must be an instance of the tzinfo class.  If the method
   * returns None, this returns 0 and sets *none to 1.  If the method doesn't
***************
*** 636,640 ****
  
  	*none = 0;
! 	u = call_tzinfo_method(tzinfoarg, tzinfo, name);
  	if (u == NULL)
  		return -1;
--- 636,640 ----
  
  	*none = 0;
! 	u = call_tzinfo_method(tzinfo, name, tzinfoarg);
  	if (u == NULL)
  		return -1;
***************
*** 703,711 ****
  static PyObject *new_delta(int d, int sec, int usec, int normalize);
  
! /* Call tzinfo.name(self) and return the offset as a timedelta or None. */
  static PyObject *
! offset_as_timedelta(PyObject *self, PyObject *tzinfo, char *name) {
  	PyObject *result;
  
  	if (tzinfo == Py_None) {
  		result = Py_None;
--- 703,713 ----
  static PyObject *new_delta(int d, int sec, int usec, int normalize);
  
! /* Call tzinfo.name(tzinfoarg), and return the offset as a timedelta or None.
!  */
  static PyObject *
! offset_as_timedelta(PyObject *tzinfo, char *name, PyObject *tzinfoarg) {
  	PyObject *result;
  
+ 	assert(tzinfo && name && tzinfoarg);
  	if (tzinfo == Py_None) {
  		result = Py_None;
***************
*** 714,718 ****
  	else {
  		int none;
! 		int offset = call_utc_tzinfo_method(tzinfo, name, self, &none);
  		if (offset < 0 && PyErr_Occurred())
  			return NULL;
--- 716,721 ----
  	else {
  		int none;
! 		int offset = call_utc_tzinfo_method(tzinfo, name, tzinfoarg,
! 						    &none);
  		if (offset < 0 && PyErr_Occurred())
  			return NULL;
***************
*** 741,757 ****
  }
  
! /* Call tzinfo.tzname(self), and return the result.  tzinfo must be
   * an instance of the tzinfo class or None.  If tzinfo isn't None, and
!  * tzname() doesn't return None ora string, TypeError is raised and this
   * returns NULL.
   */
  static PyObject *
! call_tzname(PyObject *self, PyObject *tzinfo)
  {
  	PyObject *result;
  
- 	assert(self != NULL);
  	assert(tzinfo != NULL);
  	assert(check_tzinfo_subclass(tzinfo) >= 0);
  
  	if (tzinfo == Py_None) {
--- 744,760 ----
  }
  
! /* Call tzinfo.tzname(tzinfoarg), and return the result.  tzinfo must be
   * an instance of the tzinfo class or None.  If tzinfo isn't None, and
!  * tzname() doesn't return None or a string, TypeError is raised and this
   * returns NULL.
   */
  static PyObject *
! call_tzname(PyObject *tzinfo, PyObject *tzinfoarg)
  {
  	PyObject *result;
  
  	assert(tzinfo != NULL);
  	assert(check_tzinfo_subclass(tzinfo) >= 0);
+ 	assert(tzinfoarg != NULL);
  
  	if (tzinfo == Py_None) {
***************
*** 760,764 ****
  	}
  	else
! 		result = PyObject_CallMethod(tzinfo, "tzname", "O", self);
  
  	if (result != NULL && result != Py_None && ! PyString_Check(result)) {
--- 763,767 ----
  	}
  	else
! 		result = PyObject_CallMethod(tzinfo, "tzname", "O", tzinfoarg);
  
  	if (result != NULL && result != Py_None && ! PyString_Check(result)) {
***************
*** 817,821 ****
  		       OFFSET_NAIVE : OFFSET_UNKNOWN;
  	}
! 	*offset = call_utcoffset(tzinfo, op, &none);
  	if (*offset == -1 && PyErr_Occurred())
  		return OFFSET_ERROR;
--- 820,826 ----
  		       OFFSET_NAIVE : OFFSET_UNKNOWN;
  	}
! 	*offset = call_utcoffset(tzinfo,
! 				 PyTimeTZ_Check(op) ? Py_None : op,
! 				 &none);
  	if (*offset == -1 && PyErr_Occurred())
  		return OFFSET_ERROR;
***************
*** 952,958 ****
   * giving special meanings to the %z and %Z format codes via a preprocessing
   * step on the format string.
   */
  static PyObject *
! wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple)
  {
  	PyObject *result = NULL;	/* guilty until proved innocent */
--- 957,966 ----
   * giving special meanings to the %z and %Z format codes via a preprocessing
   * step on the format string.
+  * tzinfoarg is the argument to pass to the object's tzinfo method, if
+  * needed.
   */
  static PyObject *
! wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
! 	      PyObject *tzinfoarg)
  {
  	PyObject *result = NULL;	/* guilty until proved innocent */
***************
*** 1032,1040 ****
  				if (zreplacement == NULL) goto Done;
  				if (tzinfo != Py_None && tzinfo != NULL) {
  					if (format_utcoffset(buf,
  							     sizeof(buf),
  							     "",
  							     tzinfo,
! 							     object) < 0)
  						goto Done;
  					Py_DECREF(zreplacement);
--- 1040,1049 ----
  				if (zreplacement == NULL) goto Done;
  				if (tzinfo != Py_None && tzinfo != NULL) {
+ 					assert(tzinfoarg != NULL);
  					if (format_utcoffset(buf,
  							     sizeof(buf),
  							     "",
  							     tzinfo,
! 							     tzinfoarg) < 0)
  						goto Done;
  					Py_DECREF(zreplacement);
***************
*** 1054,1059 ****
  				if (Zreplacement == NULL) goto Done;
  				if (tzinfo != Py_None && tzinfo != NULL) {
! 					PyObject *temp = call_tzname(object,
! 								     tzinfo);
  					if (temp == NULL) goto Done;
  					if (temp != Py_None) {
--- 1063,1069 ----
  				if (Zreplacement == NULL) goto Done;
  				if (tzinfo != Py_None && tzinfo != NULL) {
! 					PyObject *temp;
! 					assert(tzinfoarg != NULL);
! 					temp = call_tzname(tzinfo, tzinfoarg);
  					if (temp == NULL) goto Done;
  					if (temp != Py_None) {
***************
*** 2425,2429 ****
  	if (tuple == NULL)
  		return NULL;
! 	result = wrap_strftime((PyObject *)self, format, tuple);
  	Py_DECREF(tuple);
  	return result;
--- 2435,2440 ----
  	if (tuple == NULL)
  		return NULL;
! 	result = wrap_strftime((PyObject *)self, format, tuple,
! 			       (PyObject *)self);
  	Py_DECREF(tuple);
  	return result;
***************
*** 3653,3657 ****
  		return NULL;
  	assert(PyTuple_Size(tuple) == 9);
! 	result = wrap_strftime((PyObject *)self, format, tuple);
  	Py_DECREF(tuple);
  	return result;
--- 3664,3668 ----
  		return NULL;
  	assert(PyTuple_Size(tuple) == 9);
! 	result = wrap_strftime((PyObject *)self, format, tuple, Py_None);
  	Py_DECREF(tuple);
  	return result;
***************
*** 4141,4156 ****
  static PyObject *
  timetz_utcoffset(PyDateTime_TimeTZ *self, PyObject *unused) {
! 	return offset_as_timedelta((PyObject *)self, self->tzinfo,
! 				   "utcoffset");
  }
  
  static PyObject *
  timetz_dst(PyDateTime_TimeTZ *self, PyObject *unused) {
! 	return offset_as_timedelta((PyObject *)self, self->tzinfo, "dst");
  }
  
  static PyObject *
  timetz_tzname(PyDateTime_TimeTZ *self, PyObject *unused) {
! 	return call_tzname((PyObject *)self, self->tzinfo);
  }
  
--- 4152,4166 ----
  static PyObject *
  timetz_utcoffset(PyDateTime_TimeTZ *self, PyObject *unused) {
! 	return offset_as_timedelta(self->tzinfo, "utcoffset", Py_None);
  }
  
  static PyObject *
  timetz_dst(PyDateTime_TimeTZ *self, PyObject *unused) {
! 	return offset_as_timedelta(self->tzinfo, "dst", Py_None);
  }
  
  static PyObject *
  timetz_tzname(PyDateTime_TimeTZ *self, PyObject *unused) {
! 	return call_tzname(self->tzinfo, Py_None);
  }
  
***************
*** 4182,4186 ****
  	/* We need to append the UTC offset. */
  	if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo,
! 			     (PyObject *)self) < 0) {
  		Py_DECREF(result);
  		return NULL;
--- 4192,4196 ----
  	/* We need to append the UTC offset. */
  	if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo,
! 			     Py_None) < 0) {
  		Py_DECREF(result);
  		return NULL;
***************
*** 4235,4239 ****
  	offset = 0;
  	if (self->tzinfo != Py_None) {
! 		offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none);
  		if (offset == -1 && PyErr_Occurred())
  			return -1;
--- 4245,4249 ----
  	offset = 0;
  	if (self->tzinfo != Py_None) {
! 		offset = call_utcoffset(self->tzinfo, Py_None, &none);
  		if (offset == -1 && PyErr_Occurred())
  			return -1;
***************
*** 4544,4559 ****
  static PyObject *
  datetimetz_utcoffset(PyDateTime_DateTimeTZ *self, PyObject *unused) {
! 	return offset_as_timedelta((PyObject *)self, self->tzinfo,
! 				   "utcoffset");
  }
  
  static PyObject *
  datetimetz_dst(PyDateTime_DateTimeTZ *self, PyObject *unused) {
! 	return offset_as_timedelta((PyObject *)self, self->tzinfo, "dst");
  }
  
  static PyObject *
  datetimetz_tzname(PyDateTime_DateTimeTZ *self, PyObject *unused) {
! 	return call_tzname((PyObject *)self, self->tzinfo);
  }
  
--- 4554,4569 ----
  static PyObject *
  datetimetz_utcoffset(PyDateTime_DateTimeTZ *self, PyObject *unused) {
! 	return offset_as_timedelta(self->tzinfo, "utcoffset",
! 				   (PyObject *)self);
  }
  
  static PyObject *
  datetimetz_dst(PyDateTime_DateTimeTZ *self, PyObject *unused) {
! 	return offset_as_timedelta(self->tzinfo, "dst", (PyObject *)self);
  }
  
  static PyObject *
  datetimetz_tzname(PyDateTime_DateTimeTZ *self, PyObject *unused) {
! 	return call_tzname(self->tzinfo, (PyObject *)self);
  }