[Python-checkins] python/nondist/sandbox/datetime datetime.py,1.68,1.69 obj_date.c,1.11,1.12 test_both.py,1.13,1.14

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
2002年12月01日 22:31:01 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv317
Modified Files:
	datetime.py obj_date.c test_both.py 
Log Message:
Added pickle support to the C and Python date implementations, + a test
case.
PROBLEM: as with timedelta before it, __setstate__/__getstate__ do the
same things in both implementations, but pickling doesn't work for the C
implementation.
Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.68
retrieving revision 1.69
diff -C2 -d -r1.68 -r1.69
*** datetime.py	2 Dec 2002 06:06:48 -0000	1.68
--- datetime.py	2 Dec 2002 06:30:59 -0000	1.69
***************
*** 696,699 ****
--- 696,710 ----
 return "%04d-%02d-%02d" % (self.__year, self.__month, self.__day)
 
+ # Pickle support.
+ 
+ def __getstate__(self):
+ yhi, ylo = divmod(self.__year, 256)
+ return "%c%c%c%c" % (yhi, ylo, self.__month, self.__day)
+ 
+ def __setstate__(self, string):
+ assert len(string) == 4
+ yhi, ylo, self.__month, self.__day = map(ord, string)
+ self.__year = yhi * 256 + ylo
+ 
 date.min = date(1, 1, 1)
 date.max = date(9999, 12, 31)
Index: obj_date.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_date.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** obj_date.c	1 Dec 2002 19:37:28 -0000	1.11
--- obj_date.c	2 Dec 2002 06:30:59 -0000	1.12
***************
*** 371,374 ****
--- 371,403 ----
 	return PyInt_FromLong(dow);
 }
+ /* 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 *
+ date_getstate(PyDateTime_Date *self)
+ {
+ 	return PyString_FromStringAndSize(self->data,
+ 					 _PyDateTime_DATE_DATA_SIZE);
+ }
+ 
+ static PyObject *
+ date_setstate(PyDateTime_Date *self, PyObject *state)
+ {
+ 	const int len = PyString_Size(state);
+ 	unsigned char *pdata = (unsigned char*)PyString_AsString(state);
+ 
+ 	assert(len == _PyDateTime_DATE_DATA_SIZE);
+ 
+ 	memcpy(self->data, pdata, _PyDateTime_DATE_DATA_SIZE);
+ 	self->hashcode = -1;
+ 
+ 	Py_INCREF(Py_None);
+ 	return Py_None;
+ }
 
 static PyMethodDef date_methods[] = {
***************
*** 398,402 ****
 	 "Return the day of the week represented by the date.\n"
 	 "Monday == 0 ... Sunday == 6"},
! 	{NULL}
 };
 
--- 427,435 ----
 	 "Return the day of the week represented by the date.\n"
 	 "Monday == 0 ... Sunday == 6"},
! 	{"__getstate__", (PyCFunction)date_getstate,	METH_NOARGS,
! 	 	PyDoc_STR("__getstate__() -> state")},
! 	{"__setstate__", (PyCFunction)date_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.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** test_both.py	2 Dec 2002 06:06:48 -0000	1.13
--- test_both.py	2 Dec 2002 06:30:59 -0000	1.14
***************
*** 402,405 ****
--- 402,424 ----
 self.assertEqual(t, (1956, 3, 1+i, 0, 0, 0, (3+i)%7, 61+i, -1))
 
+ def test_pickling(self):
+ import pickle, cPickle
+ args = 6, 7, 23
+ orig = self.theclass(*args)
+ state = orig.__getstate__()
+ self.assertEqual(state, '\x00\x06\x07\x17')
+ 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)
+ 
 #############################################################################
 # datetime tests
***************
*** 596,599 ****
--- 615,622 ----
 self.assertRaises(TypeError, lambda: i+a)
 self.assertRaises(TypeError, lambda: i-a)
+ 
+ def test_pickling(self):
+ import pickle, cPickle
+ pass
 
 def test_suite():

AltStyle によって変換されたページ (->オリジナル) /