mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
Squash recently-introduced code duplication.
This commit is contained in:
parent
60c76e4016
commit
00237037ae
1 changed files with 37 additions and 66 deletions
|
@ -822,6 +822,34 @@ classify_utcoffset(PyObject *op, int *offset)
|
||||||
return none ? OFFSET_NAIVE : OFFSET_AWARE;
|
return none ? OFFSET_NAIVE : OFFSET_AWARE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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,
|
/* repr is like "someclass(arg1, arg2)". If tzinfo isn't None,
|
||||||
* stuff
|
* stuff
|
||||||
* ", tzinfo=" + repr(tzinfo)
|
* ", tzinfo=" + repr(tzinfo)
|
||||||
|
@ -3136,27 +3164,10 @@ datetime_richcompare(PyDateTime_DateTime *self, PyObject *other, int op)
|
||||||
other->ob_type->tp_name);
|
other->ob_type->tp_name);
|
||||||
return NULL;
|
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);
|
if (classify_two_utcoffsets((PyObject *)self, &offset1, &n1,
|
||||||
assert(n2 != OFFSET_UNKNOWN);
|
other, &offset2, &n2) < 0)
|
||||||
if (n2 == OFFSET_ERROR)
|
return NULL;
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
/* If they're both naive, or both aware and have the same offsets,
|
/* 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 ==
|
* we get off cheap. Note that if they're both naive, offset1 ==
|
||||||
* offset2 == 0 at this point.
|
* offset2 == 0 at this point.
|
||||||
|
@ -3667,28 +3678,9 @@ time_richcompare(PyDateTime_Time *self, PyObject *other, int op)
|
||||||
other->ob_type->tp_name);
|
other->ob_type->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
/* Ignore utcoffsets if they have identical tzinfo members. This
|
if (classify_two_utcoffsets((PyObject *)self, &offset1, &n1,
|
||||||
* isn't an optimization, it's design. If utcoffset() doesn't ignore
|
other, &offset2, &n2) < 0)
|
||||||
* its argument, it may return different results for self and other
|
return NULL;
|
||||||
* 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,
|
/* 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 ==
|
* we get off cheap. Note that if they're both naive, offset1 ==
|
||||||
* offset2 == 0 at this point.
|
* offset2 == 0 at this point.
|
||||||
|
@ -4624,30 +4616,9 @@ datetimetz_subtract(PyObject *left, PyObject *right)
|
||||||
int offset1, offset2;
|
int offset1, offset2;
|
||||||
PyDateTime_Delta *delta;
|
PyDateTime_Delta *delta;
|
||||||
|
|
||||||
/* Ignore utcoffsets if they have identical tzinfo
|
if (classify_two_utcoffsets(left, &offset1, &n1,
|
||||||
* members. This isn't an optimization, it's design.
|
right, &offset2, &n2) < 0)
|
||||||
* If utcoffset() doesn't ignore its argument, it may
|
return NULL;
|
||||||
* 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) {
|
if (n1 != n2) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"can't subtract offset-naive and "
|
"can't subtract offset-naive and "
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue