[Python-checkins] python/dist/src/Modules datetimemodule.c,1.63,1.64

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Fri, 16 May 2003 22:55:23 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv27411/python/Modules

Modified Files:
	datetimemodule.c 
Log Message:
datetime.datetime and datetime.time can now be subclassed in Python.  Brr.


Index: datetimemodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/datetimemodule.c,v
retrieving revision 1.63
retrieving revision 1.64
diff -C2 -d -r1.63 -r1.64
*** datetimemodule.c	16 May 2003 22:44:06 -0000	1.63
--- datetimemodule.c	17 May 2003 05:55:19 -0000	1.64
***************
*** 1236,1271 ****
  
  /* ---------------------------------------------------------------------------
!  * Basic object allocation.  These allocate Python objects of the right
!  * size and type, and do the Python object-initialization bit.  If there's
!  * not enough memory, they return NULL after setting MemoryError.  All
!  * data members remain uninitialized trash.
   */
! static PyDateTime_Time *
! alloc_time(int aware)
  {
! 	PyDateTime_Time *self;
  
! 	self = (PyDateTime_Time *)
  		PyObject_MALLOC(aware ?
  				sizeof(PyDateTime_Time) :
  				sizeof(_PyDateTime_BaseTime));
  	if (self == NULL)
! 		return (PyDateTime_Time *)PyErr_NoMemory();
! 	PyObject_INIT(self, &PyDateTime_TimeType);
  	return self;
  }
  
! static PyDateTime_DateTime *
! alloc_datetime(int aware)
  {
! 	PyDateTime_DateTime *self;
  
! 	self = (PyDateTime_DateTime *)
  		PyObject_MALLOC(aware ?
  				sizeof(PyDateTime_DateTime) :
  				sizeof(_PyDateTime_BaseDateTime));
  	if (self == NULL)
! 		return (PyDateTime_DateTime *)PyErr_NoMemory();
! 	PyObject_INIT(self, &PyDateTime_DateTimeType);
  	return self;
  }
--- 1236,1275 ----
  
  /* ---------------------------------------------------------------------------
!  * Basic object allocation:  tp_alloc implementatiosn.  These allocate
!  * Python objects of the right size and type, and do the Python object-
!  * initialization bit.  If there's not enough memory, they return NULL after
!  * setting MemoryError.  All data members remain uninitialized trash.
!  *
!  * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo
!  * member is needed.  This is ugly.
   */
! 
! static PyObject *
! time_alloc(PyTypeObject *type, int aware)
  {
! 	PyObject *self;
  
! 	self = (PyObject *)
  		PyObject_MALLOC(aware ?
  				sizeof(PyDateTime_Time) :
  				sizeof(_PyDateTime_BaseTime));
  	if (self == NULL)
! 		return (PyObject *)PyErr_NoMemory();
! 	PyObject_INIT(self, type);
  	return self;
  }
  
! static PyObject *
! datetime_alloc(PyTypeObject *type, int aware)
  {
! 	PyObject *self;
  
! 	self = (PyObject *)
  		PyObject_MALLOC(aware ?
  				sizeof(PyDateTime_DateTime) :
  				sizeof(_PyDateTime_BaseDateTime));
  	if (self == NULL)
! 		return (PyObject *)PyErr_NoMemory();
! 	PyObject_INIT(self, type);
  	return self;
  }
***************
*** 1303,1317 ****
  
  #define new_date(year, month, day) \
! 	(new_date_ex(year, month, day, &PyDateTime_DateType))
  
  /* Create a datetime instance with no range checking. */
  static PyObject *
! new_datetime(int year, int month, int day, int hour, int minute,
! 	     int second, int usecond, PyObject *tzinfo)
  {
  	PyDateTime_DateTime *self;
  	char aware = tzinfo != Py_None;
  
! 	self = alloc_datetime(aware);
  	if (self != NULL) {
  		self->hastzinfo = aware;
--- 1307,1321 ----
  
  #define new_date(year, month, day) \
! 	new_date_ex(year, month, day, &PyDateTime_DateType)
  
  /* Create a datetime instance with no range checking. */
  static PyObject *
! new_datetime_ex(int year, int month, int day, int hour, int minute,
! 	     int second, int usecond, PyObject *tzinfo, PyTypeObject *type)
  {
  	PyDateTime_DateTime *self;
  	char aware = tzinfo != Py_None;
  
! 	self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware));
  	if (self != NULL) {
  		self->hastzinfo = aware;
***************
*** 1329,1340 ****
  }
  
  /* Create a time instance with no range checking. */
  static PyObject *
! new_time(int hour, int minute, int second, int usecond, PyObject *tzinfo)
  {
  	PyDateTime_Time *self;
  	char aware = tzinfo != Py_None;
  
! 	self = alloc_time(aware);
  	if (self != NULL) {
  		self->hastzinfo = aware;
--- 1333,1349 ----
  }
  
+ #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo)		\
+ 	new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo,	\
+ 			&PyDateTime_DateTimeType)
+ 
  /* Create a time instance with no range checking. */
  static PyObject *
! new_time_ex(int hour, int minute, int second, int usecond,
! 	    PyObject *tzinfo, PyTypeObject *type)
  {
  	PyDateTime_Time *self;
  	char aware = tzinfo != Py_None;
  
! 	self = (PyDateTime_Time *) (type->tp_alloc(type, aware));
  	if (self != NULL) {
  		self->hastzinfo = aware;
***************
*** 1352,1355 ****
--- 1361,1367 ----
  }
  
+ #define new_time(hh, mm, ss, us, tzinfo)		\
+ 	new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType)
+ 
  /* Create a timedelta instance.  Normalize the members iff normalize is
   * true.  Passing false is a speed optimization, if you know for sure
***************
*** 3015,3019 ****
  		}
  		aware = (char)(tzinfo != Py_None);
! 		me = alloc_time(aware);
  		if (me != NULL) {
  			char *pdata = PyString_AS_STRING(state);
--- 3027,3032 ----
  		}
  		aware = (char)(tzinfo != Py_None);
! 		me = (PyDateTime_Time *) time_alloc(&PyDateTime_TimeType,
! 						    aware);
  		if (me != NULL) {
  			char *pdata = PyString_AS_STRING(state);
***************
*** 3037,3041 ****
  		if (check_tzinfo_subclass(tzinfo) < 0)
  			return NULL;
! 		self = new_time(hour, minute, second, usecond, tzinfo);
  	}
  	return self;
--- 3050,3055 ----
  		if (check_tzinfo_subclass(tzinfo) < 0)
  			return NULL;
! 		self = new_time_ex(hour, minute, second, usecond, tzinfo,
! 				   type);
  	}
  	return self;
***************
*** 3440,3444 ****
  	0,					/* tp_dictoffset */
  	0,					/* tp_init */
! 	0,					/* tp_alloc */
  	time_new,				/* tp_new */
  	0,					/* tp_free */
--- 3454,3458 ----
  	0,					/* tp_dictoffset */
  	0,					/* tp_init */
! 	time_alloc,				/* tp_alloc */
  	time_new,				/* tp_new */
  	0,					/* tp_free */
***************
*** 3535,3539 ****
  		}
  		aware = (char)(tzinfo != Py_None);
! 		me = alloc_datetime(aware);
  		if (me != NULL) {
  			char *pdata = PyString_AS_STRING(state);
--- 3549,3555 ----
  		}
  		aware = (char)(tzinfo != Py_None);
! 		me = (PyDateTime_DateTime *) datetime_alloc(
! 						&PyDateTime_DateTimeType,
! 				     		aware);
  		if (me != NULL) {
  			char *pdata = PyString_AS_STRING(state);
***************
*** 3559,3565 ****
  		if (check_tzinfo_subclass(tzinfo) < 0)
  			return NULL;
! 		self = new_datetime(year, month, day,
! 				    hour, minute, second, usecond,
! 				    tzinfo);
  	}
  	return self;
--- 3575,3581 ----
  		if (check_tzinfo_subclass(tzinfo) < 0)
  			return NULL;
! 		self = new_datetime_ex(year, month, day,
! 				    	hour, minute, second, usecond,
! 				    	tzinfo, type);
  	}
  	return self;
***************
*** 4461,4465 ****
  	0,					/* tp_dictoffset */
  	0,					/* tp_init */
! 	0,					/* tp_alloc */
  	datetime_new,				/* tp_new */
  	0,					/* tp_free */
--- 4477,4481 ----
  	0,					/* tp_dictoffset */
  	0,					/* tp_init */
! 	datetime_alloc,				/* tp_alloc */
  	datetime_new,				/* tp_new */
  	0,					/* tp_free */