[Python-checkins] python/nondist/sandbox/datetime obj_datetimetz.c,1.6,1.7 test_both.py,1.74,1.75

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
2002年12月13日 16:49:43 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv23854
Modified Files:
	obj_datetimetz.c test_both.py 
Log Message:
Added a datetimetz pickling test.
Adding datetimez utcoffset(), tzname() and dst() methods.
Index: obj_datetimetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetimetz.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** obj_datetimetz.c	14 Dec 2002 00:19:36 -0000	1.6
--- obj_datetimetz.c	14 Dec 2002 00:49:39 -0000	1.7
***************
*** 21,25 ****
 };
 
! /* Constructors. */
 
 static PyObject *
--- 21,27 ----
 };
 
! /*
! * Constructors.
! */
 
 static PyObject *
***************
*** 207,211 ****
 }
 
! /* Destructor. */
 
 static void
--- 209,215 ----
 }
 
! /*
! * Destructor.
! */
 
 static void
***************
*** 216,220 ****
 }
 
! /* datetimetz arithmetic. */
 
 static PyObject *
--- 220,260 ----
 }
 
! /*
! * Indirect access to tzinfo methods.
! */
! 
! static PyObject *
! datetimetz_convienience(PyDateTime_DateTimeTZ *self, char *name)
! {
! 	PyObject *result;
! 
! 	if (self->tzinfo == Py_None) {
! 		result = Py_None;
! 		Py_INCREF(result);
! 	}
! 	else
! 		result = PyObject_CallMethod(self->tzinfo, name, "O", self);
! 	return result;
! }
! 
! /* These are all METH_NOARGS, so don't need to check the arglist. */
! static PyObject *
! datetimetz_utcoffset(PyDateTime_DateTimeTZ *self, PyObject *unused) {
! 	return datetimetz_convienience(self, "utcoffset");
! }
! 
! static PyObject *
! datetimetz_tzname(PyDateTime_DateTimeTZ *self, PyObject *unused) {
! 	return datetimetz_convienience(self, "tzname");
! }
! 
! static PyObject *
! datetimetz_dst(PyDateTime_DateTimeTZ *self, PyObject *unused) {
! 	return datetimetz_convienience(self, "dst");
! }
! 
! /*
! * datetimetz arithmetic.
! */
 
 static PyObject *
***************
*** 585,588 ****
--- 625,637 ----
 	 	 "defaults\n"
 	 	 "to 'T'.")},
+ 
+ 	{"utcoffset",	(PyCFunction)datetimetz_utcoffset, METH_NOARGS,
+ 	 PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
+ 
+ 	{"tzname",	(PyCFunction)datetimetz_tzname,	METH_NOARGS,
+ 	 PyDoc_STR("Return self.tzinfo.tzname(self).")},
+ 
+ 	{"dst",		(PyCFunction)datetimetz_dst, METH_NOARGS,
+ 	 PyDoc_STR("Return self.tzinfo.dst(self).")},
 
 	{"__setstate__", (PyCFunction)datetimetz_setstate, METH_O,
Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.74
retrieving revision 1.75
diff -C2 -d -r1.74 -r1.75
*** test_both.py	14 Dec 2002 00:39:04 -0000	1.74
--- test_both.py	14 Dec 2002 00:49:39 -0000	1.75
***************
*** 1845,1848 ****
--- 1845,1885 ----
 self.failUnless(t.tzinfo is b)
 
+ def test_pickling(self):
+ import pickle, cPickle
+ 
+ # Try one without a tzinfo.
+ 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:
+ green = pickler.dumps(orig, binary)
+ derived = pickler.loads(green)
+ self.assertEqual(orig, derived)
+ 
+ # Try one with a tzinfo.
+ tinfo = FixedOffset(-300, 'cookie')
+ orig = self.theclass(*args, **{'tzinfo': tinfo})
+ state = orig.__getstate__()
+ derived = self.theclass(1, 1, 1)
+ derived.__setstate__(state)
+ self.assertEqual(orig, derived)
+ self.failUnless(isinstance(derived.tzinfo, FixedOffset))
+ self.assertEqual(derived.utcoffset(), -300)
+ self.assertEqual(derived.tzname(), 'cookie')
+ 
+ for pickler in pickle, cPickle:
+ for binary in 0, 1:
+ green = pickler.dumps(orig, binary)
+ derived = pickler.loads(green)
+ self.assertEqual(orig, derived)
+ self.failUnless(isinstance(derived.tzinfo, FixedOffset))
+ self.assertEqual(derived.utcoffset(), -300)
+ self.assertEqual(derived.tzname(), 'cookie')
+ 
 def test_suite():
 allsuites = [unittest.makeSuite(klass, 'test')

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