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

Skip to content

Commit 6315ffa

Browse files
committed
Merge.
2 parents 9dca535 + 504f5c3 commit 6315ffa

6 files changed

Lines changed: 64 additions & 8 deletions

File tree

Doc/library/runpy.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ The :mod:`runpy` module provides two functions:
2828

2929
.. function:: run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)
3030

31+
.. index::
32+
module: __main__
33+
3134
Execute the code of the specified module and return the resulting module
3235
globals dictionary. The module's code is first located using the standard
3336
import mechanism (refer to :pep:`302` for details) and then executed in a
@@ -87,6 +90,9 @@ The :mod:`runpy` module provides two functions:
8790

8891
.. function:: run_path(file_path, init_globals=None, run_name=None)
8992

93+
.. index::
94+
module: __main__
95+
9096
Execute the code at the named filesystem location and return the resulting
9197
module globals dictionary. As with a script name supplied to the CPython
9298
command line, the supplied path may refer to a Python source file, a

Lib/asyncio/base_events.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import collections
1818
import concurrent.futures
1919
import heapq
20+
import inspect
2021
import logging
2122
import socket
2223
import subprocess
@@ -37,6 +38,15 @@
3738
_MAX_WORKERS = 5
3839

3940

41+
def _format_handle(handle):
42+
cb = handle._callback
43+
if inspect.ismethod(cb) and isinstance(cb.__self__, tasks.Task):
44+
# format the task
45+
return repr(cb.__self__)
46+
else:
47+
return str(handle)
48+
49+
4050
class _StopError(BaseException):
4151
"""Raised to stop the event loop."""
4252

@@ -128,6 +138,9 @@ def __init__(self):
128138
self._clock_resolution = time.get_clock_info('monotonic').resolution
129139
self._exception_handler = None
130140
self._debug = False
141+
# In debug mode, if the execution of a callback or a step of a task
142+
# exceed this duration in seconds, the slow callback/task is logged.
143+
self.slow_callback_duration = 0.1
131144

132145
def __repr__(self):
133146
return ('<%s running=%s closed=%s debug=%s>'
@@ -823,16 +836,16 @@ def _run_once(self):
823836
if logger.isEnabledFor(logging.INFO):
824837
t0 = self.time()
825838
event_list = self._selector.select(timeout)
826-
t1 = self.time()
827-
if t1-t0 >= 1:
839+
dt = self.time() - t0
840+
if dt >= 1:
828841
level = logging.INFO
829842
else:
830843
level = logging.DEBUG
831844
if timeout is not None:
832845
logger.log(level, 'poll %.3f took %.3f seconds',
833-
timeout, t1-t0)
846+
timeout, dt)
834847
else:
835-
logger.log(level, 'poll took %.3f seconds', t1-t0)
848+
logger.log(level, 'poll took %.3f seconds', dt)
836849
else:
837850
event_list = self._selector.select(timeout)
838851
self._process_events(event_list)
@@ -855,7 +868,16 @@ def _run_once(self):
855868
ntodo = len(self._ready)
856869
for i in range(ntodo):
857870
handle = self._ready.popleft()
858-
if not handle._cancelled:
871+
if handle._cancelled:
872+
continue
873+
if self._debug:
874+
t0 = self.time()
875+
handle._run()
876+
dt = self.time() - t0
877+
if dt >= self.slow_callback_duration:
878+
logger.warning('Executing %s took %.3f seconds',
879+
_format_handle(handle), dt)
880+
else:
859881
handle._run()
860882
handle = None # Needed to break cycles when an exception occurs.
861883

Lib/test/test_asyncio/test_base_events.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,34 @@ def coroutine_function():
969969
with self.assertRaises(TypeError):
970970
self.loop.run_in_executor(None, coroutine_function)
971971

972+
@mock.patch('asyncio.base_events.logger')
973+
def test_log_slow_callbacks(self, m_logger):
974+
def stop_loop_cb(loop):
975+
loop.stop()
976+
977+
@asyncio.coroutine
978+
def stop_loop_coro(loop):
979+
yield from ()
980+
loop.stop()
981+
982+
asyncio.set_event_loop(self.loop)
983+
self.loop.set_debug(True)
984+
self.loop.slow_callback_duration = 0.0
985+
986+
# slow callback
987+
self.loop.call_soon(stop_loop_cb, self.loop)
988+
self.loop.run_forever()
989+
fmt, *args = m_logger.warning.call_args[0]
990+
self.assertRegex(fmt % tuple(args),
991+
"^Executing Handle.*stop_loop_cb.* took .* seconds$")
992+
993+
# slow task
994+
asyncio.async(stop_loop_coro(self.loop), loop=self.loop)
995+
self.loop.run_forever()
996+
fmt, *args = m_logger.warning.call_args[0]
997+
self.assertRegex(fmt % tuple(args),
998+
"^Executing Task.*stop_loop_coro.* took .* seconds$")
999+
9721000

9731001
if __name__ == '__main__':
9741002
unittest.main()

Lib/test/test_descr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ class C(object):
11491149
except (TypeError, UnicodeEncodeError):
11501150
pass
11511151
else:
1152-
raise TestFailed("[chr(128)] slots not caught")
1152+
self.fail("[chr(128)] slots not caught")
11531153

11541154
# Test leaks
11551155
class Counted(object):

Lib/test/test_pydoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ def test_builtin(self):
746746
try:
747747
pydoc.render_doc(name)
748748
except ImportError:
749-
self.fail('finding the doc of {!r} failed'.format(o))
749+
self.fail('finding the doc of {!r} failed'.format(name))
750750

751751
for name in ('notbuiltins', 'strrr', 'strr.translate',
752752
'str.trrrranslate', 'builtins.strrr',

PCbuild/prepare_ssl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def main():
186186

187187
ssl_dir = sys.argv[1]
188188

189-
if not os.path.exists(ssl_dir) and os.path.isdir(ssl_dir):
189+
if not os.path.isdir(ssl_dir):
190190
print(ssl_dir, "is not an existing directory!")
191191
sys.exit(1)
192192

0 commit comments

Comments
 (0)