@@ -70,20 +70,14 @@ _Static_assert(0, "__has_builtin not detected; please try a newer compiler");
70
70
#endif
71
71
#endif
72
72
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 ) \
84
74
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; \
87
81
} \
88
82
} while (0)
89
83
@@ -156,59 +150,55 @@ npy_int64 get_datetimestruct_days(const npy_datetimestruct *dts) {
156
150
int i , month ;
157
151
npy_int64 year , days = 0 ;
158
152
const int * month_lengths ;
159
- int did_overflow = 0 ;
160
153
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 ) );
163
156
164
157
/* Adjust for leap years */
165
158
if (days >= 0 ) {
166
159
/*
167
160
* 1968 is the closest leap year before 1970.
168
161
* Exclude the current year, so add 1.
169
162
*/
170
- did_overflow |= checked_int64_add (year , 1 , & year );
163
+ PD_CHECK_OVERFLOW ( checked_int64_add (year , 1 , & year ) );
171
164
/* 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 ) );
173
166
/* 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 ) );
175
168
/* 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 ) );
177
170
/* 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 ) );
179
172
/* 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 ) );
181
174
} else {
182
175
/*
183
176
* 1972 is the closest later year after 1970.
184
177
* Include the current year, so subtract 2.
185
178
*/
186
- did_overflow |= checked_int64_sub (year , 2 , & year );
179
+ PD_CHECK_OVERFLOW ( checked_int64_sub (year , 2 , & year ) );
187
180
/* 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 ) );
189
182
/* 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 ) );
191
184
/* 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 ) );
193
186
/* 2000 is also the closest later year divisible by 400 */
194
187
/* 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 ) );
196
189
}
197
190
198
191
month_lengths = days_per_month_table [is_leapyear (dts -> year )];
199
192
month = dts -> month - 1 ;
200
193
201
194
/* Add the months */
202
195
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 ) );
204
197
}
205
198
206
199
/* 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 ) );
208
201
209
- if (did_overflow ) {
210
- PD_RAISE_FOR_OVERFLOW ;
211
- }
212
202
return days ;
213
203
}
214
204
0 commit comments