[Python-checkins] python/dist/src/Modules datetimemodule.c,1.16,1.17
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
2002年12月26日 18:21:54 -0800
Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv2855/Modules
Modified Files:
datetimemodule.c
Log Message:
Squash recently-introduced code duplication.
Index: datetimemodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/datetimemodule.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** datetimemodule.c 27 Dec 2002 00:41:11 -0000 1.16
--- datetimemodule.c 27 Dec 2002 02:21:51 -0000 1.17
***************
*** 823,826 ****
--- 823,854 ----
}
+ /* Classify two objects as to whether they're naive or offset-aware.
+ * This isn't quite the same as calling classify_utcoffset() twice: for
+ * binary operations (comparison and subtraction), we generally want to
+ * ignore the tzinfo members if they're identical. This is by design,
+ * so that results match "naive" expectations when mixing objects from a
+ * single timezone. So in that case, this sets both offsets to 0 and
+ * both naiveties to OFFSET_NAIVE.
+ * The function returns 0 if everything's OK, and -1 on error.
+ */
+ static int
+ classify_two_utcoffsets(PyObject *o1, int *offset1, naivety *n1,
+ PyObject *o2, int *offset2, naivety *n2)
+ {
+ if (get_tzinfo_member(o1) == get_tzinfo_member(o2)) {
+ *offset1 = *offset2 = 0;
+ *n1 = *n2 = OFFSET_NAIVE;
+ }
+ else {
+ *n1 = classify_utcoffset(o1, offset1);
+ if (*n1 == OFFSET_ERROR)
+ return -1;
+ *n2 = classify_utcoffset(o2, offset2);
+ if (*n2 == OFFSET_ERROR)
+ return -1;
+ }
+ return 0;
+ }
+
/* repr is like "someclass(arg1, arg2)". If tzinfo isn't None,
* stuff
***************
*** 3137,3161 ****
return NULL;
}
- /* Ignore utcoffsets if they have identical tzinfo members. This
- * isn't an optimization, it's design. If utcoffset() doesn't ignore
- * its argument, it may return different results for self and other
- * even if they have identical tzinfo members, and we're deliberately
- * suppressing that (possible) difference.
- */
- if (get_tzinfo_member((PyObject *)self) == get_tzinfo_member(other)) {
- offset1 = offset2 = 0;
- n1 = n2 = OFFSET_NAIVE;
- }
- else {
- n1 = classify_utcoffset((PyObject *)self, &offset1);
- assert(n1 != OFFSET_UNKNOWN);
- if (n1 == OFFSET_ERROR)
- return NULL;
! n2 = classify_utcoffset(other, &offset2);
! assert(n2 != OFFSET_UNKNOWN);
! if (n2 == OFFSET_ERROR)
! return NULL;
! }
/* If they're both naive, or both aware and have the same offsets,
* we get off cheap. Note that if they're both naive, offset1 ==
--- 3165,3172 ----
return NULL;
}
! if (classify_two_utcoffsets((PyObject *)self, &offset1, &n1,
! other, &offset2, &n2) < 0)
! return NULL;
/* If they're both naive, or both aware and have the same offsets,
* we get off cheap. Note that if they're both naive, offset1 ==
***************
*** 3668,3693 ****
return NULL;
}
! /* Ignore utcoffsets if they have identical tzinfo members. This
! * isn't an optimization, it's design. If utcoffset() doesn't ignore
! * its argument, it may return different results for self and other
! * even if they have identical tzinfo members, and we're deliberately
! * suppressing that (possible) difference.
! */
! if (get_tzinfo_member((PyObject *)self) == get_tzinfo_member(other)) {
! offset1 = offset2 = 0;
! n1 = n2 = OFFSET_NAIVE;
! }
! else {
! n1 = classify_utcoffset((PyObject *)self, &offset1);
! assert(n1 != OFFSET_UNKNOWN);
! if (n1 == OFFSET_ERROR)
! return NULL;
!
! n2 = classify_utcoffset(other, &offset2);
! assert(n2 != OFFSET_UNKNOWN);
! if (n2 == OFFSET_ERROR)
! return NULL;
! }
!
/* If they're both naive, or both aware and have the same offsets,
* we get off cheap. Note that if they're both naive, offset1 ==
--- 3679,3685 ----
return NULL;
}
! if (classify_two_utcoffsets((PyObject *)self, &offset1, &n1,
! other, &offset2, &n2) < 0)
! return NULL;
/* If they're both naive, or both aware and have the same offsets,
* we get off cheap. Note that if they're both naive, offset1 ==
***************
*** 4625,4652 ****
PyDateTime_Delta *delta;
! /* Ignore utcoffsets if they have identical tzinfo
! * members. This isn't an optimization, it's design.
! * If utcoffset() doesn't ignore its argument, it may
! * return different results for self and other even
! * if they have identical tzinfo members, and we're
! * deliberately suppressing that (possible) difference.
! */
! if (get_tzinfo_member(left) ==
! get_tzinfo_member(right)) {
! offset1 = offset2 = 0;
! n1 = n2 = OFFSET_NAIVE;
! }
! else {
! n1 = classify_utcoffset(left, &offset1);
! assert(n1 != OFFSET_UNKNOWN);
! if (n1 == OFFSET_ERROR)
! return NULL;
!
! n2 = classify_utcoffset(right, &offset2);
! assert(n2 != OFFSET_UNKNOWN);
! if (n2 == OFFSET_ERROR)
! return NULL;
! }
!
if (n1 != n2) {
PyErr_SetString(PyExc_TypeError,
--- 4617,4623 ----
PyDateTime_Delta *delta;
! if (classify_two_utcoffsets(left, &offset1, &n1,
! right, &offset2, &n2) < 0)
! return NULL;
if (n1 != n2) {
PyErr_SetString(PyExc_TypeError,