Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
945c82ee
Commit
945c82ee
authored
Mar 12, 2015
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test
parent
f7cc3fcc
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
99 additions
and
57 deletions
+99
-57
Doc/library/time.rst
Doc/library/time.rst
+4
-0
Lib/test/eintrdata/eintr_tester.py
Lib/test/eintrdata/eintr_tester.py
+16
-1
Lib/test/test_signal.py
Lib/test/test_signal.py
+10
-7
Modules/timemodule.c
Modules/timemodule.c
+69
-49
No files found.
Doc/library/time.rst
View file @
945c82ee
...
...
@@ -350,6 +350,10 @@ The module defines the following functions and data items:
requested by an arbitrary amount because of the scheduling of other activity
in the system.
.. versionchanged:: 3.5
The function now sleeps at least *secs* even if the sleep is interrupted
by a signal (see :pep:`475` for the rationale).
.. function:: strftime(format[, t])
...
...
Lib/test/eintrdata/eintr_tester.py
View file @
945c82ee
...
...
@@ -252,8 +252,23 @@ class SocketEINTRTest(EINTRBaseTest):
lambda
path
:
os
.
close
(
os
.
open
(
path
,
os
.
O_WRONLY
)))
@
unittest
.
skipUnless
(
hasattr
(
signal
,
"setitimer"
),
"requires setitimer()"
)
class
TimeEINTRTest
(
EINTRBaseTest
):
""" EINTR tests for the time module. """
def
test_sleep
(
self
):
t0
=
time
.
monotonic
()
time
.
sleep
(
2
)
signal
.
alarm
(
0
)
dt
=
time
.
monotonic
()
-
t0
self
.
assertGreaterEqual
(
dt
,
1.9
)
def
test_main
():
support
.
run_unittest
(
OSEINTRTest
,
SocketEINTRTest
)
support
.
run_unittest
(
OSEINTRTest
,
SocketEINTRTest
,
TimeEINTRTest
)
if
__name__
==
"__main__"
:
...
...
Lib/test/test_signal.py
View file @
945c82ee
...
...
@@ -419,17 +419,20 @@ class WakeupSignalTests(unittest.TestCase):
TIMEOUT_HALF = 5
signal.alarm(1)
before_time = time.time()
# We attempt to get a signal during the sleep,
# before select is called
time.sleep(TIMEOUT_FULL)
mid_time = time.time()
dt = mid_time - before_time
if dt >= TIMEOUT_HALF:
raise Exception("%s >= %s" % (dt, TIMEOUT_HALF))
try:
select.select([], [], [], TIMEOUT_FULL)
except InterruptedError:
pass
else:
raise Exception("select() was not interrupted")
before_time = time.time()
select.select([read], [], [], TIMEOUT_FULL)
after_time = time.time()
dt = after_time -
mid
_time
dt = after_time -
before
_time
if dt >= TIMEOUT_HALF:
raise Exception("%s >= %s" % (dt, TIMEOUT_HALF))
"""
,
signal
.
SIGALRM
)
...
...
Modules/timemodule.c
View file @
945c82ee
...
...
@@ -1386,74 +1386,94 @@ floattime(_Py_clock_info_t *info)
static
int
floatsleep
(
double
secs
)
{
/* XXX Should test for MS_WINDOWS first! */
#if
defined(HAVE_SELECT) && !defined(__EMX__)
struct
timeval
t
;
_PyTime_timeval
deadline
,
monotonic
;
#if
ndef MS_WINDOWS
struct
timeval
t
imeout
;
double
frac
;
int
err
;
int
err
=
0
;
_PyTime_monotonic
(
&
deadline
);
_PyTime_ADD_SECONDS
(
deadline
,
secs
);
while
(
1
)
{
frac
=
fmod
(
secs
,
1
.
0
);
secs
=
floor
(
secs
);
t
.
tv_sec
=
(
long
)
secs
;
t
.
tv_usec
=
(
long
)(
frac
*
1000000
.
0
);
timeout
.
tv_sec
=
(
long
)
secs
;
timeout
.
tv_usec
=
(
long
)(
frac
*
1000000
.
0
);
Py_BEGIN_ALLOW_THREADS
err
=
select
(
0
,
(
fd_set
*
)
0
,
(
fd_set
*
)
0
,
(
fd_set
*
)
0
,
&
t
);
err
=
select
(
0
,
(
fd_set
*
)
0
,
(
fd_set
*
)
0
,
(
fd_set
*
)
0
,
&
timeou
t
);
Py_END_ALLOW_THREADS
if
(
err
!=
0
)
{
#ifdef EINTR
if
(
errno
==
EINTR
)
{
if
(
!
(
err
!=
0
&&
errno
==
EINTR
))
break
;
/* select() was interrupted by a signal */
if
(
PyErr_CheckSignals
())
return
-
1
;
_PyTime_monotonic
(
&
monotonic
);
secs
=
_PyTime_INTERVAL
(
monotonic
,
deadline
);
if
(
secs
<=
0
.
0
)
{
err
=
0
;
errno
=
0
;
break
;
}
else
#endif
{
}
if
(
err
!=
0
)
{
PyErr_SetFromErrno
(
PyExc_OSError
);
return
-
1
;
}
}
#elif defined(__WATCOMC__) && !defined(__QNX__)
/* XXX Can't interrupt this sleep */
Py_BEGIN_ALLOW_THREADS
delay
((
int
)(
secs
*
1000
+
0
.
5
));
/* delay() uses milliseconds */
Py_END_ALLOW_THREADS
#elif defined(MS_WINDOWS)
{
double
millisecs
=
secs
*
1000
.
0
;
#else
double
millisecs
;
unsigned
long
ul_millis
;
DWORD
rc
;
HANDLE
hInterruptEvent
;
_PyTime_monotonic
(
&
deadline
);
_PyTime_ADD_SECONDS
(
deadline
,
secs
);
do
{
millisecs
=
secs
*
1000
.
0
;
if
(
millisecs
>
(
double
)
ULONG_MAX
)
{
PyErr_SetString
(
PyExc_OverflowError
,
"sleep length is too large"
);
return
-
1
;
}
Py_BEGIN_ALLOW_THREADS
/* Allow sleep(0) to maintain win32 semantics, and as decreed
* by Guido, only the main thread can be interrupted.
*/
ul_millis
=
(
unsigned
long
)
millisecs
;
if
(
ul_millis
==
0
||
!
_PyOS_IsMainThread
())
Sleep
(
ul_millis
);
if
(
ul_millis
==
0
||
!
_PyOS_IsMainThread
())
{
Py_BEGIN_ALLOW_THREADS
Sleep
(
0
);
Py_END_ALLOW_THREADS
break
;
}
else
{
DWORD
rc
;
HANDLE
hInterruptEvent
=
_PyOS_SigintEvent
();
hInterruptEvent
=
_PyOS_SigintEvent
();
ResetEvent
(
hInterruptEvent
);
Py_BEGIN_ALLOW_THREADS
rc
=
WaitForSingleObjectEx
(
hInterruptEvent
,
ul_millis
,
FALSE
);
if
(
rc
==
WAIT_OBJECT_0
)
{
Py_BLOCK_THREADS
errno
=
EINTR
;
PyErr_SetFromErrno
(
PyExc_OSError
);
return
-
1
;
}
}
Py_END_ALLOW_THREADS
if
(
rc
!=
WAIT_OBJECT_0
)
break
;
/* WaitForSingleObjectEx() was interrupted by SIGINT */
if
(
PyErr_CheckSignals
())
return
-
1
;
_PyTime_monotonic
(
&
monotonic
);
secs
=
_PyTime_INTERVAL
(
monotonic
,
deadline
);
if
(
secs
<=
0
.
0
)
break
;
}
#else
/* XXX Can't interrupt this sleep */
Py_BEGIN_ALLOW_THREADS
sleep
((
int
)
secs
);
Py_END_ALLOW_THREADS
}
while
(
1
);
#endif
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment