Skip to content

Commit 42e5b0c

Browse files
Use helper functions instead of goto for the float case
Also simplify checked_divmod() and clarify the comment of maybe_as_integer_ratio(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1a73776 commit 42e5b0c

1 file changed

Lines changed: 88 additions & 66 deletions

File tree

‎Python/pytime.c‎

Lines changed: 88 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ pytime_double_to_denominator(double d, time_t *sec, long *numerator,
453453
/* Convert a number to a fraction representation.
454454
*
455455
* Set *ratio to a 2-tuple (numerator, denominator) and return 1 on success.
456-
* Return 0 if the number has neither the as_integer_ratio() method nor
457-
* the numerator and denominator attributes.
456+
* Set *ratio to NULL and return 0 if the number has neither the
457+
* as_integer_ratio() method nor the numerator and denominator attributes.
458458
* Return -1 on error.
459459
*/
460460
static int
@@ -515,21 +515,22 @@ static PyObject *
515515
checked_divmod(PyObject *a, PyObject *b)
516516
{
517517
PyObject *result = PyNumber_Divmod(a, b);
518-
if (result != NULL) {
519-
if (!PyTuple_Check(result)) {
520-
PyErr_Format(PyExc_TypeError,
521-
"divmod() returned non-tuple (type %T)",
522-
result);
523-
Py_DECREF(result);
524-
return NULL;
525-
}
526-
if (PyTuple_GET_SIZE(result) != 2) {
527-
PyErr_Format(PyExc_TypeError,
528-
"divmod() returned a tuple of size %zd",
529-
PyTuple_GET_SIZE(result));
530-
Py_DECREF(result);
531-
return NULL;
532-
}
518+
if (result == NULL) {
519+
return NULL;
520+
}
521+
if (!PyTuple_Check(result)) {
522+
PyErr_Format(PyExc_TypeError,
523+
"divmod() returned non-tuple (type %T)",
524+
result);
525+
Py_DECREF(result);
526+
return NULL;
527+
}
528+
if (PyTuple_GET_SIZE(result) != 2) {
529+
PyErr_Format(PyExc_TypeError,
530+
"divmod() returned a tuple of size %zd",
531+
PyTuple_GET_SIZE(result));
532+
Py_DECREF(result);
533+
return NULL;
533534
}
534535
return result;
535536
}
@@ -644,6 +645,23 @@ pytime_ratio_to_denominator(PyObject *numerator, PyObject *denominator,
644645
}
645646

646647

648+
static int
649+
pytime_pyfloat_to_denominator(PyObject *obj, time_t *sec, long *numerator,
650+
long denominator, _PyTime_round_t round)
651+
{
652+
double d = PyFloat_AsDouble(obj);
653+
if (d == -1 && PyErr_Occurred()) {
654+
return -1;
655+
}
656+
if (isnan(d)) {
657+
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
658+
return -1;
659+
}
660+
return pytime_double_to_denominator(d, sec, numerator,
661+
denominator, round);
662+
}
663+
664+
647665
static int
648666
pytime_object_to_denominator(PyObject *obj, time_t *sec, long *numerator,
649667
long denominator, _PyTime_round_t round)
@@ -659,25 +677,17 @@ pytime_object_to_denominator(PyObject *obj, time_t *sec, long *numerator,
659677
return 0;
660678
}
661679
else if (PyFloat_Check(obj)) {
662-
fromfloat:;
663-
double d = PyFloat_AsDouble(obj);
664-
if (d == -1 && PyErr_Occurred()) {
665-
return -1;
666-
}
667-
if (isnan(d)) {
668-
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
669-
return -1;
670-
}
671-
return pytime_double_to_denominator(d, sec, numerator,
672-
denominator, round);
680+
return pytime_pyfloat_to_denominator(obj, sec, numerator,
681+
denominator, round);
673682
}
674683
else {
675684
PyObject *ratio;
676685
if (maybe_as_integer_ratio(obj, &ratio) < 0) {
677686
return -1;
678687
}
679688
if (ratio == NULL) {
680-
goto fromfloat;
689+
return pytime_pyfloat_to_denominator(obj, sec, numerator,
690+
denominator, round);
681691
}
682692
int rc = pytime_ratio_to_denominator(PyTuple_GET_ITEM(ratio, 0),
683693
PyTuple_GET_ITEM(ratio, 1),
@@ -689,6 +699,35 @@ fromfloat:;
689699
}
690700

691701

702+
static int
703+
pytime_pyfloat_to_time_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
704+
{
705+
double intpart;
706+
/* volatile avoids optimization changing how numbers are rounded */
707+
volatile double d;
708+
709+
d = PyFloat_AsDouble(obj);
710+
if (d == -1 && PyErr_Occurred()) {
711+
return -1;
712+
}
713+
if (isnan(d)) {
714+
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
715+
return -1;
716+
}
717+
718+
d = pytime_round(d, round);
719+
(void)modf(d, &intpart);
720+
721+
/* See comments in pytime_double_to_denominator */
722+
if (!((double)PY_TIME_T_MIN <= intpart && intpart < -(double)PY_TIME_T_MIN)) {
723+
pytime_time_t_overflow();
724+
return -1;
725+
}
726+
*sec = (time_t)intpart;
727+
return 0;
728+
}
729+
730+
692731
int
693732
_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
694733
{
@@ -700,38 +739,15 @@ _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
700739
return 0;
701740
}
702741
else if (PyFloat_Check(obj)) {
703-
fromfloat:;
704-
double intpart;
705-
/* volatile avoids optimization changing how numbers are rounded */
706-
volatile double d;
707-
708-
d = PyFloat_AsDouble(obj);
709-
if (d == -1 && PyErr_Occurred()) {
710-
return -1;
711-
}
712-
if (isnan(d)) {
713-
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
714-
return -1;
715-
}
716-
717-
d = pytime_round(d, round);
718-
(void)modf(d, &intpart);
719-
720-
/* See comments in pytime_double_to_denominator */
721-
if (!((double)PY_TIME_T_MIN <= intpart && intpart < -(double)PY_TIME_T_MIN)) {
722-
pytime_time_t_overflow();
723-
return -1;
724-
}
725-
*sec = (time_t)intpart;
726-
return 0;
742+
return pytime_pyfloat_to_time_t(obj, sec, round);
727743
}
728744
else {
729745
PyObject *ratio;
730746
if (maybe_as_integer_ratio(obj, &ratio) < 0) {
731747
return -1;
732748
}
733749
if (ratio == NULL) {
734-
goto fromfloat;
750+
return pytime_pyfloat_to_time_t(obj, sec, round);
735751
}
736752
PyObject *secobj = divide_and_round(PyTuple_GET_ITEM(ratio, 0),
737753
PyTuple_GET_ITEM(ratio, 1),
@@ -879,6 +895,22 @@ pytime_from_double(PyTime_t *tp, double value, _PyTime_round_t round,
879895
}
880896

881897

898+
static int
899+
pytime_from_pyfloat(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
900+
long unit_to_ns)
901+
{
902+
double d = PyFloat_AsDouble(obj);
903+
if (d == -1 && PyErr_Occurred()) {
904+
return -1;
905+
}
906+
if (isnan(d)) {
907+
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
908+
return -1;
909+
}
910+
return pytime_from_double(tp, d, round, unit_to_ns);
911+
}
912+
913+
882914
static int
883915
pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
884916
long unit_to_ns)
@@ -904,25 +936,15 @@ pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
904936
return 0;
905937
}
906938
else if (PyFloat_Check(obj)) {
907-
fromfloat:;
908-
double d;
909-
d = PyFloat_AsDouble(obj);
910-
if (d == -1 && PyErr_Occurred()) {
911-
return -1;
912-
}
913-
if (isnan(d)) {
914-
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
915-
return -1;
916-
}
917-
return pytime_from_double(tp, d, round, unit_to_ns);
939+
return pytime_from_pyfloat(tp, obj, round, unit_to_ns);
918940
}
919941
else {
920942
PyObject *ratio;
921943
if (maybe_as_integer_ratio(obj, &ratio) < 0) {
922944
return -1;
923945
}
924946
if (ratio == NULL) {
925-
goto fromfloat;
947+
return pytime_from_pyfloat(tp, obj, round, unit_to_ns);
926948
}
927949
PyObject *nsobj = multiply_divide_and_round(unit_to_ns,
928950
PyTuple_GET_ITEM(ratio, 0),

0 commit comments

Comments
 (0)