Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
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
32 changes: 32 additions & 0 deletions 32 Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,38 @@ def test_sleep(self):
with self.subTest(value=value):
time.sleep(value)

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

with self.subTest(value = index_like):
self._test_sleep_duration(index_like)

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


with self.subTest(value = float_like):
self._test_sleep_duration(float_like)

def _test_sleep_duration(self, secs):
t1 = time.monotonic()
time.sleep(secs)
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(float(secs) - 0.05 <= dt)


def test_epoch(self):
# bpo-43869: Make sure that Python use the same Epoch on all platforms:
# January 1, 1970, 00:00:00 (UTC).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
support object with __float__ defined in time.sleep
11 changes: 11 additions & 0 deletions 11 Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,17 @@ pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
*tp = ns;
return 0;
}
else if (PyFloat_Check(obj)) {
double d = PyFloat_AsDouble(obj);
if (d == -1 && PyErr_Occurred()) {
return -1;
}
if (isnan(d)) {
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
return -1;
}
return pytime_from_double(tp, d, round, unit_to_ns);
}
else {
double d;
d = PyFloat_AsDouble(obj);
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.