@@ -268,11 +268,8 @@ static PyObject *
268
268
semlock_acquire (SemLockObject * self , PyObject * args , PyObject * kwds )
269
269
{
270
270
int blocking = 1 , res , err = 0 ;
271
- double timeout ;
272
271
PyObject * timeout_obj = Py_None ;
273
272
struct timespec deadline = {0 };
274
- struct timeval now ;
275
- long sec , nsec ;
276
273
277
274
static char * kwlist [] = {"block" , "timeout" , NULL };
278
275
@@ -285,19 +282,23 @@ semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
285
282
Py_RETURN_TRUE ;
286
283
}
287
284
288
- if (timeout_obj != Py_None ) {
289
- timeout = PyFloat_AsDouble (timeout_obj );
290
- if (PyErr_Occurred ())
285
+ int use_deadline = (timeout_obj != Py_None );
286
+ if (use_deadline ) {
287
+ double timeout = PyFloat_AsDouble (timeout_obj );
288
+ if (PyErr_Occurred ()) {
291
289
return NULL ;
292
- if (timeout < 0.0 )
290
+ }
291
+ if (timeout < 0.0 ) {
293
292
timeout = 0.0 ;
293
+ }
294
294
295
+ struct timeval now ;
295
296
if (gettimeofday (& now , NULL ) < 0 ) {
296
297
PyErr_SetFromErrno (PyExc_OSError );
297
298
return NULL ;
298
299
}
299
- sec = (long ) timeout ;
300
- nsec = (long ) (1e9 * (timeout - sec ) + 0.5 );
300
+ long sec = (long ) timeout ;
301
+ long nsec = (long ) (1e9 * (timeout - sec ) + 0.5 );
301
302
deadline .tv_sec = now .tv_sec + sec ;
302
303
deadline .tv_nsec = now .tv_usec * 1000 + nsec ;
303
304
deadline .tv_sec += (deadline .tv_nsec / 1000000000 );
@@ -315,7 +316,7 @@ semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
315
316
/* Couldn't acquire immediately, need to block */
316
317
do {
317
318
Py_BEGIN_ALLOW_THREADS
318
- if (timeout_obj == Py_None ) {
319
+ if (! use_deadline ) {
319
320
res = sem_wait (self -> handle );
320
321
}
321
322
else {
0 commit comments