[Python-checkins] python/nondist/sandbox/datetime datetime.c,1.64,1.65 obj_date.c,1.49,1.50 obj_datetime.c,1.47,1.48 obj_datetimetz.c,1.1,1.2 obj_time.c,1.8,1.9 obj_timetz.c,1.19,1.20

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


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv16088
Modified Files:
	datetime.c obj_date.c obj_datetime.c obj_datetimetz.c 
	obj_time.c obj_timetz.c 
Log Message:
Refactor ever-bloating argument range-check code.
Index: datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.c,v
retrieving revision 1.64
retrieving revision 1.65
diff -C2 -d -r1.64 -r1.65
*** datetime.c	13 Dec 2002 16:34:58 -0000	1.64
--- datetime.c	13 Dec 2002 16:43:11 -0000	1.65
***************
*** 113,139 ****
 
 /* ---------------------------------------------------------------------------
- * Range checkers.
- */
- 
- /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0.
- * If not, raise the given exception and return -1.
- */
- static int
- check_delta_day_range(long days, PyObject *exception)
- {
- 	char buf[200];
- 
- 	if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS)
- 		return 0;
- 	/* PyErr_Format() ignores the "l" in "%ld", which isn't correct
- 	 * on boxes where sizeof(long) > sizeof(int). So roll our own.
- 	 */
- 	PyOS_snprintf(buf, sizeof(buf), "days=%ld; must have magnitude <= %d",
- 		 days, MAX_DELTA_DAYS);
- 	PyErr_SetString(exception, buf);
- 	return -1;
- }
- 
- /* ---------------------------------------------------------------------------
 * General calendrical helper functions
 */
--- 113,116 ----
***************
*** 329,332 ****
--- 306,386 ----
 		week1_monday += 7;
 	return week1_monday;
+ }
+ 
+ /* ---------------------------------------------------------------------------
+ * Range checkers.
+ */
+ 
+ /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0.
+ * If not, raise the given exception and return -1.
+ */
+ static int
+ check_delta_day_range(long days, PyObject *exception)
+ {
+ 	char buf[200];
+ 
+ 	if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS)
+ 		return 0;
+ 	/* PyErr_Format() ignores the "l" in "%ld", which isn't correct
+ 	 * on boxes where sizeof(long) > sizeof(int). So roll our own.
+ 	 */
+ 	PyOS_snprintf(buf, sizeof(buf), "days=%ld; must have magnitude <= %d",
+ 		 days, MAX_DELTA_DAYS);
+ 	PyErr_SetString(exception, buf);
+ 	return -1;
+ }
+ 
+ /* Check that date arguments are in range. Return 0 if they are. If they
+ * aren't, raise ValueError and return -1.
+ */
+ static int
+ check_date_args(int year, int month, int day)
+ {
+ 
+ 	if (year < MINYEAR || year > MAXYEAR) {
+ 		PyErr_SetString(PyExc_ValueError,
+ 				"year is out of range");
+ 		return -1;
+ 	}
+ 	if (month < 1 || month > 12) {
+ 		PyErr_SetString(PyExc_ValueError,
+ 				"month must be in 1..12");
+ 		return -1;
+ 	}
+ 	if (day < 1 || day > days_in_month(year, month)) {
+ 		PyErr_SetString(PyExc_ValueError,
+ 				"day is out of range for month");
+ 		return -1;
+ 	}
+ 	return 0;
+ }
+ 
+ /* Check that time arguments are in range. Return 0 if they are. If they
+ * aren't, raise ValueError and return -1.
+ */
+ static int
+ check_time_args(int h, int m, int s, int us)
+ {
+ 	if (h < 0 || h > 23) {
+ 		PyErr_SetString(PyExc_ValueError,
+ 				"hour must be in 0..23");
+ 		return -1;
+ 	}
+ 	if (m < 0 || m > 59) {
+ 		PyErr_SetString(PyExc_ValueError,
+ 				"minute must be in 0..59");
+ 		return -1;
+ 	}
+ 	if (s < 0 || s > 59) {
+ 		PyErr_SetString(PyExc_ValueError,
+ 				"second must be in 0..59");
+ 		return -1;
+ 	}
+ 	if (us < 0 || us > 999999) {
+ 		PyErr_SetString(PyExc_ValueError,
+ 				"microsecond must be in 0..999999");
+ 		return -1;
+ 	}
+ 	return 0;
 }
 
Index: obj_date.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_date.c,v
retrieving revision 1.49
retrieving revision 1.50
diff -C2 -d -r1.49 -r1.50
*** obj_date.c	13 Dec 2002 01:13:46 -0000	1.49
--- obj_date.c	13 Dec 2002 16:43:15 -0000	1.50
***************
*** 44,62 ****
 	if (PyArg_ParseTupleAndKeywords(args, kw, "lll", keywords,
 					&year, &month, &day)) {
! 		if (year < MINYEAR || year > MAXYEAR) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"year is out of range");
! 			return NULL;
! 		}
! 		if (month < 1 || month > 12) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"month must be in 1..12");
! 			return NULL;
! 		}
! 		if (day < 1 || day > days_in_month(year, month)) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"day is out of range for month");
 			return NULL;
- 		}
 		self = new_date(year, month, day);
 	}
--- 44,49 ----
 	if (PyArg_ParseTupleAndKeywords(args, kw, "lll", keywords,
 					&year, &month, &day)) {
! 		if (check_date_args(year, month, day) < 0)
 			return NULL;
 		self = new_date(year, month, day);
 	}
Index: obj_datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetime.c,v
retrieving revision 1.47
retrieving revision 1.48
diff -C2 -d -r1.47 -r1.48
*** obj_datetime.c	13 Dec 2002 01:13:46 -0000	1.47
--- obj_datetime.c	13 Dec 2002 16:43:20 -0000	1.48
***************
*** 59,97 ****
 					&year, &month, &day, &hour, &minute,
 					&second, &usecond)) {
! 		if (year < MINYEAR || year > MAXYEAR) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"year is out of range");
! 			return NULL;
! 		}
! 		if (month < 1 || month > 12) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"month must be in 1..12");
! 			return NULL;
! 		}
! 		if (day < 1 || day > days_in_month(year, month)) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"day is out of range for month");
! 			return NULL;
! 		}
! 		if (hour < 0 || hour > 23) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"hour must be in 0..23");
! 			return NULL;
! 		}
! 		if (minute < 0 || minute > 59) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"minute must be in 0..59");
! 			return NULL;
! 		}
! 		if (second < 0 || second > 59) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"second must be in 0..59");
 			return NULL;
! 		}
! 		if (usecond < 0 || usecond > 999999) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"microsecond must be in 0..999999");
 			return NULL;
- 		}
 		self = new_datetime(year, month, day, hour, minute, second,
 				 usecond);
--- 59,66 ----
 					&year, &month, &day, &hour, &minute,
 					&second, &usecond)) {
! 		if (check_date_args(year, month, day) < 0)
 			return NULL;
! 		if (check_time_args(hour, minute, second, usecond) < 0)
 			return NULL;
 		self = new_datetime(year, month, day, hour, minute, second,
 				 usecond);
Index: obj_datetimetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetimetz.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** obj_datetimetz.c	13 Dec 2002 01:13:47 -0000	1.1
--- obj_datetimetz.c	13 Dec 2002 16:43:24 -0000	1.2
***************
*** 62,100 ****
 					&year, &month, &day, &hour, &minute,
 					&second, &usecond)) {
! 		if (year < MINYEAR || year > MAXYEAR) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"year is out of range");
! 			return NULL;
! 		}
! 		if (month < 1 || month > 12) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"month must be in 1..12");
! 			return NULL;
! 		}
! 		if (day < 1 || day > days_in_month(year, month)) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"day is out of range for month");
! 			return NULL;
! 		}
! 		if (hour < 0 || hour > 23) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"hour must be in 0..23");
! 			return NULL;
! 		}
! 		if (minute < 0 || minute > 59) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"minute must be in 0..59");
! 			return NULL;
! 		}
! 		if (second < 0 || second > 59) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"second must be in 0..59");
 			return NULL;
! 		}
! 		if (usecond < 0 || usecond > 999999) {
! 			PyErr_SetString(PyExc_ValueError,
! 					"microsecond must be in 0..999999");
 			return NULL;
- 		}
 		self = new_datetime(year, month, day, hour, minute, second,
 				 usecond);
--- 62,69 ----
 					&year, &month, &day, &hour, &minute,
 					&second, &usecond)) {
! 		if (check_date_args(year, month, day) < 0)
 			return NULL;
! 		if (check_time_args(hour, minute, second, usecond) < 0)
 			return NULL;
 		self = new_datetime(year, month, day, hour, minute, second,
 				 usecond);
Index: obj_time.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_time.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** obj_time.c	12 Dec 2002 17:58:28 -0000	1.8
--- obj_time.c	13 Dec 2002 16:43:25 -0000	1.9
***************
*** 39,71 ****
 /* Constructors. */
 
- /* Check that time arguments are in range. Return 0 if they are. If they
- * aren't, raise ValueError and return -1.
- */
- static int
- time_check_time_args(int h, int m, int s, int us)
- {
- 	if (h < 0 || h > 23) {
- 		PyErr_SetString(PyExc_ValueError,
- 				"hour must be in 0..23");
- 		return -1;
- 	}
- 	if (m < 0 || m > 59) {
- 		PyErr_SetString(PyExc_ValueError,
- 				"minute must be in 0..59");
- 		return -1;
- 	}
- 	if (s < 0 || s > 59) {
- 		PyErr_SetString(PyExc_ValueError,
- 				"second must be in 0..59");
- 		return -1;
- 	}
- 	if (us < 0 || us > 999999) {
- 		PyErr_SetString(PyExc_ValueError,
- 				"microsecond must be in 0..999999");
- 		return -1;
- 	}
- 	return 0;
- }
- 
 static PyObject *
 time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
--- 39,42 ----
***************
*** 83,87 ****
 	if (PyArg_ParseTupleAndKeywords(args, kw, "|iiii", keywords,
 					&hour, &minute, &second, &usecond)) {
! 		if (time_check_time_args(hour, minute, second, usecond) < 0)
 			return NULL;
 		self = new_time(hour, minute, second, usecond);
--- 54,58 ----
 	if (PyArg_ParseTupleAndKeywords(args, kw, "|iiii", keywords,
 					&hour, &minute, &second, &usecond)) {
! 		if (check_time_args(hour, minute, second, usecond) < 0)
 			return NULL;
 		self = new_time(hour, minute, second, usecond);
Index: obj_timetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_timetz.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** obj_timetz.c	13 Dec 2002 16:35:09 -0000	1.19
--- obj_timetz.c	13 Dec 2002 16:43:28 -0000	1.20
***************
*** 38,42 ****
 					&hour, &minute, &second, &usecond,
 					&tzinfo)) {
! 		if (time_check_time_args(hour, minute, second, usecond) < 0)
 			return NULL;
 		if (check_tzinfo_subclass(tzinfo, "tzinfo argument") < 0)
--- 38,42 ----
 					&hour, &minute, &second, &usecond,
 					&tzinfo)) {
! 		if (check_time_args(hour, minute, second, usecond) < 0)
 			return NULL;
 		if (check_tzinfo_subclass(tzinfo, "tzinfo argument") < 0)

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