Skip to content

Commit e444752

Browse files
authored
gh-74953: Add _PyTime_FromMicrosecondsClamp() function (#93942)
1 parent 1735710 commit e444752

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

Include/cpython/pytime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int seconds);
130130
/* Create a timestamp from a number of nanoseconds. */
131131
PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(_PyTime_t ns);
132132

133+
/* Create a timestamp from a number of microseconds.
134+
* Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow. */
135+
PyAPI_FUNC(_PyTime_t) _PyTime_FromMicrosecondsClamp(_PyTime_t us);
136+
133137
/* Create a timestamp from nanoseconds (Python int). */
134138
PyAPI_FUNC(int) _PyTime_FromNanosecondsObject(_PyTime_t *t,
135139
PyObject *obj);

Python/pytime.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,14 @@ _PyTime_FromNanoseconds(_PyTime_t ns)
406406
}
407407

408408

409+
_PyTime_t
410+
_PyTime_FromMicrosecondsClamp(_PyTime_t us)
411+
{
412+
_PyTime_t ns = _PyTime_Mul(us, US_TO_NS);
413+
return pytime_from_nanoseconds(ns);
414+
}
415+
416+
409417
int
410418
_PyTime_FromNanosecondsObject(_PyTime_t *tp, PyObject *obj)
411419
{

Python/thread_pthread.h

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -438,22 +438,15 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
438438

439439
_PyTime_t timeout; // relative timeout
440440
if (microseconds >= 0) {
441-
_PyTime_t ns;
442-
if (microseconds <= _PyTime_MAX / 1000) {
443-
ns = microseconds * 1000;
444-
}
445-
else {
446-
// bpo-41710: PyThread_acquire_lock_timed() cannot report timeout
447-
// overflow to the caller, so clamp the timeout to
448-
// [_PyTime_MIN, _PyTime_MAX].
449-
//
450-
// _PyTime_MAX nanoseconds is around 292.3 years.
451-
//
452-
// _thread.Lock.acquire() and _thread.RLock.acquire() raise an
453-
// OverflowError if microseconds is greater than PY_TIMEOUT_MAX.
454-
ns = _PyTime_MAX;
455-
}
456-
timeout = _PyTime_FromNanoseconds(ns);
441+
// bpo-41710: PyThread_acquire_lock_timed() cannot report timeout
442+
// overflow to the caller, so clamp the timeout to
443+
// [_PyTime_MIN, _PyTime_MAX].
444+
//
445+
// _PyTime_MAX nanoseconds is around 292.3 years.
446+
//
447+
// _thread.Lock.acquire() and _thread.RLock.acquire() raise an
448+
// OverflowError if microseconds is greater than PY_TIMEOUT_MAX.
449+
timeout = _PyTime_FromMicrosecondsClamp(microseconds);
457450
}
458451
else {
459452
timeout = _PyTime_FromNanoseconds(-1);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy