[Python-checkins] python/nondist/sandbox/datetime obj_delta.c,1.14,1.15 test_both.py,1.15,1.16

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Mon, 02 Dec 2002 09:31:39 -0800


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

Modified Files:
	obj_delta.c test_both.py 
Log Message:
Another broken attempt to get pickling of timedelta objects to work.


Index: obj_delta.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_delta.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** obj_delta.c	2 Dec 2002 06:06:48 -0000	1.14
--- obj_delta.c	2 Dec 2002 17:31:21 -0000	1.15
***************
*** 580,583 ****
--- 580,609 ----
  }
  
+ /* XXX A further broken attempt to get pickling to work.
+  * XXX This avoids the problem above, but dies instead with
+  * XXX    PicklingError: Can't pickle <type 'timedelta'>: it's not
+  * XXX    found as __builtin__.tmedelta
+  */
+ static PyObject *
+ delta_reduce(PyDateTime_Delta* self)
+ {
+ 	PyObject* result = NULL;
+ 	PyObject* state;
+ 
+ 	state = delta_getstate(self);
+ 	if (state != NULL) {
+ 		PyObject *emptytuple = PyTuple_New(0);
+ 		if (emptytuple != NULL) {
+ 			result = Py_BuildValue("OOO",
+ 					       self->ob_type,
+ 					       emptytuple,
+ 					       state);
+ 			Py_DECREF(emptytuple);
+ 		}
+ 		Py_DECREF(state);
+ 	}
+ 	return result;
+ }
+ 
  #define OFFSET(field)  offsetof(PyDateTime_Delta, field)
  
***************
*** 588,600 ****
  	 PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")},
  	{"microseconds", T_LONG, OFFSET(microseconds), READONLY,
! 	 PyDoc_STR("Number of microseconds (>= and less than 1 second).")},
  	{NULL}
  };
  
  static PyMethodDef delta_methods[] = {
- 	{"__getstate__", (PyCFunction)delta_getstate, METH_NOARGS,
- 	 	PyDoc_STR("__getstate__() -> state")},
  	{"__setstate__", (PyCFunction)delta_setstate, METH_O,
  	 	PyDoc_STR("__setstate__(state)")},
  	{NULL,	NULL},
  };
--- 614,628 ----
  	 PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")},
  	{"microseconds", T_LONG, OFFSET(microseconds), READONLY,
! 	 PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")},
  	{NULL}
  };
  
  static PyMethodDef delta_methods[] = {
  	{"__setstate__", (PyCFunction)delta_setstate, METH_O,
  	 	PyDoc_STR("__setstate__(state)")},
+ 	{"__reduce__", (PyCFunction)delta_reduce,     METH_NOARGS,
+ 		NULL},
+ 	{"__getstate__", (PyCFunction)delta_getstate, METH_NOARGS,
+ 	 	PyDoc_STR("__getstate__() -> state")},
  	{NULL,	NULL},
  };

Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** test_both.py	2 Dec 2002 06:42:28 -0000	1.15
--- test_both.py	2 Dec 2002 17:31:25 -0000	1.16
***************
*** 141,147 ****
              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)
--- 141,152 ----
              for binary in 0, 1:
                  # XXX Pickling fails in the C implementation.
!                 # XXX timedelta has a __reduce__ method, which returns, e.g.,
!                 # XXX
!                 # XXX    (<type 'timedelta'>, (), (1, 0, 0))
!                 # XXX
!                 # XXX but pickle then complains
!                 # XXX
!                 # XXX     PicklingError: Can't pickle <type 'timedelta'>:
!                 # XXX     it's not found as __builtin__.timedelta
                  green = pickler.dumps(orig, binary)
                  derived = pickler.loads(green)