[Python-checkins] python/dist/src/Modules timemodule.c,2.143,2.144

fdrake at users.sourceforge.net fdrake at users.sourceforge.net
Tue Aug 3 19:59:12 CEST 2004


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7177/Modules

Modified Files:
	timemodule.c 
Log Message:
allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
(closes SF bug #658254, patch #663482)


Index: timemodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/timemodule.c,v
retrieving revision 2.143
retrieving revision 2.144
diff -C2 -d -r2.143 -r2.144
*** timemodule.c	20 Jul 2004 22:34:37 -0000	2.143
--- timemodule.c	3 Aug 2004 17:58:55 -0000	2.144
***************
*** 278,288 ****
  }
  
  static PyObject *
  time_gmtime(PyObject *self, PyObject *args)
  {
  	double when;
! 	if (PyTuple_Size(args) == 0)
! 		when = floattime();
! 	if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
  		return NULL;
  	return time_convert(when, gmtime);
--- 278,308 ----
  }
  
+ /* Parse arg tuple that can contain an optional float-or-None value;
+    format needs to be "|O:name".
+    Returns non-zero on success (parallels PyArg_ParseTuple).
+ */
+ static int
+ parse_time_double_args(PyObject *args, char *format, double *pwhen)
+ {
+ 	PyObject *ot = NULL;
+ 
+ 	if (!PyArg_ParseTuple(args, format, &ot))
+ 		return 0;
+ 	if (ot == NULL || ot == Py_None)
+ 		*pwhen = floattime();
+ 	else {
+ 		double when = PyFloat_AsDouble(ot);
+ 		if (PyErr_Occurred())
+ 			return 0;
+ 		*pwhen = when;
+ 	}
+ 	return 1;
+ }
+ 
  static PyObject *
  time_gmtime(PyObject *self, PyObject *args)
  {
  	double when;
! 	if (!parse_time_double_args(args, "|O:gmtime", &when))
  		return NULL;
  	return time_convert(when, gmtime);
***************
*** 300,306 ****
  {
  	double when;
! 	if (PyTuple_Size(args) == 0)
! 		when = floattime();
! 	if (!PyArg_ParseTuple(args, "|d:localtime", &when))
  		return NULL;
  	return time_convert(when, localtime);
--- 320,324 ----
  {
  	double when;
! 	if (!parse_time_double_args(args, "|O:localtime", &when))
  		return NULL;
  	return time_convert(when, localtime);
***************
*** 503,514 ****
  time_ctime(PyObject *self, PyObject *args)
  {
! 	double dt;
  	time_t tt;
  	char *p;
  
! 	if (PyTuple_Size(args) == 0)
  		tt = time(NULL);
  	else {
! 		if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
  			return NULL;
  		tt = _PyTime_DoubleToTimet(dt);
--- 521,535 ----
  time_ctime(PyObject *self, PyObject *args)
  {
! 	PyObject *ot = NULL;
  	time_t tt;
  	char *p;
  
! 	if (!PyArg_ParseTuple(args, "|O:ctime", &ot))
! 		return NULL;
! 	if (ot == NULL || ot == Py_None)
  		tt = time(NULL);
  	else {
! 		double dt = PyFloat_AsDouble(ot);
! 		if (PyErr_Occurred())
  			return NULL;
  		tt = _PyTime_DoubleToTimet(dt);



More information about the Python-checkins mailing list