Skip to content

Commit

Permalink
Simplify code in time stamper thread and make sure the dup-ed stderr …
Browse files Browse the repository at this point in the history
…is closed in the end.
  • Loading branch information
scoder committed Feb 23, 2019
1 parent 2c0d088 commit b07ee50
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,11 @@ def time_stamper_thread(interval=10):
Print regular time stamps into the build logs to find slow tests.
@param interval: time interval in seconds
"""
if not interval or interval < 0:
# Do nothing
yield
return

try:
_xrange = xrange
except NameError:
Expand All @@ -2218,35 +2223,33 @@ def time_stamper_thread(interval=10):
import datetime
from time import sleep

if not interval or interval < 0:
# Do nothing
interval = _xrange(interval * 4)
now = datetime.datetime.now
stop = False

# We capture stderr in some places.
# => make sure we write to the real (original) stderr of the test runner.
stderr = os.dup(2)
def write(s):
os.write(stderr, s if type(s) is bytes else s.encode('ascii'))

def time_stamper():
while True:
for _ in interval:
if stop:
return
sleep(1./4)
write('\n#### %s\n' % now())

thread = threading.Thread(target=time_stamper, name='time_stamper')
thread.setDaemon(True) # Py2 ...
thread.start()
try:
yield
else:
interval = _xrange(interval * 4)
now = datetime.datetime.now
stop = False

# We capture stderr in some places.
# => make sure we write to the real (original) stderr of the test runner.
def write(s, stderr=os.dup(2)):
os.write(stderr, s if IS_PY2 else s.encode('ascii'))

def time_stamper():
while True:
for _ in interval:
if stop:
return
sleep(1./4)
write('\n#### %s\n' % now())

thread = threading.Thread(target=time_stamper, name='time_stamper')
thread.setDaemon(True) # Py2 ...
thread.start()
try:
yield
finally:
stop = True
thread.join()
finally:
stop = True
thread.join()
os.close(stderr)


def configure_cython(options):
Expand Down

0 comments on commit b07ee50

Please sign in to comment.