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

Skip to content

gh-79888: support __index__ and __float__ in time functions #11636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 44 additions & 19 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ class _PyTime(enum.IntEnum):
)


class IndexLike:
def __init__(self, value):
self.value = int(value)
def __index__(self):
return self.value

class FloatLike:
def __init__(self, value):
self.value = float(value)
def __float__(self):
return self.value


class TimeTestCase(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -150,7 +163,7 @@ def test_conversions(self):
def test_sleep(self):
self.assertRaises(ValueError, time.sleep, -2)
self.assertRaises(ValueError, time.sleep, -1)
time.sleep(1.2)
time.sleep(0.2)

def test_strftime(self):
tt = time.gmtime(self.t)
Expand Down Expand Up @@ -453,21 +466,24 @@ def test_monotonic(self):
self.assertGreaterEqual(t2, t1, "times=%s" % times)
t1 = t2

# monotonic() includes time elapsed during a sleep
t1 = time.monotonic()
time.sleep(0.5)
t2 = time.monotonic()
dt = t2 - t1
self.assertGreater(t2, t1)
# bpo-20101: tolerate a difference of 50 ms because of bad timer
# resolution on Windows
self.assertTrue(0.450 <= dt)

# monotonic() is a monotonic but non adjustable clock
info = time.get_clock_info('monotonic')
self.assertTrue(info.monotonic)
self.assertFalse(info.adjustable)

def test_monotic_sleep(self):
# This tests both time.sleep() and time.monotonic(): we test various
# types of input to time.sleep() and check using time.monotonic() that
# we sleep sufficiently long.
for T in (0.5, decimal.Decimal('0.5'), FloatLike(0.5), IndexLike(1)):
t1 = time.monotonic()
time.sleep(T)
t2 = time.monotonic()
dt = t2 - t1
# bpo-20101: tolerate a difference of 50 ms because of bad timer
# resolution on Windows
self.assertGreaterEqual(dt, float(T) - 0.05)

def test_perf_counter(self):
time.perf_counter()

Expand Down Expand Up @@ -839,14 +855,23 @@ def convert_values(ns_timestamps):
pytime_converter(value, time_rnd)

def check_int_rounding(self, pytime_converter, expected_func,
unit_to_sec=1, value_filter=None):
unit_to_sec=1, value_filter=None, *, index=True):
self._check_rounding(pytime_converter, expected_func,
False, unit_to_sec, value_filter)
if index:
def convert_IndexLike(x, rnd):
return pytime_converter(IndexLike(x), rnd)
self._check_rounding(convert_IndexLike, expected_func,
False, unit_to_sec, value_filter)

def check_float_rounding(self, pytime_converter, expected_func,
unit_to_sec=1, value_filter=None):
self._check_rounding(pytime_converter, expected_func,
True, unit_to_sec, value_filter)
def convert_FloatLike(x, rnd):
return pytime_converter(FloatLike(x), rnd)
self._check_rounding(convert_FloatLike, expected_func,
True, unit_to_sec, value_filter)

def decimal_round(self, x):
d = decimal.Decimal(x)
Expand All @@ -870,7 +895,7 @@ def c_int_filter(secs):

self.check_int_rounding(lambda secs, rnd: PyTime_FromSeconds(secs),
lambda secs: secs * SEC_TO_NS,
value_filter=c_int_filter)
value_filter=c_int_filter, index=False)

# test nan
for time_rnd, _ in ROUNDING_MODES:
Expand Down Expand Up @@ -904,7 +929,7 @@ def float_converter(ns):

self.check_int_rounding(lambda ns, rnd: PyTime_AsSecondsDouble(ns),
float_converter,
NS_TO_SEC)
NS_TO_SEC, index=False)

# test nan
for time_rnd, _ in ROUNDING_MODES:
Expand Down Expand Up @@ -941,7 +966,7 @@ def seconds_filter(secs):
self.check_int_rounding(PyTime_AsTimeval,
timeval_converter,
NS_TO_SEC,
value_filter=seconds_filter)
value_filter=seconds_filter, index=False)

@unittest.skipUnless(hasattr(_testcapi, 'PyTime_AsTimespec'),
'need _testcapi.PyTime_AsTimespec')
Expand All @@ -954,21 +979,21 @@ def timespec_converter(ns):
self.check_int_rounding(lambda ns, rnd: PyTime_AsTimespec(ns),
timespec_converter,
NS_TO_SEC,
value_filter=self.time_t_filter)
value_filter=self.time_t_filter, index=False)

def test_AsMilliseconds(self):
from _testcapi import PyTime_AsMilliseconds

self.check_int_rounding(PyTime_AsMilliseconds,
self.create_decimal_converter(MS_TO_NS),
NS_TO_SEC)
NS_TO_SEC, index=False)

def test_AsMicroseconds(self):
from _testcapi import PyTime_AsMicroseconds

self.check_int_rounding(PyTime_AsMicroseconds,
self.create_decimal_converter(US_TO_NS),
NS_TO_SEC)
NS_TO_SEC, index=False)


class TestOldPyTime(CPyTimeTestCase, unittest.TestCase):
Expand Down Expand Up @@ -1016,7 +1041,7 @@ def test_object_to_timeval(self):
self.create_converter(SEC_TO_US),
value_filter=self.time_t_filter)

# test nan
# test nan
for time_rnd, _ in ROUNDING_MODES:
with self.assertRaises(ValueError):
pytime_object_to_timeval(float('nan'), time_rnd)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Various time-related functions now use the ``__index__`` and ``__float__``
methods to convert objects to time. This affects in particular
``time.sleep()``.
Loading