summaryrefslogtreecommitdiffstats
path: root/SCons/Debug.py
diff options
context:
space:
mode:
authorMats Wichmann <mats@linux.com>2020-05-29 17:14:49 (GMT)
committerMats Wichmann <mats@linux.com>2020-05-29 17:14:49 (GMT)
commit46810dae9a306c934f89ad6b3f37cc82eb2026a5 (patch)
tree425c5968063f587ee3b24b438a6402f65bb60d61 /SCons/Debug.py
parent47b761fa3f9e24419db48393ca9d6064f7b5d21c (diff)
downloadSCons-46810dae9a306c934f89ad6b3f37cc82eb2026a5.zip
SCons-46810dae9a306c934f89ad6b3f37cc82eb2026a5.tar.gz
SCons-46810dae9a306c934f89ad6b3f37cc82eb2026a5.tar.bz2
Close scons logfiles on completion
Files written to in logging operations could remain unclosed: more modern Pythons grumble about this; given the type of Python build, could emit ResourceWarning messages which cause tests to fail. Close by registering calls with atexit. Affects Trace, cache debug, taskmastertrace, configure. Signed-off-by: Mats Wichmann <mats@linux.com>
Diffstat (limited to 'SCons/Debug.py')
-rw-r--r--SCons/Debug.py34
1 files changed, 26 insertions, 8 deletions
diff --git a/SCons/Debug.py b/SCons/Debug.py
index 706b4c4..10cd2f5 100644
--- a/SCons/Debug.py
+++ b/SCons/Debug.py
@@ -33,6 +33,7 @@ caller_trace()
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+import atexit
import os
import sys
import time
@@ -66,7 +67,7 @@ def string_to_classes(s):
def fetchLoggedInstances(classes="*"):
classnames = string_to_classes(classes)
return [(cn, len(tracked_classes[cn])) for cn in classnames]
-
+
def countLoggedInstances(classes, file=sys.stdout):
for classname in string_to_classes(classes):
file.write("%s: %d\n" % (classname, len(tracked_classes[classname])))
@@ -201,22 +202,39 @@ if sys.platform == 'win32':
TraceDefault = 'con'
else:
TraceDefault = '/dev/tty'
-
-TimeStampDefault = None
+TimeStampDefault = False
StartTime = time.time()
PreviousTime = StartTime
-def Trace(msg, file=None, mode='w', tstamp=None):
- """Write a trace message to a file. Whenever a file is specified,
- it becomes the default for the next call to Trace()."""
+def Trace(msg, filename=None, mode='w', tstamp=False):
+ """Write a trace message.
+
+ Write messages when debugging which do not interfere with stdout.
+ Useful in tests, which monitor stdout and would break with
+ unexpected output. Trace messages can go to the console (which is
+ opened as a file), or to a disk file; the file argument persists
+ across calls unless overridden.
+
+ Args:
+ filename: file to write trace message to. If omitted,
+ write to the previous trace file (default: console).
+ mode: file open mode (default: 'w')
+ tstamp: write relative timestamps with trace. Outputs time since
+ scons was started, and time since last trace (default: False)
+
+ """
global TraceDefault
global TimeStampDefault
global PreviousTime
+
+ def traace_cleanup(traceFP):
+ traceFP.close()
+
if file is None:
file = TraceDefault
else:
TraceDefault = file
- if tstamp is None:
+ if not tstamp:
tstamp = TimeStampDefault
else:
TimeStampDefault = tstamp
@@ -225,6 +243,7 @@ def Trace(msg, file=None, mode='w', tstamp=None):
except KeyError:
try:
fp = TraceFP[file] = open(file, mode)
+ atexit.register(trace_cleanup, fp)
except TypeError:
# Assume we were passed an open file pointer.
fp = file
@@ -234,7 +253,6 @@ def Trace(msg, file=None, mode='w', tstamp=None):
PreviousTime = now
fp.write(msg)
fp.flush()
- fp.close()
# Local Variables:
# tab-width:4