Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 277c840

Browse files
authored
Cleanup pytime.c (#3955)
* Move _PyTime_overflow() at the top * Move assertion on numerator into _PyTime_ObjectToDenominator() * PEP 7: add { ... } to if blocks
1 parent 703ff38 commit 277c840

1 file changed

Lines changed: 76 additions & 54 deletions

File tree

Python/pytime.c

Lines changed: 76 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ error_time_t_overflow(void)
3535
"timestamp out of range for platform time_t");
3636
}
3737

38+
static void
39+
_PyTime_overflow(void)
40+
{
41+
PyErr_SetString(PyExc_OverflowError,
42+
"timestamp too large to convert to C _PyTime_t");
43+
}
44+
3845
time_t
3946
_PyLong_AsTime_t(PyObject *obj)
4047
{
@@ -47,8 +54,9 @@ _PyLong_AsTime_t(PyObject *obj)
4754
val = PyLong_AsLong(obj);
4855
#endif
4956
if (val == -1 && PyErr_Occurred()) {
50-
if (PyErr_ExceptionMatches(PyExc_OverflowError))
57+
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
5158
error_time_t_overflow();
59+
}
5260
return -1;
5361
}
5462
return (time_t)val;
@@ -71,9 +79,10 @@ static double
7179
_PyTime_RoundHalfEven(double x)
7280
{
7381
double rounded = round(x);
74-
if (fabs(x-rounded) == 0.5)
82+
if (fabs(x-rounded) == 0.5) {
7583
/* halfway case: round to even */
7684
rounded = 2.0*round(x/2.0);
85+
}
7786
return rounded;
7887
}
7988

@@ -84,19 +93,23 @@ _PyTime_Round(double x, _PyTime_round_t round)
8493
volatile double d;
8594

8695
d = x;
87-
if (round == _PyTime_ROUND_HALF_EVEN)
96+
if (round == _PyTime_ROUND_HALF_EVEN) {
8897
d = _PyTime_RoundHalfEven(d);
89-
else if (round == _PyTime_ROUND_CEILING)
98+
}
99+
else if (round == _PyTime_ROUND_CEILING) {
90100
d = ceil(d);
91-
else
101+
}
102+
else {
92103
d = floor(d);
104+
}
93105
return d;
94106
}
95107

96108
static int
97109
_PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
98-
double denominator, _PyTime_round_t round)
110+
long idenominator, _PyTime_round_t round)
99111
{
112+
double denominator = (double)idenominator;
100113
double intpart;
101114
/* volatile avoids optimization changing how numbers are rounded */
102115
volatile double floatpart;
@@ -121,15 +134,15 @@ _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
121134
}
122135
*sec = (time_t)intpart;
123136
*numerator = (long)floatpart;
124-
137+
assert(0 <= *numerator && *numerator < idenominator);
125138
return 0;
126139
}
127140

128141
static int
129142
_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
130-
double denominator, _PyTime_round_t round)
143+
long denominator, _PyTime_round_t round)
131144
{
132-
assert(denominator <= (double)LONG_MAX);
145+
assert(denominator >= 1);
133146

134147
if (PyFloat_Check(obj)) {
135148
double d = PyFloat_AsDouble(obj);
@@ -144,8 +157,9 @@ _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
144157
else {
145158
*sec = _PyLong_AsTime_t(obj);
146159
*numerator = 0;
147-
if (*sec == (time_t)-1 && PyErr_Occurred())
160+
if (*sec == (time_t)-1 && PyErr_Occurred()) {
148161
return -1;
162+
}
149163
return 0;
150164
}
151165
}
@@ -176,8 +190,9 @@ _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
176190
}
177191
else {
178192
*sec = _PyLong_AsTime_t(obj);
179-
if (*sec == (time_t)-1 && PyErr_Occurred())
193+
if (*sec == (time_t)-1 && PyErr_Occurred()) {
180194
return -1;
195+
}
181196
return 0;
182197
}
183198
}
@@ -186,43 +201,27 @@ int
186201
_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
187202
_PyTime_round_t round)
188203
{
189-
int res;
190-
res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
191-
if (res == 0) {
192-
assert(0 <= *nsec && *nsec < SEC_TO_NS);
193-
}
194-
return res;
204+
return _PyTime_ObjectToDenominator(obj, sec, nsec, SEC_TO_NS, round);
195205
}
196206

197207
int
198208
_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
199209
_PyTime_round_t round)
200210
{
201-
int res;
202-
res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
203-
if (res == 0) {
204-
assert(0 <= *usec && *usec < SEC_TO_US);
205-
}
206-
return res;
207-
}
208-
209-
static void
210-
_PyTime_overflow(void)
211-
{
212-
PyErr_SetString(PyExc_OverflowError,
213-
"timestamp too large to convert to C _PyTime_t");
211+
return _PyTime_ObjectToDenominator(obj, sec, usec, SEC_TO_US, round);
214212
}
215213

216214
_PyTime_t
217215
_PyTime_FromSeconds(int seconds)
218216
{
219217
_PyTime_t t;
220-
t = (_PyTime_t)seconds;
221218
/* ensure that integer overflow cannot happen, int type should have 32
222219
bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
223220
bits). */
224221
Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
225222
Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
223+
224+
t = (_PyTime_t)seconds;
226225
assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
227226
|| (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
228227
t *= SEC_TO_NS;
@@ -249,8 +248,9 @@ _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
249248
t = (_PyTime_t)ts->tv_sec;
250249

251250
if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
252-
if (raise)
251+
if (raise) {
253252
_PyTime_overflow();
253+
}
254254
res = -1;
255255
}
256256
t = t * SEC_TO_NS;
@@ -271,8 +271,9 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
271271
t = (_PyTime_t)tv->tv_sec;
272272

273273
if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
274-
if (raise)
274+
if (raise) {
275275
_PyTime_overflow();
276+
}
276277
res = -1;
277278
}
278279
t = t * SEC_TO_NS;
@@ -323,8 +324,9 @@ _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
323324

324325
sec = PyLong_AsLongLong(obj);
325326
if (sec == -1 && PyErr_Occurred()) {
326-
if (PyErr_ExceptionMatches(PyExc_OverflowError))
327+
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
327328
_PyTime_overflow();
329+
}
328330
return -1;
329331
}
330332

@@ -387,24 +389,30 @@ _PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
387389
r = t % k;
388390
abs_r = Py_ABS(r);
389391
if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
390-
if (t >= 0)
392+
if (t >= 0) {
391393
x++;
392-
else
394+
}
395+
else {
393396
x--;
397+
}
394398
}
395399
return x;
396400
}
397401
else if (round == _PyTime_ROUND_CEILING) {
398-
if (t >= 0)
402+
if (t >= 0) {
399403
return (t + k - 1) / k;
400-
else
404+
}
405+
else {
401406
return t / k;
407+
}
402408
}
403409
else {
404-
if (t >= 0)
410+
if (t >= 0) {
405411
return t / k;
406-
else
412+
}
413+
else {
407414
return (t - (k - 1)) / k;
415+
}
408416
}
409417
}
410418

@@ -434,17 +442,21 @@ _PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us,
434442
usec = (int)_PyTime_Divide(ns, US_TO_NS, round);
435443
if (usec < 0) {
436444
usec += SEC_TO_US;
437-
if (secs != _PyTime_MIN)
445+
if (secs != _PyTime_MIN) {
438446
secs -= 1;
439-
else
447+
}
448+
else {
440449
res = -1;
450+
}
441451
}
442452
else if (usec >= SEC_TO_US) {
443453
usec -= SEC_TO_US;
444-
if (secs != _PyTime_MAX)
454+
if (secs != _PyTime_MAX) {
445455
secs += 1;
446-
else
456+
}
457+
else {
447458
res = -1;
459+
}
448460
}
449461
assert(0 <= usec && usec < SEC_TO_US);
450462

@@ -473,8 +485,9 @@ _PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv,
473485

474486
secs2 = (_PyTime_t)tv->tv_sec;
475487
if (res < 0 || secs2 != secs) {
476-
if (raise)
488+
if (raise) {
477489
error_time_t_overflow();
490+
}
478491
return -1;
479492
}
480493
return 0;
@@ -580,22 +593,26 @@ pygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
580593
#ifdef HAVE_CLOCK_GETTIME
581594
err = clock_gettime(CLOCK_REALTIME, &ts);
582595
if (err) {
583-
if (raise)
596+
if (raise) {
584597
PyErr_SetFromErrno(PyExc_OSError);
598+
}
585599
return -1;
586600
}
587-
if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
601+
if (_PyTime_FromTimespec(tp, &ts, raise) < 0) {
588602
return -1;
603+
}
589604

590605
if (info) {
591606
struct timespec res;
592607
info->implementation = "clock_gettime(CLOCK_REALTIME)";
593608
info->monotonic = 0;
594609
info->adjustable = 1;
595-
if (clock_getres(CLOCK_REALTIME, &res) == 0)
610+
if (clock_getres(CLOCK_REALTIME, &res) == 0) {
596611
info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
597-
else
612+
}
613+
else {
598614
info->resolution = 1e-9;
615+
}
599616
}
600617
#else /* HAVE_CLOCK_GETTIME */
601618

@@ -606,12 +623,14 @@ pygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
606623
err = gettimeofday(&tv, (struct timezone *)NULL);
607624
#endif
608625
if (err) {
609-
if (raise)
626+
if (raise) {
610627
PyErr_SetFromErrno(PyExc_OSError);
628+
}
611629
return -1;
612630
}
613-
if (_PyTime_FromTimeval(tp, &tv, raise) < 0)
631+
if (_PyTime_FromTimeval(tp, &tv, raise) < 0) {
614632
return -1;
633+
}
615634

616635
if (info) {
617636
info->implementation = "gettimeofday()";
@@ -755,8 +774,9 @@ pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
755774
}
756775
info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
757776
}
758-
if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
777+
if (_PyTime_FromTimespec(tp, &ts, raise) < 0) {
759778
return -1;
779+
}
760780
#endif
761781
return 0;
762782
}
@@ -872,8 +892,9 @@ _PyTime_localtime(time_t t, struct tm *tm)
872892
#else /* !MS_WINDOWS */
873893
if (localtime_r(&t, tm) == NULL) {
874894
#ifdef EINVAL
875-
if (errno == 0)
895+
if (errno == 0) {
876896
errno = EINVAL;
897+
}
877898
#endif
878899
PyErr_SetFromErrno(PyExc_OSError);
879900
return -1;
@@ -898,8 +919,9 @@ _PyTime_gmtime(time_t t, struct tm *tm)
898919
#else /* !MS_WINDOWS */
899920
if (gmtime_r(&t, tm) == NULL) {
900921
#ifdef EINVAL
901-
if (errno == 0)
922+
if (errno == 0) {
902923
errno = EINVAL;
924+
}
903925
#endif
904926
PyErr_SetFromErrno(PyExc_OSError);
905927
return -1;

0 commit comments

Comments
 (0)