Skip to content

Commit 60e4b87

Browse files
Address review: use the PyFloat_AsDouble() error message and rename ns to units
1 parent 0be46e4 commit 60e4b87

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

‎Lib/test/test_time.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ def check_ratio_rounding(self, pytime_converter, expected_func,
11011101
pytime_converter(decimal.Decimal('nan'), time_rnd)
11021102
# a type is not a number
11031103
for value in (int, fractions.Fraction, RatioNumber):
1104-
with self.assertRaisesRegex(TypeError, 'not type'):
1104+
with self.assertRaisesRegex(TypeError, 'must be real number'):
11051105
pytime_converter(value, time_rnd)
11061106

11071107

‎Python/pytime.c‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ maybe_as_integer_ratio(PyObject *number, PyObject **ratio)
462462
{
463463
*ratio = NULL;
464464
if (PyType_Check(number)) {
465-
PyErr_SetString(PyExc_TypeError,
466-
"required a number, not type");
465+
PyErr_Format(PyExc_TypeError,
466+
"must be real number, not %T", number);
467467
return -1;
468468
}
469469
PyObject *meth;
@@ -960,8 +960,8 @@ pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
960960
Py_DECREF(ratio);
961961
return -1;
962962
}
963-
PyTime_t ns;
964-
if (PyLong_AsInt64(PyTuple_GET_ITEM(divmod, 0), &ns) < 0) {
963+
PyTime_t units;
964+
if (PyLong_AsInt64(PyTuple_GET_ITEM(divmod, 0), &units) < 0) {
965965
Py_DECREF(divmod);
966966
Py_DECREF(ratio);
967967
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
@@ -972,7 +972,7 @@ pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
972972
if (round == _PyTime_ROUND_UP) {
973973
/* The remainder is non-negative, so the direction of rounding
974974
depends on the sign of the whole number. */
975-
round = ns < 0 ? _PyTime_ROUND_FLOOR : _PyTime_ROUND_CEILING;
975+
round = units < 0 ? _PyTime_ROUND_FLOOR : _PyTime_ROUND_CEILING;
976976
}
977977
PyObject *fracobj = multiply_divide_and_round(
978978
unit_to_ns, PyTuple_GET_ITEM(divmod, 1),
@@ -987,16 +987,18 @@ pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
987987
if (frac == -1 && PyErr_Occurred()) {
988988
return -1;
989989
}
990-
if (ns < 0 && frac > 0) {
990+
if (units < 0 && frac > 0) {
991991
/* Avoid a spurious overflow near PyTime_MIN. */
992-
ns += 1;
992+
units += 1;
993993
frac -= unit_to_ns;
994994
}
995-
if (pytime_mul(&ns, unit_to_ns) < 0 || pytime_add(&ns, frac) < 0) {
995+
if (pytime_mul(&units, unit_to_ns) < 0
996+
|| pytime_add(&units, frac) < 0)
997+
{
996998
pytime_overflow();
997999
return -1;
9981000
}
999-
*tp = ns;
1001+
*tp = units;
10001002
return 0;
10011003
}
10021004
}

0 commit comments

Comments
 (0)