]> granicus.if.org Git - python/commitdiff
Issue #10330: trace module can now be used with python built without threads.
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>
Sat, 6 Nov 2010 01:31:16 +0000 (01:31 +0000)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>
Sat, 6 Nov 2010 01:31:16 +0000 (01:31 +0000)
Lib/trace.py

index ec45d812ac6ac5c3a55e56d06eb331229b6255d2..b24d193d358208072e3a4507a66d10e24e570b51 100644 (file)
@@ -53,7 +53,6 @@ import linecache
 import os
 import re
 import sys
-import threading
 import time
 import token
 import tokenize
@@ -62,6 +61,22 @@ import gc
 import dis
 import pickle
 
+try:
+    import threading
+except ImportError:
+    _settrace = sys.settrace
+
+    def _unsettrace():
+        sys.settrace(None)
+else:
+    def _settrace(func):
+        threading.settrace(func)
+        sys.settrace(func)
+
+    def _unsettrace():
+        sys.settrace(None)
+        threading.settrace(None)
+
 def usage(outfile):
     outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]
 
@@ -491,14 +506,12 @@ class Trace:
         if globals is None: globals = {}
         if locals is None: locals = {}
         if not self.donothing:
-            threading.settrace(self.globaltrace)
-            sys.settrace(self.globaltrace)
+            _settrace(self.globaltrace)
         try:
             exec(cmd, globals, locals)
         finally:
             if not self.donothing:
-                sys.settrace(None)
-                threading.settrace(None)
+                _unsettrace()
 
     def runfunc(self, func, *args, **kw):
         result = None