[Python-checkins] python/nondist/sandbox/datetime datetime.py,1.69,1.70 obj_date.c,1.12,1.13 obj_datetime.c,1.6,1.7 test_both.py,1.14,1.15

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Sun, 01 Dec 2002 22:42:30 -0800


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

Modified Files:
	datetime.py obj_date.c obj_datetime.c test_both.py 
Log Message:
Added pickling to datetime objects.  Again the C implementation doesn't
work.


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.69
retrieving revision 1.70
diff -C2 -d -r1.69 -r1.70
*** datetime.py	2 Dec 2002 06:30:59 -0000	1.69
--- datetime.py	2 Dec 2002 06:42:28 -0000	1.70
***************
*** 1238,1241 ****
--- 1238,1256 ----
              self.__microsecond)
  
+     # Pickle support.
+ 
+     def __getstate__(self):
+         yhi, ylo = divmod(self.__year, 256)
+         us2, us3 = divmod(self.__microsecond, 256)
+         us1, us2 = divmod(us2, 256)
+         return ("%c" * 10) % (yhi, ylo, self.__month, self.__day, self.__hour,
+                               self.__minute, self.__second, us1, us2, us3)
+ 
+     def __setstate__(self, string):
+         assert len(string) == 10
+         (yhi, ylo, self.__month, self.__day, self.__hour,
+          self.__minute, self.__second, us1, us2, us3) = map(ord, string)
+         self.__year = yhi * 256 + ylo
+         self.__microsecond = (((us1 << 8) | us2) << 8) | us3
  
  datetime.min = datetime(1, 1, 1)

Index: obj_date.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_date.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** obj_date.c	2 Dec 2002 06:30:59 -0000	1.12
--- obj_date.c	2 Dec 2002 06:42:28 -0000	1.13
***************
*** 371,374 ****
--- 371,375 ----
  	return PyInt_FromLong(dow);
  }
+ 
  /* XXX Broken attempt to get pickles working.  An attempt to pickle
   * XXX craps out in

Index: obj_datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetime.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** obj_datetime.c	1 Dec 2002 19:37:28 -0000	1.6
--- obj_datetime.c	2 Dec 2002 06:42:28 -0000	1.7
***************
*** 372,375 ****
--- 372,405 ----
  }
  
+ /* XXX Broken attempt to get pickles working.  An attempt to pickle
+  * XXX craps out in
+  * XXX
+  * XXX     if base is self.__class__:
+  * XXX         raise TypeError, "can't pickle %s objects" % base.__name__
+  * XXX
+  * XXX in Python's copy_reg.py.  How to fix?
+  */
+ static PyObject *
+ datetime_getstate(PyDateTime_DateTime *self)
+ {
+ 	return PyString_FromStringAndSize(self->data,
+ 					  _PyDateTime_DATETIME_DATA_SIZE);
+ }
+ 
+ static PyObject *
+ datetime_setstate(PyDateTime_DateTime *self, PyObject *state)
+ {
+ 	const int len = PyString_Size(state);
+ 	unsigned char *pdata = (unsigned char*)PyString_AsString(state);
+ 
+ 	assert(len == _PyDateTime_DATETIME_DATA_SIZE);
+ 
+ 	memcpy(self->data, pdata, _PyDateTime_DATETIME_DATA_SIZE);
+ 	self->hashcode = -1;
+ 
+ 	Py_INCREF(Py_None);
+ 	return Py_None;
+ }
+ 
  static PyMethodDef datetime_methods[] = {
  	/* Class methods: */
***************
*** 385,389 ****
  	 "Return the day of the week represented by the datetime.\n"
  	 "Monday == 1 ... Sunday == 7"},
! 	{NULL}
  };
  
--- 415,423 ----
  	 "Return the day of the week represented by the datetime.\n"
  	 "Monday == 1 ... Sunday == 7"},
! 	{"__getstate__", (PyCFunction)datetime_getstate, METH_NOARGS,
! 	 	PyDoc_STR("__getstate__() -> state")},
! 	{"__setstate__", (PyCFunction)datetime_setstate, METH_O,
! 	 	PyDoc_STR("__setstate__(state)")},
! 	{NULL,	NULL}
  };
  

Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** test_both.py	2 Dec 2002 06:30:59 -0000	1.14
--- test_both.py	2 Dec 2002 06:42:28 -0000	1.15
***************
*** 618,622 ****
      def test_pickling(self):
          import pickle, cPickle
!         pass
  
  def test_suite():
--- 618,637 ----
      def test_pickling(self):
          import pickle, cPickle
!         args = 6, 7, 23, 20, 59, 1, 64**2
!         orig = self.theclass(*args)
!         state = orig.__getstate__()
!         self.assertEqual(state, '\x00\x06\x07\x17\x14\x3b\x01\x00\x10\x00')
!         derived = self.theclass(1, 1, 1)
!         derived.__setstate__(state)
!         self.assertEqual(orig, derived)
!         for pickler in pickle, cPickle:
!             for binary in 0, 1:
!                 # XXX Pickling fails in the C implementation.
!                 # XXX __getstate__ and __setstate__ are there, but the
!                 # XXX pickler refuses to use them.  I suspect it doesn't
!                 # XXX know how to create "an empty" base object.
!                 green = pickler.dumps(orig, binary)
!                 derived = pickler.loads(green)
!                 self.assertEqual(orig, derived)
  
  def test_suite():