[Python-checkins] python/nondist/sandbox/datetime obj_datetimetz.c,1.5,1.6 obj_timetz.c,1.23,1.24 test_both.py,1.72,1.73

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Fri, 13 Dec 2002 16:19:38 -0800


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

Modified Files:
	obj_datetimetz.c obj_timetz.c test_both.py 
Log Message:
Added datetimetz pickle support, so far untested.
Added tzinfo error-checking to datetimetz constructor, & added test.


Index: obj_datetimetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetimetz.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** obj_datetimetz.c	13 Dec 2002 22:19:25 -0000	1.5
--- obj_datetimetz.c	14 Dec 2002 00:19:36 -0000	1.6
***************
*** 48,51 ****
--- 48,53 ----
  		if (check_time_args(hour, minute, second, usecond) < 0)
  			return NULL;
+ 		if (check_tzinfo_subclass(tzinfo, "tzinfo argument") < 0)
+ 			return NULL;
  		self = new_datetimetz(year, month, day,
  				      hour, minute, second, usecond,
***************
*** 447,457 ****
  }
  
! /* Pickle support.  Quite a maze! */
  
  static PyObject *
  datetimetz_getstate(PyDateTime_DateTimeTZ *self)
  {
! 	return PyString_FromStringAndSize(self->data,
! 					  _PyDateTime_DATETIME_DATASIZE);
  }
  
--- 449,475 ----
  }
  
! /*
!  * Pickle support.  Quite a maze!
!  */
  
+ /* Let basestate be the state string returned by datetime_getstate.
+  * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo).
+  * So it's a tuple in any (non-error) case.
+  */
  static PyObject *
  datetimetz_getstate(PyDateTime_DateTimeTZ *self)
  {
! 	PyObject *basestate;
! 	PyObject *result = NULL;
! 
! 	basestate = datetime_getstate((PyDateTime_DateTime *)self);
! 	if (basestate != NULL) {
! 		if (self->tzinfo == Py_None)
! 			result = Py_BuildValue("(O)", basestate);
! 		else
! 			result = Py_BuildValue("OO", basestate, self->tzinfo);
! 		Py_DECREF(basestate);
! 	}
! 	return result;
  }
  
***************
*** 459,473 ****
  datetimetz_setstate(PyDateTime_DateTimeTZ *self, PyObject *state)
  {
! 	const int len = PyString_Size(state);
! 	unsigned char *pdata = (unsigned char*)PyString_AsString(state);
  
! 	if (! PyString_Check(state) ||
! 	    len != _PyDateTime_DATETIME_DATASIZE) {
! 		PyErr_SetString(PyExc_TypeError,
! 				"bad argument to datetime.__setstate__");
  		return NULL;
! 	}
! 	memcpy(self->data, pdata, _PyDateTime_DATETIME_DATASIZE);
! 	self->hashcode = -1;
  
  	Py_INCREF(Py_None);
--- 477,496 ----
  datetimetz_setstate(PyDateTime_DateTimeTZ *self, PyObject *state)
  {
! 	PyObject *temp;
! 	PyObject *basestate;
! 	PyObject *tzinfo = Py_None;
  
! 	if (! PyArg_ParseTuple(state, "O!|O:__setstate__",
! 			       &PyString_Type, &basestate,
! 			       &tzinfo))
  		return NULL;
! 	temp = datetime_setstate((PyDateTime_DateTime *)self, basestate);
! 	if (temp == NULL)
! 		return NULL;
! 	Py_DECREF(temp);
! 
! 	Py_INCREF(tzinfo);
! 	Py_XDECREF(self->tzinfo);
! 	self->tzinfo = tzinfo;
  
  	Py_INCREF(Py_None);
***************
*** 475,492 ****
  }
  
- /* XXX This seems a ridiculously inefficient way to pickle a short string. */
  static PyObject *
! datetimetz_pickler(PyObject *module, PyDateTime_DateTimeTZ *datetime)
  {
  	PyObject *state;
  	PyObject *result = NULL;
  
! 	if (! PyDateTimeTZ_CheckExact(datetime)) {
  		PyErr_Format(PyExc_TypeError,
! 			     "bad type passed to datetime pickler: %s",
! 			     datetime->ob_type->tp_name);
  		return NULL;
  	}
! 	state = datetimetz_getstate(datetime);
  	if (state) {
  		result = Py_BuildValue("O(O)",
--- 498,514 ----
  }
  
  static PyObject *
! datetimetz_pickler(PyObject *module, PyDateTime_DateTimeTZ *datetimetz)
  {
  	PyObject *state;
  	PyObject *result = NULL;
  
! 	if (! PyDateTimeTZ_CheckExact(datetimetz)) {
  		PyErr_Format(PyExc_TypeError,
! 			     "bad type passed to datetimetz pickler: %s",
! 			     datetimetz->ob_type->tp_name);
  		return NULL;
  	}
! 	state = datetimetz_getstate(datetimetz);
  	if (state) {
  		result = Py_BuildValue("O(O)",
***************
*** 503,515 ****
  	PyDateTime_DateTimeTZ *self;
  
! 	if (! PyString_CheckExact(arg)) {
! 		PyErr_Format(PyExc_TypeError,
! 			     "bad type passed to datetime unpickler: %s",
! 			     arg->ob_type->tp_name);
! 		return NULL;
! 	}
! 	self = PyObject_New(PyDateTime_DateTimeTZ, &PyDateTime_DateTimeType);
  	if (self != NULL) {
! 		PyObject *res = datetimetz_setstate(self, arg);
  		if (res == NULL) {
  			Py_DECREF(self);
--- 525,534 ----
  	PyDateTime_DateTimeTZ *self;
  
! 	self = PyObject_New(PyDateTime_DateTimeTZ, &PyDateTime_DateTimeTZType);
  	if (self != NULL) {
! 		PyObject *res;
! 
! 		self->tzinfo = NULL;
! 		res = datetimetz_setstate(self, arg);
  		if (res == NULL) {
  			Py_DECREF(self);
***************
*** 520,523 ****
--- 539,543 ----
  	return (PyObject *)self;
  }
+ 
  
  static PyMethodDef datetimetz_methods[] = {

Index: obj_timetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_timetz.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -d -r1.23 -r1.24
*** obj_timetz.c	13 Dec 2002 21:29:25 -0000	1.23
--- obj_timetz.c	14 Dec 2002 00:19:36 -0000	1.24
***************
*** 241,248 ****
  }
  
! /* Pickle support.  Quite a maze! */
  
  /* Let basestate be the state string returned by time_getstate.
!  * If tzinfo is None, this returns (basestate,) * else (basestate, tzinfo).
   * So it's a tuple in any (non-error) case.
   */
--- 241,250 ----
  }
  
! /*
!  * Pickle support.  Quite a maze!
!  */
  
  /* Let basestate be the state string returned by time_getstate.
!  * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo).
   * So it's a tuple in any (non-error) case.
   */

Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.72
retrieving revision 1.73
diff -C2 -d -r1.72 -r1.73
*** test_both.py	13 Dec 2002 22:56:23 -0000	1.72
--- test_both.py	14 Dec 2002 00:19:36 -0000	1.73
***************
*** 1827,1830 ****
--- 1827,1845 ----
          self.assertRaises(ValueError, lambda: t1 == t1)
  
+     def test_bad_tzinfo_classes(self):
+         tz = self.theclass
+         self.assertRaises(TypeError, tz, 1, 2, 3, tzinfo=12)
+ 
+         class NiceTry(object):
+             def __init__(self): pass
+             def utcoffset(self, dt): pass
+         self.assertRaises(TypeError, tz, 1, 2, 3, tzinfo=NiceTry)
+ 
+         class BetterTry(tzinfo):
+             def __init__(self): pass
+             def utcoffset(self, dt): pass
+         b = BetterTry()
+         t = tz(1, 2, 3, tzinfo=b)
+         self.failUnless(t.tzinfo is b)
  
  def test_suite():