-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdebug.py
More file actions
1124 lines (1021 loc) · 31.7 KB
/
Copy pathdebug.py
File metadata and controls
1124 lines (1021 loc) · 31.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
# Assorted debugging facilities.
# - Cameron Simpson <cs@cskk.id.au> 20apr2013
#
r'''
Assorted debugging facilities.
If the environment variable `$CS_DEBUG_BUILTINS` is set to a comma
separated list of names then the `builtins` module will be monkey
patched with those names, enabling trite debug use of those names
anywhere in the code provided this module has been imported somewhere.
Particularly, when debugging programmes which read data from the
standard input (`sys.stdin`) it is helpful to monkey patch `breakpoint`
with the function from this module, which attaches to `/dev/tty`
for the duration of the breakpoint call.
The allowed names are the list `cs.debug.__all__` and include:
* `X`: `cs.x.X`
* `abrk`: a decorator to call `breakpoint()` on logic errors such as `AssertionError`
* `breakpoint`: a wrapper for the builtin `breakpoint` which attaches to `/dev/tty`
* `pformat`: `pprint.pformat`
* `pprint`: `pprint.pprint`
* `print`: `cs.upd.print`
* `r`: `cs.lex.r`
* `redirect_stdout`: `contextlib.redirect_stdout`
* `s`: `cs.lex.s`
* `stack_dump`: dump current `Thread`'s call stack
* `thread_dump` dump the active `Thread`s with their call stacks
* `trace`: the `@trace` decorator
`$CS_DEBUG_BUILTINS` can also be set to `"1"` to install all of
`__all__` in the builtins.
'''
from __future__ import print_function
import builtins
from builtins import breakpoint as _breakpoint
from cmd import Cmd
from collections import defaultdict
from contextlib import redirect_stdout
import inspect
import logging
import os
from pprint import pformat, pprint # pylint: disable=unused-import
import re
from subprocess import Popen, PIPE
import sys
from threading import (
enumerate as enumerate_threads,
Lock as threading_Lock,
RLock as threading_RLock,
Thread as threading_Thread,
)
import time
import traceback
from types import SimpleNamespace as NS
from typing import Mapping, Sequence
from cs.context import stackattrs
from cs.deco import ALL, attr, decorator
from cs.fs import shortpath
from cs.lex import (
cropped_repr,
s,
r,
is_identifier,
is_dotted_identifier,
printt,
) # pylint: disable=unused-import
import cs.logutils
from cs.logutils import debug, error, warning, D, ifdebug, loginfo
from cs.obj import Proxy
from cs.pfx import Pfx
from cs.py.func import funccite, funcname, func_a_kw_fmt
from cs.py.stack import caller, frames
from cs.py3 import Queue, Queue_Empty, exec_code
from cs.seq import seq
from cs.threads import ThreadState
from cs.upd import print # pylint: disable=redefined-builtin
from cs.x import X
__version__ = '20260602-post'
DISTINFO = {
'keywords': ["python2", "python3"],
'classifiers': [
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
],
'install_requires': [
'cs.context',
'cs.deco',
'cs.fs',
'cs.lex',
'cs.logutils',
'cs.obj',
'cs.pfx',
'cs.py.func',
'cs.py.stack',
'cs.py3',
'cs.seq',
'cs.threads',
'cs.upd',
'cs.x',
],
}
__all__ = ['X', 'pformat', 'pprint', 'print', 'r', 'redirect_stdout', 's']
# environment variable specifying names to become built in
CS_DEBUG_BUILTINS_ENVVAR = 'CS_DEBUG_BUILTINS'
# white list of allowed builtin names
CS_DEBUG_BUILTINS_NAMES = ('X', 'pformat', 'pprint', 's', 'r', 'trace')
# @DEBUG dispatches a thread to monitor function elapsed time.
# This is how often it polls for function completion.
DEBUG_POLL_RATE = 0.25
@ALL
class TimingOutLock(object):
''' A `Lock` replacement which times out, used for locating deadlock points.
'''
def __init__(self, deadlock_timeout=20.0, recursive=False):
self._lock = threading_RLock() if recursive else threading_Lock()
self._deadlock_timeout = deadlock_timeout
def acquire(self, blocking=True, timeout=-1, name=None):
if timeout < 0:
timeout = self._deadlock_timeout
else:
timeout = min(timeout, self._deadlock_timeout)
ok = (
self._lock.acquire(timeout=timeout)
if blocking else self._lock.acquire(blocking=blocking)
)
if not ok:
raise RuntimeError(
"TIMEOUT acquiring lock held by %s:%r" %
(self.owner, self.owner_name)
)
self.owner = caller()
self.owner_name = name
return True
def release(self):
return self._lock.release()
def __enter__(self):
self.acquire()
self.owner = caller()
return True
def __exit__(self, *a):
return self._lock.__exit__(*a)
class TraceSuite(object):
''' Context manager to trace start and end of a code suite.
'''
def __init__(self, msg, *a):
if a:
msg = msg % a
self.msg = msg
def __enter__(self):
X("TraceSuite ENTER %s", self.msg)
def __exit__(self, exc_type, exc_value, exc_tb):
X("TraceSuite LEAVE %s: exc_value=%s", self.msg, exc_value)
def Thread(*a, **kw):
if not ifdebug():
return threading_Thread(*a, **kw)
filename, lineno = inspect.stack()[1][1:3]
return DebuggingThread({'filename': filename, 'lineno': lineno}, *a, **kw)
@ALL
def thread_dump(Ts=None, fp=None):
''' Write thread identifiers and stack traces to the file `fp`.
Parameters:
* `Ts`: the `Thread`s to dump; if unspecified use `threading.enumerate()`.
* `fp`: the file to which to write; if unspecified use `sys.stderr`.
'''
if Ts is None:
Ts = enumerate_threads()
if fp is None:
fp = sys.stderr
with Pfx("thread_dump"):
frames = sys._current_frames()
for T in Ts:
try:
frame = frames[T.ident]
except KeyError:
warning("no frame for Thread.ident=%s", T.ident)
continue
print("Thread", T.ident, T.name, T, file=fp)
traceback.print_stack(frame, None, fp)
print(file=fp)
@ALL
def stack_dump(stack=None, limit=None, logger=None, log_level=None):
''' Dump a stack trace to a logger.
Parameters:
* `stack`: a stack list as returned by `traceback.extract_stack`.
If missing or `None`, use the result of `traceback.extract_stack()`.
If `stack` has a `.tb_frame` or `.__traceback__` attribute,
extract the stack from that (this covers traceback objects and exceptions).
* `limit`: a limit to the number of stack entries to dump.
If missing or `None`, dump all entries.
* `logger`: a `logger.Logger` ducktype or the name of a logger.
If missing or `None`, obtain a logger from `logging.getLogger()`.
* `log_level`: the logging level for the dump.
If missing or `None`, use `cs.logutils.loginfo.level`.
'''
stack = frames(stack, limit=limit)
if logger is None:
logger = logging.getLogger()
elif isinstance(logger, str):
logger = logging.getLogger(logger)
if log_level is None:
log_level = getattr(loginfo, 'level', logging.WARNING)
for text in traceback.format_list(stack):
for line in text.splitlines():
logger.log(log_level, line.rstrip())
def DEBUG(f, force=False):
''' Decorator to wrap functions in timing and value debuggers.
'''
from cs.result import Result
def inner(*a, **kw):
if not force and not ifdebug():
return f(*a, **kw)
filename, lineno = inspect.stack()[1][1:3]
n = seq()
R = Result()
T = threading_Thread(
target=_debug_watcher, args=(filename, lineno, n, f.__name__, R)
)
T.daemon = True
T.start()
debug(
"%s:%d: [%d] call %s(*%r, **%r)", filename, lineno, n, f.__name__, a,
kw
)
start = time.time()
try:
retval = f(*a, **kw)
except Exception as e:
error("EXCEPTION from %s(*%s, **%s): %s", f, a, kw, e)
raise
end = time.time()
debug(
"%s:%d: [%d] called %s, elapsed %gs, got %r", filename, lineno, n,
f.__name__, end - start, retval
)
R.put(retval)
return retval
return inner
def _debug_watcher(filename, lineno, n, funcname, R):
slow = 2
sofar = 0
slowness = 0
while not R.ready:
if slowness >= slow:
debug(
"%s:%d: [%d] calling %s, %gs elapsed so far...", filename, lineno, n,
funcname, sofar
)
# reset report time and complain more slowly next time
slowness = 0
slow += 1
time.sleep(DEBUG_POLL_RATE)
sofar += DEBUG_POLL_RATE
slowness += DEBUG_POLL_RATE
def DF(func, *a, **kw):
''' Wrapper for a function call to debug its use.
This requires rewriting the call from `f(*a,*kw)` to `DF(f,*a,**kw)`.
Alternatively one could rewrite as `DEBUG(f)(*a,**kw)`.
'''
return DEBUG(func, force=True)(*a, **kw)
class DebugWrapper(NS):
''' Base class for classes presenting debugging wrappers.
'''
def debug(self, msg, *a):
if a:
msg = msg % a
cs.logutils.debug(': '.join((self.debug_label, msg)))
@property
def debug_label(self):
info = '%s:%d' % (self.filename, self.lineno)
try:
context = self.context
except AttributeError:
pass
else:
info = ':'.join(info, str(context))
label = '%s-%d[%s]' % (self.__class__.__name__, id(self), info)
return label
class DebuggingLock(DebugWrapper):
''' Wrapper class for `threading.Lock` to trace creation and use.
`cs.threads.Lock()` returns one of these in debug mode or a raw
`threading.Lock` otherwise.
'''
def __init__(self, *, slow=2, **dkw):
DebugWrapper.__init__(self, **dkw)
self.debug("__init__(slow=%r)", slow)
if slow <= 0:
raise ValueError("slow must be positive, received: %r" % (slow,))
self.slow = slow
self.lock = threading_Lock()
self.held = None
def __enter__(self):
##self.lock.__enter__()
self.acquire()
return self
def __exit__(self, *a):
##return self.lock.__exit__(*a)
self.release()
return False
def acquire(self, *a):
''' Acquire the lock.
'''
# quietly support Python 3 arguments after blocking parameter
blocking = True
if a:
blocking = a[0]
a = a[1:]
filename, lineno = inspect.stack()[1][1:3]
debug("%s:%d: acquire(blocking=%s)", filename, lineno, blocking)
if blocking:
# blocking
# try non-blocking first
# if successful, good
# otherwise spawn a monitoring thread to report on slow acquisition
# and block
taken = self.lock.acquire(False)
if not taken:
Q = Queue()
T = Thread(target=self._timed_acquire, args=(Q, filename, lineno))
T.daemon = True
T.start()
taken = self.lock.acquire(blocking, *a)
Q.put(taken)
else:
# non-blocking: do ordinary lock acquisition
taken = self.lock.acquire(blocking, *a)
if taken:
self.held = (filename, lineno)
return taken
def release(self):
''' Release the lock.
'''
filename, lineno = inspect.stack()[0][1:3]
debug("%s:%d: release()", filename, lineno)
self.held = None
self.lock.release()
def _timed_acquire(self, Q, filename, lineno):
''' Block waiting for lock acquisition.
Report slow acquisition.
This would be inline above except that Python 2 `Lock`s do
not have a timeout parameter, hence this thread.
This probably scales VERY badly if there is a lot of `Lock`
contention.
'''
slow = self.slow
sofar = 0
slowness = 0
while True:
# block until lock acquired
try:
Q.get(True, 1)
except Queue_Empty:
sofar += 1
slowness += 1
if slowness >= slow:
self.debug(
"from %s:%d: acquire: after %gs, held by %s", filename, lineno,
sofar, self.held
)
# complain more slowly next time
slowness = 0
slow += 1
else:
break
class DebuggingRLock(DebugWrapper):
''' Wrapper class for threading.RLock to trace creation and use.
`cs.threads.RLock()` returns on of these in debug mode or a raw
`threading.RLock` otherwise.
'''
def __init__(self, owner=None, **dkw):
if owner is None:
owner = caller()
DebugWrapper.__init__(
self, filename=owner.filename, lineno=owner.lineno, **dkw
)
self.debug('__init__')
self.lock = threading_RLock()
self.stack = []
def __str__(self):
return "%s[%s:%s]%s" % (
type(self).__name__,
shortpath(self.filename),
self.lineno,
"->".join(
["%s:%s" % filename_lineno for filename_lineno in self.stack]
),
)
def __enter__(self, locker=None):
if locker is None:
locker = caller()
filename_lineno = locker.filename, locker.lineno
self.debug('from %s:%d: __enter__ ...', locker.filename, locker.lineno)
entry = self.lock.__enter__()
self.stack.append(filename_lineno)
return entry
def __exit__(self, *a, exiter=None):
if exiter is None:
exiter = caller()
self.debug('%s:%d: __exit__(*%s) ...', exiter.filename, exiter.lineno, a)
exited = self.lock.__exit__(*a)
self.stack.pop()
return exited
def acquire(self, blocking=True, timeout=-1, acquirer=None):
if acquirer is None:
acquirer = caller()
self.debug(
'%s:%d: acquire(blocking=%s)', acquirer.filename, acquirer.lineno,
blocking
)
if timeout < 0:
ret = self.lock.acquire(blocking)
else:
ret = self.lock.acquire(blocking, timeout)
if ret:
self.stack.append((acquirer.filename, acquirer.lineno))
return ret
def release(self, releaser=None):
if releaser is None:
releaser = caller()
self.debug('%s:%d: release()', releaser.filename, releaser.lineno)
self.lock.release()
self.stack.pop()
Lock = DebuggingLock
RLock = DebuggingRLock
_debug_threads = set()
def dump_debug_threads():
D("dump_debug_threads:")
for T in _debug_threads:
D("dump_debug_threads: thread %r: %r", T.name, T.debug_label)
D("dump_debug_threads done")
class DebuggingThread(threading_Thread, DebugWrapper):
def __init__(self, dkw, *a, **kw):
DebugWrapper.__init__(self, **dkw)
self.debug("NEW THREAD(*%r, **%r)", a, kw)
_debug_threads.add(self)
threading_Thread.__init__(self, *a, **kw)
@DEBUG
def join(self, timeout=None):
self.debug("join(timeout=%r)...", timeout)
retval = threading_Thread.join(self, timeout=timeout)
self.debug("join(timeout=%r) completed", timeout)
_debug_threads.discard(self)
return retval
class TracingObject(Proxy):
def __init__(self, other):
Proxy.__init__(self, other)
self.__attr_map = {}
def __getattribute__(self, attr):
X("TracingObject.__getattribute__(attr=%r)", attr)
_proxied = Proxy.__getattribute__(self, '_proxied')
try:
value = object.__getattribute__(_proxied, attr)
except AttributeError:
X("no .%s attribute", attr)
raise
else:
X("getattr .%s", attr)
return TracingObject(value)
def __call__(self, *a, **kw):
_proxied = Proxy.__getattribute__(self, '_proxied')
X("call %s(*%r, **%r)", _proxied, a, kw)
return _proxied(*a, **kw)
class DummyMap(object):
def __init__(self, label, d=None):
X("new DummyMap labelled %r, d=%r", label, d)
self.__label = label
self.__map = {}
if d:
self.__map.update(d)
def __str__(self):
return self.__label
def items(self):
X("%s.items", self)
return []
def __getitem__(self, key):
v = self.__map.get(key)
X("%s[%r] => %r", self, key, v)
return v
def openfiles(substr=None, pid=None):
''' Run lsof(8) against process `pid`
returning paths of open files whose paths contain `substr`.
Parameters:
* `substr`: default substring to select by; default returns all paths.
* `pid`: process to examine; default from `os.getpid()`.
'''
if pid is None:
pid = os.getpid()
paths = []
P = Popen(['lsof', '-p', str(pid)], stdout=PIPE)
for lsof in P.stdout:
lsof = lsof.decode()
fields = lsof.split()
if len(fields) >= 9:
if fields[4] == 'REG':
if substr is None or substr in fields[8]:
paths.append(fields[8])
P.wait()
return paths
class DebugShell(Cmd):
''' An interactive prompt for python statements, attached to `/dev/tty` by default.
'''
def __init__(self, var_dict, stdin=None, stdout=None):
if stdin is None:
stdin = open('/dev/tty', 'r')
if stdout is None:
stdout = open('/dev/tty', 'a')
self.stdin = stdin
self.stdout = stdout
Cmd.__init__(self, stdin=stdin, stdout=stdout)
self.vars = var_dict
def default(self, line):
''' Default command action.
'''
if line == 'EOF':
return True
try:
exec_code(line, globals(), self.vars)
except Exception as e:
X("Exception: %s", e)
self.stdout.flush()
return False
def debug_object_shell(o, prompt=None):
''' Interactive prompt for inspecting variables.
'''
if prompt is None:
prompt = str(o) + '> '
v = o.__dict__
C = DebugShell(v)
intro = '\n\n'
for k in sorted(v.keys()):
intro += '\n %s = %r' % (k, v[k])
intro += '\n'
C.prompt = prompt
C.cmdloop(intro)
_trace_state = ThreadState(indent='')
def log_via_print(msg, *a, file=None):
''' Logging style message using `cs.upd.print`.
'''
if a:
msg = msg % a
if file is None:
file = sys.stdout
print(msg, file=file, flush=True)
@ALL
def breakpoint(*a, **kw):
''' Wrapper for buildins.breakpoint()` which attaches `/dev/tty`
as `sys.stdin` if `sys.stdin` is not a tty.
'''
if sys.stdin.isatty():
return _breakpoint(*a, **kw)
with open('/dev/tty', 'r') as ttyf:
with stackattrs(sys, stdin=ttyf):
print(
'breakpoint wrapper using /dev/tty, type "up" to go to the normal stack frame'
)
return _breakpoint(*a, **kw)
@ALL
@decorator
def abrk(func, exceptions=(AssertionError, NameError, RuntimeError)):
''' A decorator to intercept the specified `exceptions`
(by default `AssertionError`, `NameError`, `RuntimeError`)
and call `breakpoint()`.
The breakpoint frame contains:
- `func`: the wrapper function
- `func_a`, `func_kw`: the function positional and keyword arguments
Examples:
@abrk
def broken_function(......):
@property
@abrk(exceptions=AttributeError)
def broken_property(......):
'''
def cs_debug_abrk_wrapper(*func_a, **func_kw):
try:
return func(*func_a, **func_kw)
except exceptions as e:
warning(
"%s: %s\n func = %s\n func_a = %r\nfunc_kw = %r",
funccite(func),
e,
funccite(func),
func_a,
func_kw,
)
breakpoint()
raise
return cs_debug_abrk_wrapper
@ALL
@decorator
# pylint: disable=too-many-arguments
def trace(
func,
call=True,
retval=False,
exception=True,
use_pformat=False,
with_caller=True,
with_pfx=False,
xlog=None,
verbose=False,
breakpoint=False,
):
''' Decorator to report the call and return of a function.
Decorator parameters:
* `call`: trace the call, default `True`
* `retval`: trace the return, default `False`
* `exception`: trace raised exceptions, default `True`
* `use_pformat`: present the return value using
`pformat` instead of `repr`, default `False`
* `with_caller`: include the caller if this function, default `True`
* `with_pfx`: include the current `Pfx` prefix, default `False`
'''
citation = funcname(func) ## funccite(func)
fmtv = pformat if use_pformat else cropped_repr
def traced_function_wrapper(*a, **kw):
''' Wrapper for `func` to trace call and return.
'''
global _trace_state # pylint: disable=global-statement
if with_pfx:
# late import so that we can use this in modules we import
# pylint: disable=import-outside-toplevel
try:
from cs.pfx import XP as xlog
except ImportError:
xlog = X
else:
xlog = X
log_cite = citation
old_indent = _trace_state.indent
_trace_state.indent += ' '
indent = _trace_state.indent
if call:
fmt, av = func_a_kw_fmt(log_cite, *a, **kw)
if verbose and (a or kw):
xlog("%sCALL %s(", old_indent, log_cite)
for arg in a:
xlog("%s %s,", old_indent, r(arg, None))
for kwname, kwarg in kw.items():
xlog("%s %s=%s,", old_indent, kwname, r(kwarg, None))
xlog("%s )", old_indent)
else:
xlog("%sCALL " + fmt, old_indent, *av)
if with_caller:
xlog("%sFROM %s", indent, caller(-4))
if breakpoint:
breakpoint() if callable(breakpoint) else builtins.breakpoint()
start_time = time.time()
try:
result = func(*a, **kw)
except Exception as e:
end_time = time.time()
if exception:
xlog_kw = {}
if xlog is X:
xlog_kw['colour'] = 'white' ## 'red'
xlog(
"%sRAISE %s => %s:%s\n"
"%s at %s\n"
"%s elapsed %gs",
indent,
log_cite,
e.__class__.__name__,
e,
indent,
(
"no-frame" if e.__traceback__.tb_next is None else
e.__traceback__.tb_next.tb_frame
),
indent,
end_time - start_time,
**xlog_kw,
)
_trace_state.indent = old_indent
raise
end_time = time.time()
if inspect.isgeneratorfunction(func):
iterator = result
def traced_generator():
while True:
next_time = time.time()
if call:
xlog(
"%sNEXT %s at %gs",
old_indent,
log_cite,
next_time - start_time,
)
try:
item = next(iterator)
except StopIteration:
yield_time = time.time()
xlog(
"%sDONE %s in %gs",
indent,
log_cite,
yield_time - next_time,
)
break
except Exception as e:
end_time = time.time()
if exception:
xlog_kw = {}
if xlog is X:
xlog_kw['colour'] = 'red'
xlog(
"%sRAISE %s => %s:%s\n"
"%s at %s\n"
"%s elapsed %gs\n",
indent,
log_cite,
e.__class__.__name__,
e,
indent,
e.__traceback__.tb_next.tb_frame,
indent,
end_time - start_time,
**xlog_kw,
)
_trace_state.indent = old_indent
raise
else:
yield_time = time.time()
xlog(
"%sYIELD %s => %s at %gs",
old_indent,
log_cite,
fmtv(item),
yield_time - next_time,
)
yield item
result = traced_generator()
else:
if retval:
xlog(
"%sRETURN %s => %s:%s in %gs",
indent, ##_trace_state.indent,
log_cite,
result.__class__.__name__,
fmtv(result),
end_time - start_time,
)
_trace_state.indent = old_indent
return result
traced_function_wrapper.__name__ = "@trace(%s)" % (citation,)
traced_function_wrapper.__doc__ = "@trace(%s)\n\n" + (func.__doc__ or '')
return traced_function_wrapper
def trace_DEBUG(debug_spec=None):
''' Apply the `@trace` decorator to functions specified by `debug_spec`,
default from the environment variable `$DEBUG`.
'''
with Pfx("trace_DEBUG"):
try:
import importlib
except ImportError as e:
warning("trace_DEBUG: cannot import importlib, no applying: %s", e)
return
if debug_spec is None:
debug_spec = os.environ.get('DEBUG', '')
if isinstance(debug_spec, str):
debug_spec = debug_spec.split(',')
with Pfx("%r", debug_spec):
module_names = []
function_names = []
for spec in debug_spec:
with Pfx(spec):
if is_dotted_identifier(spec):
module_names.append(spec)
elif ':' in spec:
# module:funcname
module_name, func_name = spec.split(':', 1)
if (is_dotted_identifier(module_name)
and is_dotted_identifier(func_name)):
function_names.append((module_name, func_name))
for module_name in module_names:
with Pfx("module %s", module_name):
try:
M = importlib.import_module(module_name)
except ImportError as e:
warning("cannot import: %s", e)
continue
M.DEBUG = True
for module_name, func_name in function_names:
with Pfx("function %s:%s", module_name, func_name):
try:
M = importlib.import_module(module_name)
except ImportError as e:
warning("cannot import: %s", e)
continue
try:
F = getattr(M, func_name)
except AttributeError as e:
warning("function %s not found: %s", e)
continue
if callable(F):
setattr(M, func_name, trace(F))
@ALL
@attr(
# types whose values we check by value not id()
BASIC_TYPES=(int, float, str),
# a regexp to match name(x=1,y=2,...) repr()s
REPR_re=re.compile(r'[._\w]+\((([\w_]+=\S+, ?)*[\w_]+=\S+)\)$')
)
def tabulate_obj(obj, label=None, *, seen=None):
''' Tabulate the contents of an object for display via `cs.lex.printt()`.
'''
BASIC_TYPES = tabulate_obj.BASIC_TYPES
##print(type(obj), repr(obj))
if label is None:
label = f'{obj.__class__.__name__}:{id(obj)}'
if seen is None:
seen = set()
if id(obj) in seen:
##print("SEEN")
yield [label, "" if isinstance(obj, BASIC_TYPES) else "(already seen)"]
return
seen.add(id(obj))
objcls = obj.__class__
# dotted attributes
attrmap = None
if not isinstance(obj, BASIC_TYPES):
try:
attrmap = obj.__dict__
except AttributeError:
# TODO: unpack descriptors from dir(type(obj))?
try:
clsslots = objcls.__slots__
except AttributeError:
pass
else:
if clsslots:
attrmap = {name: getattr(obj, name) for name in clsslots}
else:
attrmap = {
attr: value
for attr, value in attrmap.items()
if not attr.startswith('_')
}
if not attrmap:
# recognised namedtuples and their ilk via repr()
m = tabulate_obj.REPR_re.match(repr(obj))
if m:
attrmap = {}
for fv in re.split(', ?', m.group(1)):
f, v = fv.split('=', 1)
try:
v = int(v)
except ValueError:
try:
v = float(v)
except ValueError:
pass
attrmap[f] = v
assert attrmap is None or isinstance(attrmap, Mapping), (
f'attrmap should be a Mapping but is {type(attrmap)} {attrmap!r}'
)
# collection members
items = None
if isinstance(obj, Mapping):
try:
items = sorted(obj.items())
except TypeError:
items = list(obj.items())
# sequences, excluding strings which just make more strings
elif not isinstance(obj, str) and isinstance(obj, Sequence):
items = list(enumerate(obj))
assert items is None or isinstance(items, Sequence)
if not attrmap and not items:
yield [label, obj]
else:
yield [label, f'{obj.__class__.__name__}:{id(obj)}']
attrs_by_value_id = defaultdict(list) # indexed rows to subsume
attrs_by_value = defaultdict(list) # indexed rows to subsume
attr_indices = defaultdict(list) # attrs for values
if attrmap is None:
attrmap = {}
else:
for attr, value in sorted(attrmap.items()):
if not attr.startswith('_'):
if isinstance(value, BASIC_TYPES):
attrs_by_value[value].append(attr)
else:
attrs_by_value_id[id(value)].append(attr)
if items is None:
items = ()
else:
# figure out which values have been seen
for index, value in items:
if isinstance(value, BASIC_TYPES):
attr_names = attrs_by_value.get(value, ())
else:
attr_names = attrs_by_value_id.get(id(value), ())
for attr in attr_names:
attr_indices[attr].append(index)
# generate the listing
subrows = []
# list the attributes
used_indices = set()
for attr, indices in sorted(attrmap.items()):
label = f'.{attr}'
indices = attr_indices.get(attr, ())
if indices:
label += f', also [{",".join(map(repr, indices))}]'
used_indices.update(indices)
subrows.extend(tabulate_obj(attrmap[attr], label, seen=seen))
# list the unmentioned indices
for index, value in items: