Skip to content

Commit d2a1014

Browse files
committed
stay with PD_CHECK_OVERFLOW
1 parent e51d4f9 commit d2a1014

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

pandas/_libs/src/vendored/numpy/datetime/np_datetime.c

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,14 @@ _Static_assert(0, "__has_builtin not detected; please try a newer compiler");
7070
#endif
7171
#endif
7272

73-
#define XSTR(a) STR(a)
74-
#define STR(a) #a
75-
76-
#define PD_RAISE_FOR_OVERFLOW \
77-
PyGILState_STATE gstate = PyGILState_Ensure(); \
78-
PyErr_SetString(PyExc_OverflowError, \
79-
"Overflow occurred at " __FILE__ ":" XSTR(__LINE__)); \
80-
PyGILState_Release(gstate); \
81-
return -1;
82-
83-
#define PD_CHECK_OVERFLOW(EXPR) \
73+
#define PD_CHECK_OVERFLOW(FUNC) \
8474
do { \
85-
if ((EXPR) != 0) { \
86-
PD_RAISE_FOR_OVERFLOW \
75+
if ((FUNC) != 0) { \
76+
PyGILState_STATE gstate = PyGILState_Ensure(); \
77+
PyErr_SetString(PyExc_OverflowError, \
78+
"Overflow occurred in npy_datetimestruct_to_datetime"); \
79+
PyGILState_Release(gstate); \
80+
return -1; \
8781
} \
8882
} while (0)
8983

@@ -156,59 +150,55 @@ npy_int64 get_datetimestruct_days(const npy_datetimestruct *dts) {
156150
int i, month;
157151
npy_int64 year, days = 0;
158152
const int *month_lengths;
159-
int did_overflow = 0;
160153

161-
did_overflow |= checked_int64_sub(dts->year, 1970, &year);
162-
did_overflow |= checked_int64_mul(year, 365, &days);
154+
PD_CHECK_OVERFLOW(checked_int64_sub(dts->year, 1970, &year));
155+
PD_CHECK_OVERFLOW(checked_int64_mul(year, 365, &days));
163156

164157
/* Adjust for leap years */
165158
if (days >= 0) {
166159
/*
167160
* 1968 is the closest leap year before 1970.
168161
* Exclude the current year, so add 1.
169162
*/
170-
did_overflow |= checked_int64_add(year, 1, &year);
163+
PD_CHECK_OVERFLOW(checked_int64_add(year, 1, &year));
171164
/* Add one day for each 4 years */
172-
did_overflow |= checked_int64_add(days, year / 4, &days);
165+
PD_CHECK_OVERFLOW(checked_int64_add(days, year / 4, &days));
173166
/* 1900 is the closest previous year divisible by 100 */
174-
did_overflow |= checked_int64_add(year, 68, &year);
167+
PD_CHECK_OVERFLOW(checked_int64_add(year, 68, &year));
175168
/* Subtract one day for each 100 years */
176-
did_overflow |= checked_int64_sub(days, year / 100, &days);
169+
PD_CHECK_OVERFLOW(checked_int64_sub(days, year / 100, &days));
177170
/* 1600 is the closest previous year divisible by 400 */
178-
did_overflow |= checked_int64_add(year, 300, &year);
171+
PD_CHECK_OVERFLOW(checked_int64_add(year, 300, &year));
179172
/* Add one day for each 400 years */
180-
did_overflow |= checked_int64_add(days, year / 400, &days);
173+
PD_CHECK_OVERFLOW(checked_int64_add(days, year / 400, &days));
181174
} else {
182175
/*
183176
* 1972 is the closest later year after 1970.
184177
* Include the current year, so subtract 2.
185178
*/
186-
did_overflow |= checked_int64_sub(year, 2, &year);
179+
PD_CHECK_OVERFLOW(checked_int64_sub(year, 2, &year));
187180
/* Subtract one day for each 4 years */
188-
did_overflow |= checked_int64_add(days, year / 4, &days);
181+
PD_CHECK_OVERFLOW(checked_int64_add(days, year / 4, &days));
189182
/* 2000 is the closest later year divisible by 100 */
190-
did_overflow |= checked_int64_sub(year, 28, &year);
183+
PD_CHECK_OVERFLOW(checked_int64_sub(year, 28, &year));
191184
/* Add one day for each 100 years */
192-
did_overflow |= checked_int64_sub(days, year / 100, &days);
185+
PD_CHECK_OVERFLOW(checked_int64_sub(days, year / 100, &days));
193186
/* 2000 is also the closest later year divisible by 400 */
194187
/* Subtract one day for each 400 years */
195-
did_overflow |= checked_int64_add(days, year / 400, &days);
188+
PD_CHECK_OVERFLOW(checked_int64_add(days, year / 400, &days));
196189
}
197190

198191
month_lengths = days_per_month_table[is_leapyear(dts->year)];
199192
month = dts->month - 1;
200193

201194
/* Add the months */
202195
for (i = 0; i < month; ++i) {
203-
did_overflow |= checked_int64_add(days, month_lengths[i], &days);
196+
PD_CHECK_OVERFLOW(checked_int64_add(days, month_lengths[i], &days));
204197
}
205198

206199
/* Add the days */
207-
did_overflow |= checked_int64_add(days, dts->day - 1, &days);
200+
PD_CHECK_OVERFLOW(checked_int64_add(days, dts->day - 1, &days));
208201

209-
if (did_overflow) {
210-
PD_RAISE_FOR_OVERFLOW;
211-
}
212202
return days;
213203
}
214204

0 commit comments

Comments
 (0)