Skip to content

Commit b77edfd

Browse files
gh-67795: Accept any real numbers as timestamp and timeout
Functions that take timestamp or timeout arguments now accept any real numbers, not only integers or floats, although this does not improve precision.
1 parent a756a4b commit b77edfd

18 files changed

+295
-94
lines changed

Doc/library/datetime.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,9 @@ Other constructors, all class methods:
535535
:c:func:`localtime` function. Raise :exc:`OSError` instead of
536536
:exc:`ValueError` on :c:func:`localtime` failure.
537537

538+
.. versionchanged:: next
539+
Accepts any real number as *timestamp*, not only integer or float.
540+
538541

539542
.. classmethod:: date.fromordinal(ordinal)
540543

@@ -1020,6 +1023,10 @@ Other constructors, all class methods:
10201023
.. versionchanged:: 3.6
10211024
:meth:`fromtimestamp` may return instances with :attr:`.fold` set to 1.
10221025

1026+
.. versionchanged:: next
1027+
Accepts any real number as *timestamp*, not only integer or float.
1028+
1029+
10231030
.. classmethod:: datetime.utcfromtimestamp(timestamp)
10241031

10251032
Return the UTC :class:`.datetime` corresponding to the POSIX timestamp, with
@@ -1060,6 +1067,9 @@ Other constructors, all class methods:
10601067

10611068
Use :meth:`datetime.fromtimestamp` with :const:`UTC` instead.
10621069

1070+
.. versionchanged:: next
1071+
Accepts any real number as *timestamp*, not only integer or float.
1072+
10631073

10641074
.. classmethod:: datetime.fromordinal(ordinal)
10651075

Doc/library/os.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,7 +3618,8 @@ features:
36183618
where each member is an int expressing nanoseconds.
36193619
- If *times* is not ``None``,
36203620
it must be a 2-tuple of the form ``(atime, mtime)``
3621-
where each member is an int or float expressing seconds.
3621+
where each member is a real number expressing seconds,
3622+
rounded down to nanoseconds.
36223623
- If *times* is ``None`` and *ns* is unspecified,
36233624
this is equivalent to specifying ``ns=(atime_ns, mtime_ns)``
36243625
where both times are the current time.
@@ -3645,6 +3646,9 @@ features:
36453646
.. versionchanged:: 3.6
36463647
Accepts a :term:`path-like object`.
36473648

3649+
.. versionchanged:: next
3650+
Accepts any real numbers as *times*, not only integers or floats.
3651+
36483652

36493653
.. function:: walk(top, topdown=True, onerror=None, followlinks=False)
36503654

@@ -4050,7 +4054,7 @@ Naturally, they are all only available on Linux.
40504054
the timer will fire when the timer's clock
40514055
(set by *clockid* in :func:`timerfd_create`) reaches *initial* seconds.
40524056

4053-
The timer's interval is set by the *interval* :py:class:`float`.
4057+
The timer's interval is set by the *interval* real number.
40544058
If *interval* is zero, the timer only fires once, on the initial expiration.
40554059
If *interval* is greater than zero, the timer fires every time *interval*
40564060
seconds have elapsed since the previous expiration.

Doc/library/select.rst

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ The module defines the following:
129129

130130
Empty iterables are allowed, but acceptance of three empty iterables is
131131
platform-dependent. (It is known to work on Unix but not on Windows.) The
132-
optional *timeout* argument specifies a time-out as a floating-point number
133-
in seconds. When the *timeout* argument is omitted the function blocks until
132+
optional *timeout* argument specifies a time-out in seconds; it may be
133+
a non-integer to specify fractions of seconds.
134+
When the *timeout* argument is omitted the function blocks until
134135
at least one file descriptor is ready. A time-out value of zero specifies a
135136
poll and never blocks.
136137

@@ -164,6 +165,9 @@ The module defines the following:
164165
:pep:`475` for the rationale), instead of raising
165166
:exc:`InterruptedError`.
166167

168+
.. versionchanged:: next
169+
Accepts any real number as *timeout*, not only integer or float.
170+
167171

168172
.. data:: PIPE_BUF
169173

@@ -270,6 +274,9 @@ object.
270274
:pep:`475` for the rationale), instead of raising
271275
:exc:`InterruptedError`.
272276

277+
.. versionchanged:: next
278+
Accepts any real number as *timeout*, not only integer or float.
279+
273280

274281
.. _epoll-objects:
275282

@@ -368,14 +375,19 @@ Edge and Level Trigger Polling (epoll) Objects
368375

369376
.. method:: epoll.poll(timeout=None, maxevents=-1)
370377

371-
Wait for events. timeout in seconds (float)
378+
Wait for events.
379+
If *timeout* is given, it specifies the length of time in seconds
380+
(may be non-integer) which the system will wait for events before returning.
372381

373382
.. versionchanged:: 3.5
374383
The function is now retried with a recomputed timeout when interrupted by
375384
a signal, except if the signal handler raises an exception (see
376385
:pep:`475` for the rationale), instead of raising
377386
:exc:`InterruptedError`.
378387

388+
.. versionchanged:: next
389+
Accepts any real number as *timeout*, not only integer or float.
390+
379391

380392
.. _poll-objects:
381393

@@ -464,6 +476,9 @@ linearly scanned again. :c:func:`!select` is *O*\ (*highest file descriptor*), w
464476
:pep:`475` for the rationale), instead of raising
465477
:exc:`InterruptedError`.
466478

479+
.. versionchanged:: next
480+
Accepts any real number as *timeout*, not only integer or float.
481+
467482

468483
.. _kqueue-objects:
469484

@@ -496,7 +511,7 @@ Kqueue Objects
496511

497512
- changelist must be an iterable of kevent objects or ``None``
498513
- max_events must be 0 or a positive integer
499-
- timeout in seconds (floats possible); the default is ``None``,
514+
- timeout in seconds (non-integers are possible); the default is ``None``,
500515
to wait forever
501516

502517
.. versionchanged:: 3.5
@@ -505,6 +520,9 @@ Kqueue Objects
505520
:pep:`475` for the rationale), instead of raising
506521
:exc:`InterruptedError`.
507522

523+
.. versionchanged:: next
524+
Accepts any real number as *timeout*, not only integer or float.
525+
508526

509527
.. _kevent-objects:
510528

Doc/library/signal.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,11 @@ The :mod:`signal` module defines the following functions:
478478
.. versionadded:: 3.3
479479

480480

481-
.. function:: setitimer(which, seconds, interval=0.0)
481+
.. function:: setitimer(which, seconds, interval=0)
482482

483483
Sets given interval timer (one of :const:`signal.ITIMER_REAL`,
484484
:const:`signal.ITIMER_VIRTUAL` or :const:`signal.ITIMER_PROF`) specified
485-
by *which* to fire after *seconds* (float is accepted, different from
485+
by *which* to fire after *seconds* (rounded up to microseconds, different from
486486
:func:`alarm`) and after that every *interval* seconds (if *interval*
487487
is non-zero). The interval timer specified by *which* can be cleared by
488488
setting *seconds* to zero.
@@ -493,13 +493,18 @@ The :mod:`signal` module defines the following functions:
493493
:const:`signal.ITIMER_VIRTUAL` sends :const:`SIGVTALRM`,
494494
and :const:`signal.ITIMER_PROF` will deliver :const:`SIGPROF`.
495495

496-
The old values are returned as a tuple: (delay, interval).
496+
The old values are returned as a two-tuple of floats:
497+
(``delay``, ``interval``).
497498

498499
Attempting to pass an invalid interval timer will cause an
499500
:exc:`ItimerError`.
500501

501502
.. availability:: Unix.
502503

504+
.. versionchanged:: next
505+
Accepts any real numbers as *seconds* and *interval*, not only integers
506+
or floats.
507+
503508

504509
.. function:: getitimer(which)
505510

@@ -676,6 +681,9 @@ The :mod:`signal` module defines the following functions:
676681
by a signal not in *sigset* and the signal handler does not raise an
677682
exception (see :pep:`475` for the rationale).
678683

684+
.. versionchanged:: next
685+
Accepts any real number as *timeout*, not only integer or float.
686+
679687

680688
.. _signal-example:
681689

Doc/library/socket.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,11 +1407,14 @@ The :mod:`socket` module also offers various network-related services:
14071407

14081408
.. function:: setdefaulttimeout(timeout)
14091409

1410-
Set the default timeout in seconds (float) for new socket objects. When
1410+
Set the default timeout in seconds (real number) for new socket objects. When
14111411
the socket module is first imported, the default is ``None``. See
14121412
:meth:`~socket.settimeout` for possible values and their respective
14131413
meanings.
14141414

1415+
.. versionchanged:: next
1416+
Accepts any real number, not only integer or float.
1417+
14151418

14161419
.. function:: sethostname(name)
14171420

@@ -2073,7 +2076,7 @@ to sockets.
20732076
.. method:: socket.settimeout(value)
20742077

20752078
Set a timeout on blocking socket operations. The *value* argument can be a
2076-
nonnegative floating-point number expressing seconds, or ``None``.
2079+
nonnegative real number expressing seconds, or ``None``.
20772080
If a non-zero value is given, subsequent socket operations will raise a
20782081
:exc:`timeout` exception if the timeout period *value* has elapsed before
20792082
the operation has completed. If zero is given, the socket is put in
@@ -2085,6 +2088,9 @@ to sockets.
20852088
The method no longer toggles :const:`SOCK_NONBLOCK` flag on
20862089
:attr:`socket.type`.
20872090

2091+
.. versionchanged:: next
2092+
Accepts any real number, not only integer or float.
2093+
20882094

20892095
.. method:: socket.setsockopt(level, optname, value: int)
20902096
.. method:: socket.setsockopt(level, optname, value: buffer)

Doc/library/threading.rst

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ since it is impossible to detect the termination of alien threads.
608608
timeout occurs.
609609

610610
When the *timeout* argument is present and not ``None``, it should be a
611-
floating-point number specifying a timeout for the operation in seconds
611+
real number specifying a timeout for the operation in seconds
612612
(or fractions thereof). As :meth:`~Thread.join` always returns ``None``,
613613
you must call :meth:`~Thread.is_alive` after :meth:`~Thread.join` to
614614
decide whether a timeout happened -- if the thread is still alive, the
@@ -632,6 +632,9 @@ since it is impossible to detect the termination of alien threads.
632632

633633
May raise :exc:`PythonFinalizationError`.
634634

635+
.. versionchanged:: next
636+
Accepts any real number as *timeout*, not only integer or float.
637+
635638
.. attribute:: name
636639

637640
A string used for identification purposes only. It has no semantics.
@@ -764,7 +767,7 @@ All methods are executed atomically.
764767
If a call with *blocking* set to ``True`` would block, return ``False``
765768
immediately; otherwise, set the lock to locked and return ``True``.
766769

767-
When invoked with the floating-point *timeout* argument set to a positive
770+
When invoked with the *timeout* argument set to a positive
768771
value, block for at most the number of seconds specified by *timeout*
769772
and as long as the lock cannot be acquired. A *timeout* argument of ``-1``
770773
specifies an unbounded wait. It is forbidden to specify a *timeout*
@@ -783,6 +786,9 @@ All methods are executed atomically.
783786
.. versionchanged:: 3.14
784787
Lock acquisition can now be interrupted by signals on Windows.
785788

789+
.. versionchanged:: next
790+
Accepts any real number as *timeout*, not only integer or float.
791+
786792

787793
.. method:: release()
788794

@@ -863,7 +869,7 @@ call release as many times the lock has been acquired can lead to deadlock.
863869
* If no thread owns the lock, acquire the lock and return immediately.
864870

865871
* If another thread owns the lock, block until we are able to acquire
866-
lock, or *timeout*, if set to a positive float value.
872+
lock, or *timeout*, if set to a positive value.
867873

868874
* If the same thread owns the lock, acquire the lock again, and
869875
return immediately. This is the difference between :class:`Lock` and
@@ -890,6 +896,9 @@ call release as many times the lock has been acquired can lead to deadlock.
890896
.. versionchanged:: 3.2
891897
The *timeout* parameter is new.
892898

899+
.. versionchanged:: next
900+
Accepts any real number as *timeout*, not only integer or float.
901+
893902

894903
.. method:: release()
895904

@@ -1023,7 +1032,7 @@ item to the buffer only needs to wake up one consumer thread.
10231032
occurs. Once awakened or timed out, it re-acquires the lock and returns.
10241033

10251034
When the *timeout* argument is present and not ``None``, it should be a
1026-
floating-point number specifying a timeout for the operation in seconds
1035+
real number specifying a timeout for the operation in seconds
10271036
(or fractions thereof).
10281037

10291038
When the underlying lock is an :class:`RLock`, it is not released using
@@ -1150,6 +1159,9 @@ Semaphores also support the :ref:`context management protocol <with-locks>`.
11501159
.. versionchanged:: 3.2
11511160
The *timeout* parameter is new.
11521161

1162+
.. versionchanged:: next
1163+
Accepts any real number as *timeout*, not only integer or float.
1164+
11531165
.. method:: release(n=1)
11541166

11551167
Release a semaphore, incrementing the internal counter by *n*. When it
@@ -1250,7 +1262,7 @@ method. The :meth:`~Event.wait` method blocks until the flag is true.
12501262
the internal flag did not become true within the given wait time.
12511263

12521264
When the timeout argument is present and not ``None``, it should be a
1253-
floating-point number specifying a timeout for the operation in seconds,
1265+
real number specifying a timeout for the operation in seconds,
12541266
or fractions thereof.
12551267

12561268
.. versionchanged:: 3.1

Doc/library/time.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Functions
189189
.. versionadded:: 3.7
190190

191191

192-
.. function:: clock_settime(clk_id, time: float)
192+
.. function:: clock_settime(clk_id, time)
193193

194194
Set the time of the specified clock *clk_id*. Currently,
195195
:data:`CLOCK_REALTIME` is the only accepted value for *clk_id*.
@@ -201,6 +201,9 @@ Functions
201201

202202
.. versionadded:: 3.3
203203

204+
.. versionchanged:: next
205+
Accepts any real number as *time*, not only integer or float.
206+
204207

205208
.. function:: clock_settime_ns(clk_id, time: int)
206209

@@ -223,6 +226,9 @@ Functions
223226
``asctime(localtime(secs))``. Locale information is not used by
224227
:func:`ctime`.
225228

229+
.. versionchanged:: next
230+
Accepts any real number, not only integer or float.
231+
226232

227233
.. function:: get_clock_info(name)
228234

@@ -258,6 +264,9 @@ Functions
258264
:class:`struct_time` object. See :func:`calendar.timegm` for the inverse of this
259265
function.
260266

267+
.. versionchanged:: next
268+
Accepts any real number, not only integer or float.
269+
261270

262271
.. function:: localtime([secs])
263272

@@ -271,6 +280,9 @@ Functions
271280
:c:func:`gmtime` failure. It's common for this to be restricted to years
272281
between 1970 and 2038.
273282

283+
.. versionchanged:: next
284+
Accepts any real number, not only integer or float.
285+
274286

275287
.. function:: mktime(t)
276288

@@ -382,8 +394,7 @@ Functions
382394
.. function:: sleep(secs)
383395

384396
Suspend execution of the calling thread for the given number of seconds.
385-
The argument may be a floating-point number to indicate a more precise sleep
386-
time.
397+
The argument may be a non-integer to indicate a more precise sleep time.
387398

388399
If the sleep is interrupted by a signal and no exception is raised by the
389400
signal handler, the sleep is restarted with a recomputed timeout.
@@ -428,6 +439,9 @@ Functions
428439
.. versionchanged:: 3.13
429440
Raises an auditing event.
430441

442+
.. versionchanged:: next
443+
Accepts any real number, not only integer or float.
444+
431445
.. index::
432446
single: % (percent); datetime format
433447

Doc/whatsnew/3.15.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ Other language changes
279279
and ends with a forward slash (``/``).
280280
(Contributed by Serhiy Storchaka in :gh:`134716`.)
281281

282+
* Functions that take timestamp or timeout arguments now accept any
283+
real numbers, not only integers or floats, although this does not
284+
improve precision.
285+
(Contributed by Serhiy Storchaka in :gh:`67795`.)
286+
282287

283288
New modules
284289
===========

0 commit comments

Comments
 (0)